dynamic inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alf

    #1

    dynamic inheritance

    is there any way to tell the class the base class during runtime?

    a.
  • Dan Sommers

    #2
    Re: dynamic inheritance

    On Thu, 08 Jun 2006 21:14:48 -0400,
    alf <ask@me> wrote:
    [color=blue]
    > is there any way to tell the class the base class during runtime?[/color]
    [color=blue][color=green][color=darkred]
    >>> class A(object):
    >>> pass
    >>> class B(object):
    >>> pass
    >>> o = A()
    >>> o.__class__[/color][/color][/color]
    <class '__main__.A'>[color=blue][color=green][color=darkred]
    >>> o.__class__ = B
    >>> o.__class__[/color][/color][/color]
    <class '__main__.B'>

    I don't know if that's a good idea. Maybe this one:

    class A(object):
    pass
    class B(object):
    pass

    if some_function() :
    C = A
    else:
    C = B

    class D(C):
    pass

    Why do you want to change a class' base class during runtime?

    HTH,
    Dan

    --
    Dan Sommers
    <http://www.tombstoneze ro.net/dan/>
    "I wish people would die in alphabetical order." -- My wife, the genealogist

    Comment

    • Kay Schluehr

      #3
      Re: dynamic inheritance


      alf wrote:[color=blue]
      > is there any way to tell the class the base class during runtime?
      >
      > a.[/color]

      Example:
      [color=blue][color=green][color=darkred]
      >>> class A(object):pass
      >>> class B(A):pass
      >>> B.mro()[/color][/color][/color]
      [<class '__main__.B'>, <class '__main__.A'>, <type 'object'>]

      See also Micheles nice article about the semantics of the "mro" (
      method resolution order).



      Regards,
      Kay

      Comment

      • bruno at modulix

        #4
        Re: dynamic inheritance

        alf wrote:[color=blue]
        > is there any way to tell the class the base class during runtime?
        >[/color]
        Technically, yes - the solution depending on your definition of "during
        runtime"

        FWIW, the class statement is evaled at import/load time, which is
        "during runtime".... So if you want to use one or other (compatible)
        classes depending on configuration or system or like, you can use a
        conditionnal at the top level, *before* the class statement is eval'd. ie:

        import os
        if os.name == 'posix':
        import posixmodule as basemodule
        elif os.name == 'nt':
        import ntmodule as basemodule
        # etc...

        class MyClass(basemod ule.baseclass):
        # class def here


        If you want to dynamically change the base class (or one of the base
        classes) during execution (ie: after the class statement has been
        eval'd), read Kay Schluehr's answer.

        *But* you'd probably better tell us about the problem you're trying to
        solve. Since in Python, inheritance is mostly about implementation (ie:
        not needed for subtyping), your problem would probably be best solved
        with composition/delegation, for which Python offers a good support:

        class MyClass(object) :
        def __init__(self, delegate):
        self._delegate = delegate

        def __getattr__(sel f, name):
        return getattr(self._d elegate, name)

        or, if you don't want to explicitely pass the delegate at instanciation
        time:

        import os
        if os.name == 'posix':
        import posixmodule as basemodule
        elif os.name == 'nt':
        import ntmodule as basemodule
        # etc...

        class MyClass(object) :
        _delegate_class = basemodule.Some Class

        def __init__(self):
        self._delegate = self._delegate_ class()

        # etc

        there are of course some variants of the above solutions, but one can't
        tell you which one to use without knowing more about your actual problem.

        HTH
        --
        bruno desthuilliers
        python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
        p in 'onurb@xiludom. gro'.split('@')])"

        Comment

        • alf

          #5
          Re: dynamic inheritance

          bruno at modulix wrote:[color=blue]
          >
          > *But* you'd probably better tell us about the problem you're trying to
          > solve. Since in Python, inheritance is mostly about implementation (ie:
          > not needed for subtyping), your problem would probably be best solved
          > with composition/delegation, for which Python offers a good support:
          >[/color]

          I did not think about any particular problem, just thought it would be
          cool to abstract out the base class. In fact you can do that in C++ (to
          some extend) using templates and parameterizing the base class.

          regards,
          a.

          Comment

          • Michele Simionato

            #6
            Re: dynamic inheritance

            alf wrote:[color=blue]
            > I did not think about any particular problem, just thought it would be
            > cool to abstract out the base class. In fact you can do that in C++ (to
            > some extend) using templates and parameterizing the base class.[/color]

            Python is ways cooler than C++. This is a sensible use case where you
            may
            want to change the base class at runtime:
            [color=blue][color=green][color=darkred]
            >>> class Base(object):[/color][/color][/color]
            .... pass
            [color=blue][color=green][color=darkred]
            >>> class BasePlusDebugMe thods(Base):[/color][/color][/color]
            .... pass
            ....
            [color=blue][color=green][color=darkred]
            >>> class C(Base):[/color][/color][/color]
            .... pass
            [color=blue][color=green][color=darkred]
            >>> C.__bases__ = (BasePlusDebugM ethods,)[/color][/color][/color]
            [color=blue][color=green][color=darkred]
            >>> C.mro()[/color][/color][/color]
            [<class '__main__.C'>,
            <class '__main__.BaseP lusDebugMethods '>,
            <class '__main__.Base' >,
            <type 'object'>]

            (i.e. in a running program with a problem you can add debug methods and
            possibily
            even fix the problem without restarting the program).

            Michele Simionato

            Comment

            • alf

              #7
              Re: dynamic inheritance

              Michele Simionato wrote:[color=blue]
              > alf wrote:
              > Python is ways cooler than C++.[/color]

              I switched to Python from C++ over year ago and do not see a way back.
              C++ just sucks at each corner.

              [color=blue]
              > This is a sensible use case where you may
              > want to change the base class at runtime:[/color]

              Thx for the example.

              A.

              Comment

              Working...