473,395 Members | 1,442 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,395 software developers and data experts.

Problem with dynamic creation of classes.

I have a base class B and a derived class D which inherits from B. I also
have a D2 class which inherits from D. D is used as a base class for most
of my generated classes. I have a "special" class which inherits from D2
because I need to override a couple of its methods. Anything based on D2
will inherit Encode and Decode from B.

class D(b.B):
def __init__(self, data, timestamp = None):
b.B.__init__(self, data)
self.timestamp = timestamp

class D2( D ):
def __init__(self, data, timestamp = None):
DS.__init__(self, data, timestamp)
def Decode(self):
pass
def Encode(self):
pass

The generated classes come from this loop:

for m in mdefs:
mdef = mdefs[m]
name = mdef['name'] + fbase
if mdef.has_key('baseclass'):
base_class_seq = mdef['baseclass']
else:
base_class_seq = DEFAULT_BASE_CLASS
nclass = new.classobj( name, base_class_seq, globals() )

base_class_seq is either going to be (D,) or (D2,)

After I get through the class generation, I want to say
globals()['IFRAMED2'].Decode = dynDecode
globals()['IFRAMED2'].Encode = dynEncode
for the one class that inherited from D2. If I do that, I find that *all*
classes that call Encode or Decode are all calling dyn{En,De}code (which
is exactly what I *don't* want).

So I looked at the id values after class generation.

print 'IFRAMED2.Encode = ', IFRAMED2.Encode, id(IFRAMED2.Encode)
print 'SNRMD.Encode = ', SNRMD.Encode, id(SNRMD.Encode)
print 'IFRAMED2 = ', IFRAMED2, id(IFRAMED2)
print 'SNRMD = ', SNRMD, id(SNRMD)
IFRAMED2.Decode = dynDecode
IFRAMED2.Encode = dynEncode
print 'IFRAMED2.Encode = ', IFRAMED2.Encode, id(IFRAMED2.Encode)
print 'SNRMD.Encode = ', SNRMD.Encode, id(SNRMD.Encode)

Here's the output:

IFRAMED2.Encode = <unbound method IFRAMED2.Encode1100623660
SNRMD.Encode = <unbound method SNRMD.Encode1100623660
IFRAMED2 = d.IFRAMED2 1076299644
SNRMD = d.SNRMD 1103202364
IFRAMED2.Encode = <unbound method IFRAMED2.dynEncode1100623660
SNRMD.Encode = <unbound method SNRMD.dynEncode1100623660

So it looks like the IFRAMED2 class which inherits from D2 is starting out
with the same id value for Encode as SNRMD which inherits from D, even
though D2 defines its own Encode method.

Is it me of is it the interpreter doing something wrong?

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Aug 23 '07 #1
0 864

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

Similar topics

6
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only...
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,...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
1
by: Steven W. Orr | last post by:
Sorry, I had a small description problem. It's corrected below. I have a base class B and a derived class D which inherits from B. I also have a D2 class which inherits from D. D is used as a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.