开发者

Python Attributes and Inheritance

Say I have the folowing code:

class Class1(object):

    def __init__(self):
        self.my_attr = 1
        self.my_other_attr = 2

class Class2(Class1):

    def __init__(self):
        super(Class1,self).__init__()

Why does Class2 not inherit the attributes开发者_开发知识库 of Class1?


You used super wrong, change it to

super(Class2, self).__init__()

Basically you tell super to look above the given class, so if you give Class1 then that __init__ method is never called.


Because you're giving super the wrong class. It should be:

class Class2(Class1):

    def __init__(self):
        super(Class2,self).__init__()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜