Search code examples
pythoninheritancesuperclass

class definition dependence on runtime


in my webapp I made two different sessionhandler classes inheriting from a class called SessionHandler

Now I'd like to initiate the appropriate handler (dependent on a cookie value.)

Background: My SessionHandler should be the base class of the Controller as it needs to call a Controller backend method otherwise i would assign the handler object to a ctrl member

Is there a way to set the superclass at runtime?

Or other way to solve that? Hope you got what i meant!


Solution

  • I am not sure I entirely understood your question, but are you sure you need to use inheritance? Couldn't the class you need just be a member instead?

    Changing the superclass at runtime does not sound like a very good design, if at all possible.

    Just to be sure I got you, what you want to do is have Controller inherit from one of the SessionHandlers, but select which one at runtime? I would rather make Controller have a member variable of type SessionHandler.