tags:

views:

71

answers:

4

I am running through a tutorial online at http://www.sthurlow.com/python/lesson08/ and I believe I understand how classes work in python, at least to some degree but when I run this code:

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

I get this error:

Traceback (most recent call last):
  File "Y:/python/Shape.py", line 1, in -toplevel-
    class Shape:
  File "Y:/python/Shape.py", line 17, in Shape
    self.y = self.y * scale
NameError: name 'self' is not defined

Any Help would be great

Thanks

Richard

+10  A: 

You need to indent the last line.

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

Should be

def scaleSize(self,scale): 
    self.x = self.x * scale 
    self.y = self.y * scale 
klausbyskov
Thank you for such a quick reply. This is a the first time I used stackoverflow. I can already see that this going to be a great community to get involved in. Thanks all.
Richard
@Richard, if this answer has answered your question, its suggested you click the arrow next to the answer box. This ensures anyone else who comes to the site with the identical problem and quickly identify which one works.
Anthony Forloney
+1  A: 

The indicated line has the wrong indent level, so it's not in the scaleSize() method.

unwind
+3  A: 

The last part of your code is wrong,

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

Notice the indentation, which is important in Python. By what you have now, means self.y = self.y * scale is not inside the scaleSize method and the keyword self is undefined for your Shape class.

What you need to do is properly indent,

def scaleSize(self,scale):
    self.x = self.x * scale
    self.y = self.y * scale
Anthony Forloney
+1  A: 

You need to align line 17 with line 16

You have:

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

Must be:

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

That's how python knows they belong to the same method.

OscarRyz