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.
Accessing Parent Class Functions This is really simple, you just have to call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class. # how parent constructors are called.
Using Classname: Parent's class methods can be called by using the Parent classname. method inside the overridden method. Using Super(): Python super() function provides us the facility to refer to the parent class explicitly.
The child class inherits the attributes and functions of its parent class. If we have several similar classes, we can define the common functionalities of them in one class and define child classes of this parent class and implement specific functionalities there.
However, inside the class (myClass) we can access the private variables. In the hello() method, the __privateVar variable can be accessed (as shown above: “Private Variable value: 27”). So from the above example, we can understand that all the variables and the methods inside the class are public by the method.
You can't.
A class definition works in Python works as follows.
The interpreter sees a class
statement followed by a block of code.
It creates a new namespace and executes that code in the namespace.
It calls the type
builtin with the resulting namespace, the class name, the base classes, and the metaclass (if applicable).
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 x
s, 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?
As answered by @katrielalex, my_list
is not in the namespace by default before the class has been created. What you could do however in Python 3, if you want to dive into metaclasses, is to add my_list
to the namespace manually:
class Meta(type): def __prepare__(name, bases, **kwds): print("preparing {}".format(name), kwds) my_list = [] for b in bases: if hasattr(b, 'my_list'): my_list.extend(b.my_list) return {'my_list': my_list} class A(metaclass=Meta): my_list = ["a"] class B(A): my_list.append(2) print(A().my_list) print(B().my_list)
Note that this example probably does not yet handle diamond-shaped inheritance structures well.
The new __prepare__
attribute is defined in PEP 3115.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With