Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically choosing class to inherit from

My Python knowledge is limited, I need some help on the following situation.

Assume that I have two classes A and B, is it possible to do something like the following (conceptually) in Python:

import os if os.name == 'nt':     class newClass(A):        # class body else:    class newClass(B):        # class body 

So the problem is that I would like to create a class newClass such that it will inherit from different base classes based on platform difference, is this possible to do in Python? Thanks.

like image 900
taocp Avatar asked Jul 11 '13 17:07

taocp


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 inheritance in C++?

Dynamic inheritance should mean that you can alter the class hierarchy at runtime. This is something which should be pretty straightforward to do in a dynamic language. For instance, in Javascript, an object will have a prototype property.

Does Java support dynamic inheritance?

Short answer: no.

Are Classmethod inherited?

Yes, they can be inherited.


1 Answers

You can use a conditional expression:

class newClass(A if os.name == 'nt' else B):     ... 
like image 156
arshajii Avatar answered Sep 24 '22 08:09

arshajii