Skip to content Skip to sidebar Skip to footer

I Can't Get Super() To Work In Python 2.7

With a simple pair of classes, I cannot get super working: class A(object): q = 'foo' class B(A): q = 'bar' def __init__(self): self.a = super(A, self).q a = B(

Solution 1:

You are using the wrong search target (the first argument); use super(B, self) instead:

def__init__(self):
    self.a = super(B, self).q

The first argument gives super() a starting point; it means look through the MRO, starting at the next class past the one I gave you, where the MRO is the method resolution order of the second argument (type(self).__mro__).

By telling super() to start looking past A, you effectively told super() to start the search too far down the MRO. object is next, and that type doesn't have a q attribute:

>>> B.__mro__
(<class'__main__.B'>, <class'__main__.A'>, <type'object'>)

Your real code has the exact same issue:

classDirchanger(D):def__init__(self,client,*args):
        if len(args) == 1:
            self.cd(args[0])
    defcd(self,directory):
        super(D, self).cd(directory)

You are starting the MRO search at D, not Dirchanger here.

Post a Comment for "I Can't Get Super() To Work In Python 2.7"