oop - Python: Access base class "Class variable" in derive class -
i trying access class variable base class in derived class , getting no attributeerror
class parent(object): variable = 'foo' class child(parent): def do_something(self): local = self.variable
i tried using parent.variable
did not work either. getting same error attributeerror: 'child' object has no attribute 'child variable'
how resolve this
i'm not sure you're doing wrong. code below assumes have initialization method, however.
class parent(object): variable = 'foo' class child(parent): def __init__(self): pass def do_something(self): local = self.variable print(local) c = child() c.do_something()
output:
foo
Comments
Post a Comment