[Python] Accessing parent class attribute from sub-class body

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    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):
    # this works, but I must specify parent class explicitly
    my_list = Klass.my_list + [4, 5]

    # SystemError: super(): __class__ cell not found
    #my_list = super().my_list + [4, 5]

    # NameError: name 'my_list' is not defined
    #my_list = my_list + [4, 5]


    print(Klass.my_list)
    print(SubKlass.my_list)


    So, is there a way to access parent class attribute without specifying its name?


    There is a bug on Python issue tracker: http://bugs.python.org/issue11339. Let's hope it will be solved at some point.

    Continue reading...

Compartilhe esta Página