Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:08:40+00:00 2026-05-31T22:08:40+00:00

I have a class Klass with a class attribute my_list . I have a

  • 0

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?

UPDATE:

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:08:41+00:00Added an answer on May 31, 2026 at 10:08 pm

    You can’t.

    A class definition works in Python works as follows.

    1. The interpreter sees a class statement followed by a block of code.

    2. It creates a new namespace and executes that code in the namespace.

    3. It calls the type builtin with the resulting namespace, the class name, the base classes, and the metaclass (if applicable).

    4. It assigns the result to the name of the class.

    While running the code inside the class definition, you don’t know what the base classes are, so you can’t get their attributes.

    What you can do is modify the class immediately after defining it.


    EDIT: here’s a little class decorator that you can use to update the attribute. The idea is that you give it a name and a function. It looks through all the base classes of your class, and gets their attributes with that name. Then it calls the function with the list of values inherited from the base class and the value you defined in the subclass. The result of this call is bound to the name.

    Code might make more sense:

    >>> def inherit_attribute(name, f):
    ...     def decorator(cls):
    ...             old_value = getattr(cls, name)
    ...             new_value = f([getattr(base, name) for base in cls.__bases__], old_value)
    ...             setattr(cls, name, new_value)
    ...             return cls
    ...     return decorator
    ... 
    >>> def update_x(base_values, my_value):
    ...    return sum(base_values + [my_value], tuple())
    ... 
    >>> class Foo: x = (1,)
    ... 
    >>> @inherit_attribute('x', update_x)
    ... class Bar(Foo): x = (2,)
    ... 
    >>> Bar.x
    (1, 2)
    

    The idea is that you define x to be (2,) in Bar. The decorator will then go and look through the subclasses of Bar, find all their xs, and call update_x with them. So it will call

    update_x([(1,)], (2,))
    

    It combines them by concatenating them, then binds that back to x again. Does that make sense?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is what I have so far: def get_concrete_name_of_class(klass): Given a class return the
i have class call example1 this class defines a background, and i want to
Let's say I have this class: class ComponentLibrary def self.register(klass); ...; end end And
I have this program, which I want to understand the following output: #include <stdio.h>
I have class A: public class ClassA<T> Class B derives from A: public class
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have class A, which exposes an event. an object of class B subscribed
I have class A: public B { ...} vector<A*> v; I want to do
Let's say I have a class named Klass , and a class called Klass2
I have class A which has ISerializable implemented for custom serialization. Now i need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.