![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2005
Posts: 67
Rep Power: 4
![]() |
Classes and methods
I did not touch python for qiiet a while, and now i am readin some code on the net, and i am wondering: 'Where the heck does this 'self' in the classes come from??'
Here an example: class Bag:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
def addtwice(self, x):
self.add(x)
self.add(x)what exactely is the self.data doing there Hwy not use JUST data... I read the reason to this somewhere a looong time ago, but can't remember or refind it. Any feedback would be appriciated!!! thx |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,827
Rep Power: 5
![]() |
self automatically applies it throughout the entire class, as long as you put self in all the function arguments.
In your example, self.data can be altered in a different function from where it was called in (def add). If you were to try to do the same thing with functions rather classes, it would crash because data wouldn't be defined yet. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
if i remember correctly, self is the reference to the object your talking about that python sends to class method calls. since each object AND each method is its own namespace, without self python wouldn't know which data you were talking about. if you just said data, python would look for a data variable in the method's namespace, which isnt what you want. self.data tells python to look in the object's namespace.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Posts: 67
Rep Power: 4
![]() |
I think i got it again...
Thanks a lot for the help guys!!!! Almost forgot what a great forum this was... ![]() |
|
|
|
|
|
#5 |
|
Newbie
|
to be fair, the first argument doesn't have to be named `self'; that's just a convention.
__________________
now go away or I shall taunt you a second time :D |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Just clarifying - by default methods in the class block are bound methods, meaning calling myobject.foo() will pass "myobject" as the first parameter to the foo method. If I have the following class..
class Foo:
def bar(self):
print "bar"f = Foo() f.bar() f = Foo() Foo.bar(f) If you don't want this behaviour, you can make your method static like so: class Foo:
def bar():
print "foo"
bar = classmethod(bar)
// You can now call the method in the class.
Foo.bar() |
|
|
|
|
|
#7 |
|
Newbie
|
Cerulean: class methods recieve the class as implicit first argument. static methods (`>>> help(staticmethod)') don't get an implicit argument.
Also, in 2.4+, there's decorators: class Foo:
@classmethod
def bar(cls, foo): print cls, type(cls)
__________________
now go away or I shall taunt you a second time :D |
|
|
|
|
|
#8 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|