[Tutor] Know Thy Self

Tom Jenkins TomJenkins@zentuit.com
Tue, 13 Apr 1999 12:52:53 -0400


Arne,
Joe is more technically correct since you could say 
Spammer.spam(foo, 5) as he points out.  I just never think about it like 
that.

> 
> >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              |
> `------------------------------------------------------------------'
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
> 


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Tom Jenkins                    DevIS (http://www.devis.com)
"In a world without fences, who needs Gates?"