Why does super() require the class as the first argument?

Chris Cioffi evenprimes at gmail.com
Thu Feb 3 15:24:59 EST 2005


At runtime there is nothing to say that the class hasn't been
subclassed again.

example:

class A(someclass):
    def __init__(self):
        super(self).__init__()
        do_something()

class B(A):
    """No __init__"""
    def blah(self):
        pass

When you use class B, how does the super we inheirited from A know
where to call?  Without the class name in super you end up with
infinite recursion.  (Which can be kinda fun to watch as it
crashes...)

Chris

On Thu, 03 Feb 2005 12:05:27 -0800 (PST), Kevin Smith
<Kevin.Smith at sas.com> wrote:
> I like the idea of the super() function, but it doesn't seem to solve
> the problem that I'm trying to fix.  I don't like hard-coding in calls
> to super classes using their names:
> 
> class A(object):
>    def go(self):
>        ...
> 
> class B(A):
>    def go(self):
>        ...
>        A.go(self)
> 
> I don't like this because if I ever change the name of 'A', I have to go
> through all of the methods and change the names there too.  super() has
> the same problem, but I'm not sure why.  It seems like I should be able
> to do:
> 
> class B(A):
>    def go(self):
>        ...
>        super(self).go()
> 
> I can create a super() that does this as follows:
> 
> _super = super
> def super(obj, cls=None):
>    if cls is None:
>        return _super(type(obj), obj)
>    return super(cls, obj)
> 
> I guess I'm just not sure why it wasn't done that way in the first place.
> 
> --
> Kevin Smith
> Kevin.Smith at sas.com
> --
> http://mail.python.org/mailman/listinfo/python-list
> 


-- 
"It is our responsibilities, not ourselves, that we should take
seriously." -- Peter Ustinov



More information about the Python-list mailing list