Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a way to specify a superclass at runtime?

1 view
Skip to first unread message

Chris Colbert

unread,
Oct 5, 2009, 9:22:17 AM10/5/09
to pytho...@python.org
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

MRAB

unread,
Oct 5, 2009, 9:32:17 AM10/5/09
to pytho...@python.org
Why not just:

SIMULATION = False

class SimController(object):
"do sim stuff here"

class RealController(object):
" do real stuff here"

if SIMULATION:
SuperKlass = SimController
else:
SuperKlass = RealController

class Controller(SuperKlass):
pass

Richard Brodie

unread,
Oct 5, 2009, 9:44:58 AM10/5/09
to

"Chris Colbert" <scco...@gmail.com> wrote in message
news:mailman.868.1254748...@python.org...

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


Chris Colbert

unread,
Oct 5, 2009, 9:49:31 AM10/5/09
to MRAB, pytho...@python.org
because when i import this module, the classes will already be
determined by the intitial flag setting.

i.e.
SIMULATION = False

class SimController(object):
def foo(self):
print 'bar'

class RealController(object):
def foo(self):
print 'baz'

if SIMULATION:
SuperKlass = SimController
else:
SuperKlass = RealController

class Controller(SuperKlass):
pass

In [2]: import testcontroller

In [3]: testcontroller.SIMULATION
Out[3]: False

In [4]: c = testcontroller.Controller()

In [5]: c.foo()
baz

In [6]: testcontroller.SIMULATION = True

In [7]: c = testcontroller.Controller()

In [8]: c.foo()
baz

On Mon, Oct 5, 2009 at 3:32 PM, MRAB <pyt...@mrabarnett.plus.com> wrote:
> Chris Colbert wrote:
>>
>> 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.
>>

> --
> http://mail.python.org/mailman/listinfo/python-list
>

Chris Colbert

unread,
Oct 5, 2009, 9:49:42 AM10/5/09
to Richard Brodie, pytho...@python.org
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...

> --
> http://mail.python.org/mailman/listinfo/python-list
>

Chris Colbert

unread,
Oct 5, 2009, 9:51:59 AM10/5/09
to Richard Brodie, pytho...@python.org
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 <scco...@gmail.com> 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 <R.Br...@rl.ac.uk> wrote:
>>

>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>

MRAB

unread,
Oct 5, 2009, 10:18:35 AM10/5/09
to pytho...@python.org
[snip]
You could put the Controller class inside a factory function:

def Controller():


if SIMULATION:
SuperKlass = SimController
else:
SuperKlass = RealController
class Controller(SuperKlass):
pass

return Controller()

Chris Colbert

unread,
Oct 5, 2009, 11:33:01 AM10/5/09
to MRAB, pytho...@python.org
that's a good idea.

Thanks!

> --
> http://mail.python.org/mailman/listinfo/python-list
>

Jean-Michel Pichavant

unread,
Oct 5, 2009, 12:11:15 PM10/5/09
to Chris Colbert, pytho...@python.org, Richard Brodie
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 <R.Br...@rl.ac.uk> wrote:
>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
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

Mick Krippendorf

unread,
Oct 6, 2009, 5:26:18 AM10/6/09
to
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.

Rhodri James

unread,
Oct 6, 2009, 8:23:01 PM10/6/09
to Chris Colbert, Richard Brodie, pytho...@python.org
On Mon, 05 Oct 2009 14:49:42 +0100, Chris Colbert <scco...@gmail.com>
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...

I almost hate to point it out, but that command line argument is


"logic outside of the controller class to determine which controller

to instantiate." It's also a darn sight easier to code.

--
Rhodri James *-* Wildebeest Herder to the Masses

0 new messages