Can't access parent member variable in Python

前端 未结 2 543
一向
一向 2021-02-03 21:58

I\'m trying to access a parent member variable from an extended class. But running the following code...

class Mother(object):
    def __init__(self):
        se         


        
相关标签:
2条回答
  • 2021-02-03 22:28

    You want the instance attribute, not the class attribute, so you should use self._haircolor.

    Also, you really should use super in the __init__ in case you decide to change your inheritance to Father or something.

    class Child(Mother):
        def __init__(self): 
            super(Child, self).__init__()
        def print_haircolor(self):
            print self._haircolor
    
    0 讨论(0)
  • 2021-02-03 22:50

    You're mixing up class and instance attributes.

    print self._haircolor
    
    0 讨论(0)
提交回复
热议问题