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