In Python, when defining a class it's possible to use previously defined attributes in new attributes definition without any further reference.
class Parent(object):
a_value = 'a'
another_value = 'b'
all_values = (a_value, another_value)
Is it possible to do the same in a derived class but still accessing some of these parent attributes?
I tried doing something like this:
class Child(Parent):
a_child_value = 'c'
all_values = (a_value, another_value, a_child_value)
But it seems that it doesn't take into account the Parent inheritance and gives me the following error:
NameError: name 'a_value' is not defined
So, is there any way to indicate that the a_value
and another_value
should be from the parent class instead of the current context?
In my case in particular the values are not strings but rather pre-compiled regular expressions, so I would like to avoid having to create them inside the __init__
method every time a new instance is created.