Is this the proper way to use a class method?

Ian Kelly ian.g.kelly at gmail.com
Fri Mar 2 09:04:22 CET 2012


On Fri, Mar 2, 2012 at 12:16 AM, John Salerno <johnjsal at gmail.com> wrote:
>> That's just a coincidence. Your supercall is ought to be: super().move()
>> In contrast, super().move(self) calls the superclass instance method
>> `move` with 2 arguments, both `self`, which just happens to work given
>> your move() method, inside which `cls` isn't actually a class like it
>> ought to be.
>
> Thank you! This is the whole reason I tried using a class method in the first place. I was getting an error that said my move method only takes one argument, but I was passing in two.
>
> But if I make the super call as super().move(), how does that work? The move method in the superclass takes an argument, and if I just do super().move(), isn't it the subclass that's getting passed to it?

The self that gets passed into the superclass.move() or the
subclass.move() is the exact same object in either case.  There is no
"up-casting" (or any casting at all, for that matter) in Python.

> How does the superclass move method know what 'self' is if it doesn't get passed to it as I did originally?

super() called without arguments is equivalent to super(<class this
method was defined in>, self) -- it collects the value of self from
the current stack frame.  So self is able to be passed in because the
super object implicitly knows what self is.

Cheers,
Ian



More information about the Python-list mailing list