473,406 Members | 2,816 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,406 software developers and data experts.

runtime inheritance (e.g. __new__, __metaclass__)

I try to work out how to use __new__ and metaclass (or __metaclass__)
mechanisms in order to change the base class at runtime. I haven't been
successful so far, however, even after also reading most of the relevant
documentation.

The most simple representation (an oversimplification indeed) of what
I'm trying to accomplish is this:

class Base1:
pass

class Base2:
pass

class Meta:
pass

class Good(Meta):
pass

What I want to achieve at runtime is this:

g1 = Good(Base1) # a Good object with base class Base1
g2 = Good(Base2) # a Good object with base class Base2

Of course, this oversimplified (pseudo-code) example will not work but
illustrates most clearly my idea. How can I come closest to realizing
this by using the above mechanisms?
Thanks

--Henk
--
------------------------------------------------------------------------------
The disclaimer that applies to e-mail from
TNO Physics and Electronics Laboratory
can be found on: http://www.tno.nl/disclaimer/email.html
------------------------------------------------------------------------------
Jul 18 '05 #1
3 2024
H Jansen wrote:
I try to work out how to use __new__ and metaclass (or __metaclass__)
mechanisms in order to change the base class at runtime. I haven't been
successful so far, however, even after also reading most of the relevant
documentation.

The most simple representation (an oversimplification indeed) of what
I'm trying to accomplish is this:

class Base1:
pass

class Base2:
pass

class Meta:
pass

class Good(Meta):
pass

What I want to achieve at runtime is this:

g1 = Good(Base1) # a Good object with base class Base1
g2 = Good(Base2) # a Good object with base class Base2

Of course, this oversimplified (pseudo-code) example will not work but
illustrates most clearly my idea. How can I come closest to realizing
this by using the above mechanisms?

class Base1: pass .... class Base2: pass .... def Good(base): .... class Derived(base):
.... pass
.... return Derived()
.... g1 = Good(Base1)
g2 = Good(Base2)
isinstance(g1, Base1) True isinstance(g1, Base2) False isinstance(g2, Base1) False isinstance(g2, Base2)

True

:-)

Seriously, either too much information was lost in the simplification
process or you want to have instances of the same class with different
bases - which doesn't seem possible to me as the __bases__ attribute is in
the class, not the instance. On the other hand, changing base classes at
runtime is as simple as assigning __bases__.
__new__() can serve as a factory that returns objects of an arbitrary type,
but I don't consider that good style. Metaclasses - no idea where they
could enter the picture.
Peter

Jul 18 '05 #2
"H Jansen" <h.******@fel.tno.nl> wrote in message news:<ma*************************************@pyth on.org>...
What I want to achieve at runtime is this:

g1 = Good(Base1) # a Good object with base class Base1
g2 = Good(Base2) # a Good object with base class Base2


Peter's solution is clean (and the way I'd go) as long as you don't
mind Good being a function, not a class. If you need Good to be a
class, you could probably get what you want by changing the __bases__
attribute:
class Base1: .... def __init__(self):
.... print "Base1"
.... class Base2: .... def __init__(self):
.... print "Base2"
.... class Good: .... pass
.... Good.__bases__ = (Base1, )
Good() Base1
<__main__.Good instance at 0x00E6D738> Good.__bases__ = (Base2, )
Good() Base2
<__main__.Good instance at 0x00E6DC38>

Note that once you've changed the base classes of the Good class,
you'll have to change them back if you want the original behavior
again.

Or if you actually need different Good classes, you could try
something like:
class Base1(object): .... def __init__(self):
.... print "Base1"
.... class Base2(object): .... def __init__(self):
.... print "Base2"
.... class Good(object): .... pass
.... Good1 = type.__new__(type, "Good1", (Base1,), dict(Good.__dict__))
Good1() Base1
<__main__.Good1 object at 0x00E664F0> Good2 = type.__new__(type, "Good2", (Base2,), dict(Good.__dict__))
Good2() Base2
<__main__.Good2 object at 0x00E66450>

Note that type.__new__ requires a dict (not a dictproxy like the
__dict__ attribute), so I've copied the Good.__dict__ attribute
instead of having both classes reference the same one. This means
that if you have a class variable in Good (and therefore in Good1 and
Good2), if you modify it in Good1, it will not be modified in Good2,
e.g.:
Good1 = type.__new__(type, "Good1", (Base1,), dict(Good.__dict__))
Good2 = type.__new__(type, "Good2", (Base2,), dict(Good.__dict__))
Good1.x = 2
Good1.x 2 Good2.x 1

This may or may not be desirable.

This technique does seem to transfer any methods successfully:
class Good(object): .... def f(self, x):
.... print (self.__class__, x)
.... Good1 = type.__new__(type, "Good1", (Base1,), dict(Good.__dict__))
Good2 = type.__new__(type, "Good2", (Base2,), dict(Good.__dict__))
Good1().f(1) Base1
(<class '__main__.Good1'>, 1) Good2().f(2)

Base2
(<class '__main__.Good2'>, 2)

Steve
Jul 18 '05 #4

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

Similar topics

0
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing...
1
by: Antoine Pitrou | last post by:
Hi, I've been looking at writing parameterized metaclasses and here are the two solutions I've come to: (my goal was to build a way to automatically add a hash function that would take into...
11
by: wijhierbeneden | last post by:
Hello Where can I find some info about the inner working of new/delete?? Is this compiler specific or defined by the standard?? There is a lot of info about the different malloc implementations...
1
by: Casper | last post by:
I am issuing the following commands in a form's detail on click event. Private Sub Detail_Click() Dim Nline As Line Set Nline = New Line I get :"ActiveX component can't create object" ...
3
by: | ov | last post by:
Hi, Need some help here. I have a .NET class library in C++ (with CLR). In this project I have a class with the __nogc keyword. When I use the new operator within this class's code I receive:...
1
by: Luc | last post by:
Hi, I have a TabControl and, at runtime, I need to add some tabpages. The problem is that each tabpage is similar to the others and contains several controls. If I do...
4
by: news.microsoft.com | last post by:
Hello, I've got a design problem that I can't figure out. I need to override a static method which happens to be referenced by methods inside nested classes. In the sample below, in the call...
0
by: dakvanslam | last post by:
I am new to VB .NET and SQL Server, so I apologize if this is a basic question. I am using Visual Studio 2005 with SQL Server Express. Below is the code in question: ChosenProjectName =...
3
by: =?Utf-8?B?R3JhaGFt?= | last post by:
I've added 2 tracking services to the wf runtime; one is the standard SqlTrackingService: trackingService = new SqlTrackingService(<trackingConnectionString>); <workflow...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.