Does anyone know if it is even possible to change the inherited variables of the parent class by changing a variable in a child class?
Without declaring them in the __init__
child?
class Foo(object):
def __init__(self):
self.data = "abc"
self.data_2 = self.data
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
self.data = "def"
f = Foo()
b = Bar()
print(f.data_2) # --> abc
print(b.data_2) # --> abc
But I expect to get:
print(f.data_2) # --> abc
print(b.data_2) # --> def
CodePudding user response:
this solves the issue if you don't want to accept data
as an argument to your init func as someone pointed out above
class Foo(object):
def __init__(self):
self.data = "abc"
@property
def data_2(self):
return self.data
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
self.data = "def"
f = Foo()
b = Bar()
print(f.data_2)
print(b.data_2)