taocp taocp - 1 year ago 76
Python Question

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

, is it possible to do something like the following (conceptually) in Python:

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

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

Answer Source

You can use a conditional expression:

class newClass(A if os.name == 'nt' else B):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download