[Tutor] Know Thy Self
Joseph J. Strout
joe@strout.net
Tue, 13 Apr 1999 09:37:06 -0700
At 9:19 AM -0700 04/13/99, Arne Mueller wrote:
>As far as I know it's a referecne to the calling object.
No, it's a reference to the object by which a method was invoked. Let's
make a simpler example:
class Spammer:
def __init__(self):
self.str = "spam "
# note: don't use 'string' for a variable; it's a module!
def spam(self,n):
print self.str * n
foo = Spammer() # create a Spammer object
foo.spam(5) # tell it to spam
You see, when you say "foo.spam(5)", this is implicitly converted to:
Spammer.spam(foo,5)
And so within that method, 'self' refers to 'foo'. That's it. That's all
that's going on. Note that it does *not* refer to the calling object... in
the example above, there is NO calling object it might refer to! If you
want to pass a reference to the calling object to another object's method,
you'll have to pass it in explicitly like any other parameter.
Cheers,
-- Joe
,------------------------------------------------------------------.
| Joseph J. Strout Biocomputing -- The Salk Institute |
| joe@strout.net http://www.strout.net |
`------------------------------------------------------------------'