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.
I have found my own way of dynamically inheriting classes when you have more than two options to chose from. I'm sure that others have used this before me, however I couldn't find anything like this online, therefore I thought that I should share it with you.
This code can be altered and you can do quite allot with it. This is my favorite way of setting up dynamic class inheritance, even though it's a bit long, as you can have as many options as you want and even could have multiple dynamically chosen inheritances for each object.
You can use a conditional expression:
Yep, you can do exactly what you wrote. Though personally, I'd probably do it this way for cleanliness:
This assumes that you need to actually do different things implementation-wise within the class body. If you only need to change the inheritance, you can just embed an
if
statement right there:Coming from the worlds of Java and C#, the thought of doing something like this makes me cringe =P. (Not that the idea of conditionally inheriting a class is bad - I'm just not used to it.)
Thinking about it for a bit, I would have done something like this if this question were about Java. I'm posting the pattern - implement an interface, then use a factory to select between the implementations - in order to provide another perspective to the problem:
Definitely a lot more code, but it's an appropriate solution in a strongly typed environment.
EDIT: One significant difference I noticed between my answer and the original question:
If you conditionally inherit from multiple different classes, then the superclasses contain code that depend on which OS you're using, while the class that inherits from them contains code that is the same for all OS's. The top of the inheritance chain is OS-dependent; the bottom isn't.
My approach goes the other way. The
OsDepndent
interface (or superclass) defines methods that are similar for all platforms, while the different implementations (or subclasses) have OS-dependent code. The top of the inheritance chain is OS-agnostic.For those who have similar needs whilst working with different modules. First, create a file
switcher.py
and put inside it the followingNext, in your
newClass.py
putFinally in your main script