dynamic inheritance

D

Dan Sommers

is there any way to tell the class the base class during runtime?
<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
 
B

bruno at modulix

alf said:
is there any way to tell the class the base class during runtime?
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(basemodule.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__(self, name):
return getattr(self._delegate, 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.SomeClass

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
 
A

alf

bruno said:
*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:

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.
 
M

Michele Simionato

alf said:
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.

Python is ways cooler than C++. This is a sensible use case where you
may
want to change the base class at runtime:
.... pass
.... pass
....
.... pass
[<class '__main__.C'>,
<class '__main__.BasePlusDebugMethods'>,
<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
 
A

alf

Michele said:
alf wrote:
Python is ways cooler than C++.

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

This is a sensible use case where you may
want to change the base class at runtime:

Thx for the example.

A.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top