473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

dynamic inheritance

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

a.
Jun 9 '06 #1
6 2344
On Thu, 08 Jun 2006 21:14:48 -0400,
alf <ask@me> wrote:
is there any way to tell the class the base class during runtime?

class A(object):
pass
class B(object):
pass
o = A()
o.__class__ <class '__main__.A'> o.__class__ = B
o.__class__

<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.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
Jun 9 '06 #2

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

a.


Example:
class A(object):pass
class B(A):pass
B.mro()

[<class '__main__.B'>, <class '__main__.A'>, <type 'object'>]

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

http://www.python.org/download/releases/2.3/mro/

Regards,
Kay

Jun 9 '06 #3
alf wrote:
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
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 9 '06 #4
alf
bruno at modulix wrote:

*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.
Jun 21 '06 #5
alf wrote:
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:
class Base(object): .... pass
class BasePlusDebugMethods(Base): .... pass
....
class C(Base): .... pass
C.__bases__ = (BasePlusDebugMethods,) C.mro()

[<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

Jun 21 '06 #6
alf
Michele Simionato wrote:
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.
Jun 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PengYu.UT | last post by:
Hi, Some dynamic polymorphism programs can be converted to the equavalent static polymorphism programs. I'm wondering if there are any generall procedures that I can use to do this conversion. ...
1
by: puzzlecracker | last post by:
I am specifically referring to bridge pattern illustrated in GOF. Thanks
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
5
by: Derek | last post by:
I have an advanced question that I hope there is a possible answer for... Say that I have a list of base objects and I wish to allow them to be passed to various routines which may wish to...
2
by: Marvin | last post by:
Hi, It's been claimed that inheritance structures are less important in dynamic languages like Python. Why is that and where can i read more about that? /Marv
3
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
11
by: axel22 | last post by:
Please observe this simple model of multiple inheritance: void main() { class A { public: virtual void print() { cout << "A" << endl; }; class Support1 : virtual public A {
16
by: devicerandom | last post by:
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. ...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
3
by: c.ginestet | last post by:
Hello, I want to create classes that I could aggregate with each others using operators. This would therefore take the form of a dynamic aggregation. For instance, (This code does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.