Is there a way to specify a superclass at runtime?

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.

Advertisements

"Chris Colbert" <> wrote in message
news:...
> 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?

Advertisements

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...

On Mon, Oct 5, 2009 at 3:44 PM, Richard Brodie <> wrote:
>
> "Chris Colbert" <> wrote in message
> news:...
>
>> 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?
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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...

On Mon, Oct 5, 2009 at 3:49 PM, Chris Colbert <> wrote:
> 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...
>
> On Mon, Oct 5, 2009 at 3:44 PM, Richard Brodie <> wrote:
>>
>> "Chris Colbert" <> wrote in message
>> news:...
>>
>>> 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?
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>

Chris Colbert wrote:
> 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...
>
> On Mon, Oct 5, 2009 at 3:44 PM, Richard Brodie <> wrote:
>
>> "Chris Colbert" <> wrote in message
>> news:...
>>
>>
>>> 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?
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
Please don't top post.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!