Accessing parent class attribute from sub-class body
Posted
by
warwaruk
on Stack Overflow
See other posts from Stack Overflow
or by warwaruk
Published on 2012-03-18T17:41:54Z
Indexed on
2012/03/18
17:57 UTC
Read the original article
Hit count: 151
python
|python-3.x
I have a class Klass
with a class attribute my_list
. I have a subclass of it SubKlass
, in which i want to have a class attribute my_list
which is a modified version of the same attribute from parent class:
class Klass():
my_list = [1, 2, 3]
class SubKlass(Klass):
my_list = Klass.my_list + [4, 5] # this works, but i must specify parent class explicitly
#my_list = super().my_list + [4, 5] # SystemError: super(): __class__ cell not found
#my_list = my_list + [4, 5] # NameError: name 'my_list' is not defined
print(Klass.my_list)
print(SubKlass.my_list)
So, is there a way to access parent class attribute without specifying its name?
© Stack Overflow or respective owner