Get attribute of parent of parent of a multilevel inherited class
Let's imagine the following classes structure:
class A: def t(self): print("a") class B(A): def t(self): super().t() print("b")
I want, from a class
C, that inherits from
B, to run only
A.t() method using
super keyword, so that running
will print only
For information, let's suppose that I do want
C to act like
B, excepted for the method
t(), which will have to completely bypass
B.t() to override
class C(B): def t(self): super().super().t()
AttributeError: 'super' object has no attribute 'super'.
I also tried
class C(B): def t(self): A.t()
TypeError: t() missing 1 required positional argument: 'self'.
Obviously, I am doing something wrong. Can someone show me how to overide to the method of the second parent, while still inheriting from the first parent?
As I mentioned in the comments, you can use
A.t(self) to achieve your goal.
But .. it seems like it's not really necessary to do this.
class A: def real_t(self): print("a") def t(self): self.real_t() class B(A): def t(self): self.real_t() print("b") class C(B): def t(self): self.real_t() print("c")
Intro to Multiple Inheritance & super(), Create a child class that inherits from Parent class Child(Parent): def __init__(self ): We see that the Child class 'inherited' attributes and methods from the Parent class. Before we get into multiple inheritance and super . Java Inheritance (Subclass and Superclass) In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class; superclass (parent) - the class being inherited from; To inherit from a class, use the extends keyword.
You can redesign your class hierarchy such that
C both inherit from a common subclass of
A, which implements every method that your current
B class implements except the method
t, which is left to the new
B class to implement on its own:
class A: def t(self): print("a") class SubA(A): def other_method(self): pass class B(SubA): def t(self): super().t() print("b") class C(SubA): pass
Python - Access Parent Class Attribute, But have you ever wondered how to access the parent's class methods? the object of a child class can access the methods and attributes of the parent class. Multilevel Inner Class: In multilevel inner classes, the inner class contains Attributes in Python � How to Get a List of Class Attributes in Python? Output: Geeks for Geeks Calling Parent class Method. To understand about the concept of parent class, you have to know about Inheritance in Python.In simpler terms, inheritance is the concept by which one class (commonly known as child class or sub class) inherits the properties from another class (commonly known as Parent class or super class).
This is atrocious and you should never do it, but it is possible.
super() is short for
super(__class__, self_or_whatever_the_first_argument_is), but it's legal to pass any class in the MRO as the first argument.
The below code will work even if globals are replaced (e.g. by
imp.reload), but not if something else inherits from
B and gets between C and B in the MRO. If you hard-code B in place of
next_class, you will instead skip such classes and it will break if globals are replaced.
#!/usr/bin/env python3 class A: def t(self): print("a") class B(A): def t(self): super().t() print("b") class C(B): def t(self): s = super() # s.__self_class__ == self.__class__ == type(self) == F mro = s.__self_class__.mro() # unless globals are replaced, s.__thisclass__ == __class__ == C thisclass_index = mro.index(__class__) next_class = mro[thisclass_index+1] # unless globals are replaced or something else inherits from B, next_class == B super(next_class, self).t() print('c') class D(A): def t(self): super().t() print('d') class E(A): def t(self): super().t() print('e') class F(D, C, E): def t(self): super().t() print('f') def main(): assert F.mro() == [F, D, C, B, E, A, object] print('This should print the letters `aecdf` in order:') F().t() if __name__ == '__main__': main()
Python super() can't call parent's inherited method, I'm afraid you have built up the wrong mental model on how Python instances and classes relate. Classes only provide a series of attributes for instances to� Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.
Class inheritance, The syntax to extend another class is: class Child extends Parent . If we call super.method() then, the engine needs to get the method from� Inherited attributes are not returned. If you set the inherit flag to true then you'll get the explicit and inherited attributes. Now for your problem. You are confusing inheritance with overloading and this is compounded by the inheritance of attributes. It is truly a confusing issue so I'll try to clarify it. Here are my sample classes.
Python Multiple Inheritance Explained with Examples, However, Multiple Inheritance is a feature where a class can derive attributes and In multilevel inheritance, properties of the parent and the child classes are to demonstrate how to get MRO using __mro__ and mro() """ class Appliance: def� Accessing Parent class method from inner class. An inner class or nested class is a defined inside the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have one or more than one inner classes. Types of Inner Classes: Multiple Inner Class Multilevel Inner Class
A Child class can inherit only one Parent class. (A child can have only one parent) Multiple (sub) classes can inherit a same (super) class. (A parent can have multiple children) Child class may use the methods and variables of the Parent class. A child class can inherit all methods of parent class except those which are priavte.
- If you want C to not act like B, then maybe inheritance is the wrong mechanism for this. If you explain what these classes are and why you don't want B, it might be easier to help.
- I want C to act like B, except for C.t(), which completely overrides B.t()
- You can use
A.t(self), but I don't recommend it.
- There's the method, which is actually f*ck idea of inheritance. You can call
- This is a bad idea, because
Cis reaching past the interface that
Bpresents to subclasses.
A.tis not part of
B's subclass interface. Subclasses are not supposed to have access to it. If subclasses are supposed to have access to it,
Bshould do something to expose access.
- This is fragile if there are other classes in the hierarchy.
- @o11c, this answer is based on comments under question, where author described what he wanted to achieve. As I understood, this should fit as a simple solution for this particular "issue". Of course this can't be used as universal recommendation or recommendation at all. It's just an option, which I find better than call method from "superbase" class by it's name.
- I marked it as answer because it is the answer to my question. However, I feel like I should promote the more usual ways of solving problems of class hierarchy. I too, believe that
A.t(self)is a terrible way of fixing bad code design, but it really works in the way that it indeed reaches the parent of the parent of the class. I highly recomend other users to fix their class hierarchy if they can before attempting this.
- For anyone who thinks they ought to "fix"
__class__is correct. It's a real thing.
self.__class__is a different thing, and it's the wrong thing if further subclasses are created.