Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 21st, 2005, 6:00 PM   #1
Fred
Programmer
 
Fred's Avatar
 
Join Date: Feb 2005
Posts: 67
Rep Power: 4 Fred is on a distinguished road
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
Fred is offline   Reply With Quote
Old May 21st, 2005, 8:42 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old May 22nd, 2005, 5:22 AM   #3
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
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.
ZenMasterJG is offline   Reply With Quote
Old May 22nd, 2005, 3:45 PM   #4
Fred
Programmer
 
Fred's Avatar
 
Join Date: Feb 2005
Posts: 67
Rep Power: 4 Fred is on a distinguished road
I think i got it again...
Thanks a lot for the help guys!!!! Almost forgot what a great forum this was...
Fred is offline   Reply With Quote
Old May 23rd, 2005, 11:32 AM   #5
jonaskoelker
Newbie
 
Join Date: May 2005
Location: Denmark
Posts: 13
Rep Power: 0 jonaskoelker is on a distinguished road
Send a message via ICQ to jonaskoelker Send a message via AIM to jonaskoelker Send a message via MSN to jonaskoelker Send a message via Yahoo to jonaskoelker
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
jonaskoelker is offline   Reply With Quote
Old Jun 23rd, 2005, 5:51 PM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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"
The following two are equivalent:
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()
Cerulean is offline   Reply With Quote
Old Aug 20th, 2005, 8:36 AM   #7
jonaskoelker
Newbie
 
Join Date: May 2005
Location: Denmark
Posts: 13
Rep Power: 0 jonaskoelker is on a distinguished road
Send a message via ICQ to jonaskoelker Send a message via AIM to jonaskoelker Send a message via MSN to jonaskoelker Send a message via Yahoo to jonaskoelker
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)
hth --Jonas
__________________
now go away or I shall taunt you a second time :D
jonaskoelker is offline   Reply With Quote
Old Aug 20th, 2005, 1:41 PM   #8
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
Cerulean: class methods recieve the class as implicit first argument. static methods (`>>> help(staticmethod)') don't get an implicit argument.
Indeed. I'm quite aware of that..?
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC