I\'m currently trying to implement some inheritance in my Python project and have hit a road block. I\'m trying to create a baseParentClass that will handle the basic functi
You were close. This works:
class Parent(object):
def __init__(self):
for attr in self.ATTRS:
setattr(self, attr, 0)
class Child(Parent):
#class variable
ATTRS = ['attr1', 'attr2', 'attr3']
def __init__(self):
super(Child, self).__init__()
Here, you set the ATTRS
list at the class level, and conveniently access it in self.ATTRS
at baseclass's __init__
.
Adding to the above answer,
Although ATTR is a class variable in the child class, the instance variable of the child class is able to access the class variable ATTR because, Python does the following using namespaces