Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic inheritance in Python

Say I have 2 different implementations of a class

class ParentA:     def initialize(self):         pass      def some_event(self):         pass      def order(self, value):         # handle order in some way for Parent A   class ParentB:     def initialize(self):         pass      def some_event(self):         pass      def order(self, value):         # handle order in another for Parent B 

How can I dynamically let some 3rd class inherit from either ParentA or ParentB based on something like this?

class MyCode:     def initialize(self):         self.initial_value = 1      def some_event(self):         # handle event         order(self.initial_value)   # let MyCode inherit from ParentA and run run(my_code, ParentA) 
like image 471
Morten Avatar asked Jan 11 '14 08:01

Morten


People also ask

What is dynamic inheritance?

Dynamic inheritance helps you to override functions at runtime. This is useful when there are lots of different implementations possible for each of the interface functions. This avoids creation of large number of class definitions with all the possible combination of interface functions.

What is dynamic method in Python?

Python Code can be dynamically imported and classes can be dynamically created at run-time. Classes can be dynamically created using the type() function in Python. The type() function is used to return the type of the object. The above syntax returns the type of object.

What is inheritance in Python with example?

Inheritance relationship defines the classes that inherit from other classes as derived, subclass, or sub-type classes. Base class remains to be the source from which a subclass inherits. For example, you have a Base class of “Animal,” and a “Lion” is a Derived class. The inheritance will be Lion is an Animal.

What is inheritance concept in Python?

Inheritance in Python Inheritance is a powerful feature in object oriented programming. It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class.


Video Answer


2 Answers

Simply store the class-object in a variable (in the example below, it is named base), and use the variable in the base-class-spec of your class statement.

def get_my_code(base):      class MyCode(base):         def initialize(self):           ...      return MyCode  my_code = get_my_code(ParentA) 
like image 135
shx2 Avatar answered Sep 20 '22 18:09

shx2


Also, you can use type builtin. As callable, it takes arguments: name, bases, dct (in its simplest form).

def initialize(self):     self.initial_value = 1  def some_event(self):     # handle event     order(self.initial_value)  subclass_body_dict = {     "initialize": initialize,     "some_event": some_event }  base_class = ParentA # or ParentB, as you wish  MyCode = type("MyCode", (base_class, ), subclass_body_dict) 

This is more explicit than snx2 solution, but still - I like his way better.

PS. of course, you dont have to store base_class, nor subclass_body_dict, you can build those values in type() call like:

MyCode = type("MyCode", (ParentA, ), {         "initialize": initialize,         "some_event": some_event     }) 
like image 42
Filip Malczak Avatar answered Sep 21 '22 18:09

Filip Malczak