[Tutor] question on self

wesley chun wescpy at gmail.com
Tue Mar 13 17:07:15 CET 2012


i want to expand specifically on steve's response and note the big
distinction that needs to be made for everyone is that this is primary the
difference between calling a *function* and calling a *method* (which is a
function that belongs to/defined for a class).

with that instance (self), that method is considered "bound," and Python
automagically passes it in as the first argument to that method (self). if
you wish to call an *unbound* method, you need to pass an instance on your
own *and* reference it via its class, i.e., YourClass.TakeTurns(self) --
readability takes a blow there.

btw, if you want to make just a function call *and* that function doesn't
have much to do with the class, then just define it as a function. a
seldomly-used alternative is to make it a static method (using the
@staticmethod decorator) -- this lets you define a method within a class
but pretend it's like a function (where you don't need to use the instance
[self]).

cheers,
--wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.chun : wescpy-gmail.com : @wescpy/+wescpy
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120313/93f0fc8e/attachment.html>


More information about the Tutor mailing list