Is there a way to specify a superclass at runtime?
Mick Krippendorf
mad.mick at gmx.de
Tue Oct 6 05:26:18 EDT 2009
Chris Colbert schrieb:
>
> SIMULATION = False
>
> class SimController(object):
> "do sim stuff here"
>
> class RealController(object):
> " do real stuff here"
>
> class Controller(SuperKlass):
> pass
>
>
> so if SIMULATION == False I want to be able to instance a Controller
> object that inherits from RealController and vice-versa.
How about:
SIMULATION = True
class Controller(object):
def __new__(cls):
if SIMULATION:
return SimController()
else:
return RealController()
class SimController(object):
pass
class RealController(object):
pass
print Controller()
But my preferred solution (in case SIMULATION never changes during
runtime) would be:
class SimController(object):
pass
class RealController(object):
pass
if SIMULATION:
Controller = SimController
else:
Controller = RealController
print Controller()
Regards,
Mick.
More information about the Python-list
mailing list