I'm trying to get a class attribute from the subclass. The attribute is assigned to the value on the superclass initialization. See:
class A:
_b = None
def __init__(self, a, b):
self.a = a
_b = b
class B(A):
def __init__(self, a, b):
super(B,self).__init__(a=a, b=b)
print('Try to get _b value:', self._b)
B(4,5)
On initialization of class B, super(...). init is called so the constructor of A class should be called. This results in b of being assigned to A._b. However, when I try to get self._b from class B, it seems like it doesn't initialize A because self._b is equal None.
Your A.__init__
doesn't initialize self._b
, it initializes a local variable called _b
. Since this is a local variable it disappears as soon as that function returns.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.