I have the following class
class Foo():
data = "abc"
And i subclass it
class Bar(Foo):
data +="def"
I am trying to edit a parent class attribute in subclass. I want my parent class to have some string, and my subclass should add some extra data to that string. How it should be done in Python? Am i wrong by design?
You ask two questions:
How it should be done in Python?
class Bar(Foo):
data = Foo.data + "def"
Am i wrong by design?
I generally don't use class variables in Python. A more typical paradigm is to initialize an instance variable:
>>> class Foo(object):
... data = "abc"
...
>>> class Bar(Foo):
... def __init__(self):
... super(Bar, self).__init__()
... self.data += "def"
...
>>> b = Bar()
>>> b.data
'abcdef'
>>>