Is there a way to specify a superclass at runtime?

C

Chris Colbert

I have an application that needs to run different depending on whether
the input data is being simulated, or provided from instrumentation.

I am trying to abstract this machinery in a single class called
Controller which I want to inherit from either SimController or
RealController based on whether a module level flag SIMULATION is set
to True or False.

so I have something like this:


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.

I thought this might be possible with metaclasses, but I didnt find
anything useful in the docs or on google.

Thanks for any help!

Cheers,

Chris
 
R

Richard Brodie

I am trying to abstract this machinery in a single class called
Controller which I want to inherit from either SimController or
RealController based on whether a module level flag SIMULATION is set
to True or False.

At first sight, that seems kind of odd. Wouldn't it be simpler to have
SimController and RealController inherit from Controller?
 
C

Chris Colbert

I dont think so, because that would require logic outside of the
controller class to determine which controller to instantiate.

My whole purpose for Controller is to encapsulate this logic.

So, if the data should be simulated, then i just need to pass
-simulate True as a command line argument, and the controller takes
care of it...
 
C

Chris Colbert

I suppose i can just move the SIMULATION flag to another module, and
then import it and check it before intstantiation.

So, the arg parser will have to set the flag before any other
processing begins...

I dont think so, because that would require logic outside of the
controller class to determine which controller to instantiate.

My whole purpose for Controller is to encapsulate this logic.

So, if the data should be simulated, then i just need to pass
-simulate True as a command line argument, and the controller takes
care of it...
 
J

Jean-Michel Pichavant

Chris said:
I dont think so, because that would require logic outside of the
controller class to determine which controller to instantiate.

My whole purpose for Controller is to encapsulate this logic.

So, if the data should be simulated, then i just need to pass
-simulate True as a command line argument, and the controller takes
care of it...
Please don't top post.

Yet Richard's design is the way to go.

controller.py:

class Controller:
def getInstance(simulation):
if simulation:
return SimController()
else:
return RealController()
getInstance = staticmethod(getInstance)

class RealController(Controller):
pass

class SimController(Controller):
pass

myController = Controller.getInstance(simulation=True)


I personnally would define getInstance as a module function, but because
you prefer to put all the logic in Controller... It doesn't really
matter in the end.

Cheers,

Jean-Michel
 
M

Mick Krippendorf

Chris said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top