1 Replies - 304 Views - Last Post: 16 September 2011 - 11:56 AM Rate Topic: -----

#1 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Class won't work

Posted 16 September 2011 - 11:47 AM

I am following a tutorial on python 2.x and can't figure out why the code example for a class does not work.

I get this error:
"self.y = self.y * scale
NameError: name 'self' is not defined"

Is there a syntax error in this code, or is it a python compatibility issue? I'm using version 2.7, but this code is version 2.4 I believe. I though it was version 3 that was incompatible. Any idea why this code won't work? I thought 'self' was a built in thing.

class Shape:
    def __init__(self,x,y):
        self.x = x
        self.y = y

    description = "This shape has not been described yet"
    author = "Nobody has claimed to make this shape yet"

    def area(self):
        return self.x * self.y

    def perimeter(self):
        return 2 * self.x + 2 * self.y

    def describe(self,text):
        self.description = text

    def authorName(self,text):
        self.author = text

    def scaleSize(self,scale):
        self.x = self.x * scale

    self.y = self.y * scale




Is This A Good Question/Topic? 0
  • +

Replies To: Class won't work

#2 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: Class won't work

Posted 16 September 2011 - 11:56 AM

Sorry never-mind, indentation issue. >.<
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1