Is there a way to specify a superclass at runtime?

Discussion in 'Python' started by Chris Colbert, Oct 5, 2009.

  1. 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
    Chris Colbert, Oct 5, 2009
    #1
    1. Advertising

  2. "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?
    Richard Brodie, Oct 5, 2009
    #2
    1. Advertising

  3. 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, Oct 5, 2009
    #3
  4. 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, Oct 5, 2009
    #4
  5. 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.

    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
    Jean-Michel Pichavant, Oct 5, 2009
    #5
  6. 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.
    Mick Krippendorf, Oct 6, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rubikzube*
    Replies:
    4
    Views:
    850
    Gordon Beaton
    Feb 26, 2007
  2. Evan Klitzke
    Replies:
    0
    Views:
    348
    Evan Klitzke
    Aug 2, 2007
  3. aaragon
    Replies:
    15
    Views:
    508
    Daniel T.
    Dec 30, 2007
  4. Max2006
    Replies:
    2
    Views:
    542
    Steven Cheng [MSFT]
    Jul 17, 2008
  5. bart van deenen
    Replies:
    6
    Views:
    738
    bart van deenen
    Mar 3, 2009
Loading...

Share This Page