You are not logged in.
Please, give me an example. I'm tired of definitions and such that I find in the web. I can't learn these damn classes...
Offline
class Pen(object):
def __init__(self, colour, type):
self.colour = colour
self.type = type
def get_colour(self):
return self.colour
def get_type(self):
return self.type
def print_info(self):
print "colour: %s" % self.get_colour()
print "type: %s" % self.get_type()
In use:
>>> p = Pen('red', 'felt tipped')
>>> p.colour
'red'
>>> p.get_colour()
'red'
>>> p.print_info()
colour: red
type: felt tipped
>>>
>>> Pen.get_colour(p)
'red'
Basically, 'self' is a way to reference the object you are currently 'inside'. So if you had created an object and called it 'p', you could access its attribute with p.colour. But if you're inside that object, how do you access the attribute? that's where self comes in.
when you call p.get_colour(), python actually calls Pen.get_colour(), but automatically passes p as the first argument, which goes into self. That's why:
p.get_colour()
and
Pen.get_colour(p)
are more or less identical.
Hope this helps!
Dusty
Offline
Suppose you are making a class that describes cars and the car has his type of fuel as a class variable, Like this: (java syntax, I don't know python that well, but you'll get the idea)
public class car {
private String fueltype;
}
You can now refer to that variable as fueltype or as this.fueltype ("this" is what is called "self" in python). At first sight this gives no more power, but consider this method:
public class car{
private String fueltype;
public void setFueltype(String fueltype) {
if ( ... ) {
this.fueltype = fueltype;
}
}
}
See the difference? fueltype refers to the variable with the smallest scope, here that is the local variable passed as an argument. If you want to refer to the instance variable, you use "this" (self), which means "this object", so you ask after this objects property called "fueltype".
Last edited by Ramses de Norre (2008-10-08 10:35:15)
Offline
Thanks, Dusty, I think I get it... sort of..
Ramses de Norre, when I start learning Java I'll get here and read it again. Thanks anyway.
Offline
Example class and function:
class A(object):
def __init__(self):
self.x = 1
def set_x(self, value):
self.x = value
def set_x_2(object, v):
object.x = v
def do_not_set_x(self, value):
x = value
def fun_set_x(object, v):
object.x = v
and ipython session:
In [2]: a = A()
In [3]: a.x
Out[3]: 1
In [4]: a.set_x(2)
In [5]: a.x
Out[5]: 2
In [6]: a.set_x_2(3)
In [7]: a.x
Out[7]: 3
In [8]: a.do_not_set_x(4)
In [9]: a.x
Out[9]: 3
In [10]: fun_set_x(a, 5)
In [11]: a.x
Out[11]: 5
Offline
Thanks, Husio.
Offline
Note: the name 'self' is just a tradition. You can call it anyway you like, as long it is the first in the argument list, will always receive a reference to the current object. So, in constrast, 'self' is not a reserved word like 'this' in other languages - it's explicity declared.
Offline
Thanks, Dusty, I think I get it... sort of..
Ramses de Norre, when I start learning Java I'll get here and read it again. Thanks anyway.
Don't bother the syntax, look at the difference between the referencing to variables. The situation is completely identical in python and java.
Offline
Not quite completely identical -- self is an explicit reference in python. Some have even said its not truly object oriented because of that.
Dusty
Offline
True, but the use is the same.
Offline