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 -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -