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

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -