[Tutor] why "self" in methods?

Bob Gailer bgailer at alum.rpi.edu
Sun Apr 4 06:05:53 EDT 2004


At 08:52 PM 4/3/2004, Chris Lott wrote:
>Why do object methods need the (self) parameter rather than just accepting 
>() as a function can?

Note that this applies to "class" methods rather than to "object" methods.

>I mean, given:
>
>class Foo:
>         def doIt(self):
>                 print "did it"
>
>why couldn't it be:
>         def doIt():

That's how it is in Visual Foxpro (VFP). As a consequence VFP needs a 
keyword "this" to identify the instance calling the method.

Python lets you give the instance variable any name you like. "self" is 
used by tradition.

Also Python's approach allows for "unbound" methods.

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 




More information about the Tutor mailing list