python classes question

Started by
1 comment, last by Zahlman 16 years, 9 months ago
so in classes you have to as the first paramater always include the same thing. for example(self). in my book the paramaters that they passed lets say "name" and "point1" in the __init__ method they would redefine them as self.name = name and self.point1 = point1 why do they do this? in classes i understand that variables created in each method are accessible by the other methods, unlike in regular function definitions. so does name work in other methods? or since its a parameter is it only local to that definition? so does defining self.name = name make self.name global to all the other methods? and if so is the self necessary? thanks oh heres an example of a class im trying to make right now. im trying to get it to draw a simple door. right now the class doesnt work says something about cant set something to something on line 11. if u need the exact error ill look it up. plz tell me why it does not work and then also am i using self in the wrong places? or unnecessary places? thanks again hey ho hey

from graphics import *



class Door:
    def __init__(self, name, p1, p2, win):
        self.name = name
        self.p1 = p1
        self.p2 = p2
        self.width = self.p2.getX() = self.p1.getX()
        self.length = self.p2.getY() - self.p1.getY()
        self.middle = Point((self.width / 2), (self.length / 2))
        self.door = Rectangle(p1, p2)
        self.door.setFill('black')
        self.door.draw(win)
        Text(middle, name).draw(win)


oops i feel like an idiot i just realized my mistake. it was a typo :( self.width = self.p2.getX() = self.p1.getX() meant to have a minus sign where i have the second equals sign. but still if anyone can answer my question am i using self correctly and im using it unecessarily? thanks hey [Edited by - HeyHoHey on July 14, 2007 10:17:40 PM]
Advertisement
Inside classes you will need to use self.variable in order to reference them.

There is a performance advantage from making them local however. So if you wish you can reassign them to a local value when you start a method. You can also do the following

self.variable = variable = value

You can then reference variable as well as self.variable.
Quote:Original post by HeyHoHey
so in classes you have to as the first paramater always include the same thing. for example(self). in my book the paramaters that they passed lets say "name" and "point1" in the __init__ method they would redefine them as self.name = name and self.point1 = point1 why do they do this?


That is not redefinition, it is assignment.

In this case, we say "objects of this class have a 'name' that will be initialized with the 'name' value that's passed to __init__, and a 'point1' value that will be initialized with the 'point1' value that's passed to __init__".

The 'name' and 'point1' that get passed in could be anything that get passed in from anywhere else in the program. We give the object access to some specific values, which are the name and point1 of that object. Remember, classes define a type, so we need to say what the properties of that type are - i.e. the things that you can do with it. And that in turn requires that we save some data, with which to do the work.

Quote:in classes i understand that variables created in each method are accessible by the other methods, unlike in regular function definitions.


It's not "variables created in each method"; it's members of the class. I.e., parts of the object whose method is being called.

Quote:so does name work in other methods? or since its a parameter is it only local to that definition? so does defining self.name = name make self.name global to all the other methods? and if so is the self necessary?


In Python, the 'self' is necessary. The name "self" is arbitrary, and used by convention by Pythonistas: in reality, whatever is the first argument to the function is what gets used.

ALL that is happening is: when you write 'foo.bar(baz)', it gets translated into something like 'foo.__class__.bar(foo, baz)' - i.e., the 'bar' function belongs to the *class*, so we reach into the class to find the function, and pass the *object* to it automatically, so that its members can be accessed - "normally", i.e. in exactly the same way we would access members of baz from within bar.

thanks

Quote:oh heres an example of a class im trying to make right now. im trying to get it to draw a simple door. right now the class doesnt work says something about cant set something to something on line 11. if u need the exact error ill look it up. plz tell me why it does not work and then also am i using self in the wrong places? or unnecessary places?


Don't draw the Door from its __init__ function. __init__ only exists to *initialize* the door, i.e., create it. This means we'll need to remember the 'win'(dow?) so that we know what it is when we .draw() a Door. This allows us to draw the same door several times.

As for "unnecessary" places, it depends on whether you need to remember all those values. Chances are the width and length are redundant. In general, you should avoid storing redundant information, instead calculating it on demand. This avoids errors that occur when you change the values that something's calculated from, and forget to recalculate it.

So it would look like this:

class Door:    def __init__(self, name, p1, p2, win):        self.name = name        self.p1 = p1        self.p2 = p2        self.win = win    def draw(self):        # I think you really want something like this for the 'middle' calculation:        size = self.p2 - self.p1 # both at once        middle = self.p1 + size/2 # i.e., offset relative to the door        rect = Rectangle(p1, p2)        rect.setFill('black')        rect.draw(self.win)        Text(middle, self.name).draw(self.win)

This topic is closed to new replies.

Advertisement