Python – Dynamically Choosing Class to Inherit From

classinheritancepython

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.

Best Answer

You can use a conditional expression:

class newClass(A if os.name == 'nt' else B):
    ...
Related Question