I can't get super() to work in python 2.7

浪子不回头ぞ 提交于 2019-12-01 14:23:06

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:

class Dirchanger(D):
    def __init__(self,client,*args):
        if len(args) == 1:
            self.cd(args[0])
    def cd(self,directory):
        super(D, self).cd(directory)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!