Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know all the derived classes of a parent?

Tags:

Suppose you have a base class A, and this class is reimplemented by B and C. Suppose also there's a class method A.derived() that tells you which classes are reimplementing A, hence returns [B, C], and if you later on have class D(A): pass or class D(B): pass, now A.derived() returns [B,C,D].

How would you implement the method A.derived() ? I have the feeling that is not possible unless you use metaclasses. You can traverse the inheritance tree only from child to parent with the standard mechanism. To have the link in the other direction, you have to keep it "by hand", and this means overriding the traditional class declaration mechanics.

like image 298
Stefano Borini Avatar asked Feb 08 '10 06:02

Stefano Borini


People also ask

How do you find the parent class of a function?

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. It is basically useful where we have to call superclass functions.

What are the derived classes?

What is a Derived Class? A derived class is a class that is constructed from a base class or an existing class. It has a tendency to acquire all the methods and properties of a base class. It is also known as a subclass or child class.

Does a derived class inherit all methods of its parent base class?

Data Abstraction and Object Orientation Here queue is said to be a derived class (also called a child class or subclass); list is said to be a base class (also called a parent class or superclass). The derived class automatically has all the fields and methods of the base class.

What is the parent class of all classes in Python?

Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.


1 Answers

If you define your classes as a new-style class (subclass of object) then this is possible since the subclasses are saved in __subclasses__.

class A(object):
 def hello(self):
  print "Hello A"

class B(A):
 def hello(self):
   print "Hello B"

>>> for cls in A.__subclasses__():
...  print cls.__name__
...
B

I do not know exactly when this was introduced or if there are any special considerations. It does however work fine to declare a subclass in a function:

>>> def f(x):
...  class C(A):
...   def hello(self):
...    print "Hello C"
...  c = C()
...  c.hello()
...  print x
...  for cls in A.__subclasses__():
...   print cls.__name__
...
>>> f(4)
Hello C
4
B
C

However, you need to note that until the class definitions have been run the interpreter does not know about them. In the example above C is not recognized as a subclass of A until the function f is executed. But this is the same for python classes every time anyway, as I presume you are already aware of.

like image 58
Hannes Ovrén Avatar answered Oct 03 '22 23:10

Hannes Ovrén