Why do class methods always need 'self' as the first parameter?

Chris Torek nospam at torek.net
Tue Sep 6 03:10:40 CEST 2011


>Chris Torek <nospam at torek.net> writes:
>[snip]
>> when you have [an] instance and call [an] instance or class method:

[note: I have changed the names very slightly here, and removed
additional arguments, on purpose]

>>     black_knight = K()
>>     black_knight.spam()
>>     black_knight.eggs()
>>
>> the first parameters ... are magic, and invisible.
>>
>> Thus, Python is using the "explicit is better than implicit" rule
>> in the definition, but not at the call site. ...

In article <m2wrdnf53u.fsf at cochabamba.vanoostrum.org>
Piet van Oostrum  <piet at vanoostrum.org> wrote:
>It *is* explicit also at the call site. It only is written at the left
>of the dot rather than at the right of the parenthesis.

It cannot possibly be explicit.  The first parameter to one of the
method functions is black_knight, but the first parameter to the
other method is black_knight.__class__.

Which one is which?  Is spam() the instance method and eggs() the
class method, or is spam() the class method and eggs the instance
method?  (One does not, and should not, have to *care*, which is
kind of the point here. :-) )

>And that is necessary to locate which definition of the method
>applies.

By "that" I assume you mean the name "black_knight" here.  But the
name is not required to make the call; see the last line of the
following code fragment:

    funclist = []
    ...
    black_knight = K()
    funclist.append(black_knight.spam)
    funclist.append(black_knight.eggs)
    ...
    # At this point, let's say len(funclist) > 2,
    # and some number of funclist[i] entries are ordinary
    # functions that have no special first parameter.
    random.choice(funclist)()

>It would be silly to repeat this information after the parenthesis.
>Not only silly, it would be stupid as it would be a source of errors,
>and an example of DRY.

Indeed.  But I believe the above is a demonstration of how the
"self" or "cls" parameter is in fact implicit, not explicit.

(I am using python 2.x, and doing this in the interpreter:

    random.choice(funclist)

-- without the parentheses to call the function -- produces:

    <bound method K.[name omitted] of <__main__.K object at 0x249f50>>
    <bound method type.[name omitted] of <class '__main__.K'>>
    <function ordinary at 0x682b0>

The first is the instance method, whose name I am still keeping
secret; the second is the class method; and the third is the ordinary
function I added to the list.  The actual functions print their
own name and their parameters if any, and one can see that the
class and instance methods get one parameter, and the ordinary
function gets none.)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html



More information about the Python-list mailing list