• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

working with classes

im new to python  - i only started learning about it yesterday as i want o learn a new language.

Ive being following some tutorials to get me started.  im working with classes and cannot fix this issue:

The shell window returns this:

Traceback (most recent call last):
  File "W:/Python/examples/ex14.py", line 2, in <module>
    class Shape:
  File "W:/Python/examples/ex14.py", line 18, in Shape
    self.y = self.y * scale
NameError: name 'self' is not defined

Here is my code:

#An example of a class
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

rectangle = Shape(100,45)

#finding the area of your rectangle:
print rectangle.area()

#finding the perimeter of your rectangle:
print rectangle.perimeter()

#describing the rectangle
rectangle.describe("A wide rectangle, more than twice\
 as wide as it is tall")

#making the rectangle 50% smaller
rectangle.scaleSize(0.5)

#re-printing the new area of the rectangle
print rectangle.area()
0
ellandrd
Asked:
ellandrd
  • 5
  • 3
1 Solution
 
peprCommented:
Bad indentation of self.y = self.y * scale. It probably is the part of scaleSize() method, but you indented it to the same level as the definition of the method. Put 4 more spaces in front.
0
 
ellandrdAuthor Commented:
so indentation will cause problem?  i didnt know that...

0
 
peprCommented:
The self is not the part of the class. Interpreter correctly reports that 'self' does not exist. It was never created as a part of the class. It was always used only as the reference to the instance of the class inside the methods.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
peprCommented:
It is one of the complaints of beginners. It is rather unusual for current programming languages to mark a block of commands via indentation. But it is essential in Python. Anyway, it is part of a good style to indent perfectly also in other languages. Later, you will not think about it as about something strange. It is simply unusual.
0
 
peprCommented:
Sorry, you are not a total beginner ;)
0
 
ellandrdAuthor Commented:
not to programming no, but yes to python and with out the brackets like other languages i didnt notice that the line wasnt indented but your sharp eyes did...

my script works perfect now..

thank you.
0
 
peprCommented:
Thanks for the points. However, my eyes are sharp only when I clean my glasses 8) The real truth is that Python is quite good in pointing to problems. See

Traceback (most recent call last):
  File "W:/Python/examples/ex14.py", line 2, in <module>
    class Shape:
  File "W:/Python/examples/ex14.py", line 18, in Shape
    self.y = self.y * scale
NameError: name 'self' is not defined

So, it was easy to spot the problem when you know that it is on the line 18. You, as a Python beginner, were only slighly blinded by not knowing what should you search for ;)
0
 
ellandrdAuthor Commented:
i did actually look at that line before opening this question, but because i didn't know about the indentation, i wasn't able to fix it myself without your help...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now