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.
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.
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.
Short answer: no.
Yes, they can be inherited.
You can use a conditional expression:
class newClass(A if os.name == 'nt' else B): ...
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