Hi!

I'm in the position that I have a bunch of classes defined before hand
and then in some special circumstances I need to dynamically create a
class that has a number of the static classes as parents.

So I thought I could use classobj() from the new module, it seem exactly
what I wanted.

But, it doesn't perform as I expected.

I've made an extremely simple program to try to show what I mean and
what I expected. It's attached to this mail.

So, how should I have done it ?

-- Roland
#!/usr/bin/env python 

class A(object):
	def __init__(self):
		if "list" in self.__dict__.keys():
			self.list.extend(["a","b","c"])
		else:
			self.list = ["a","b","c"]
		self.list.sort()

class B(object):
	def __init__(self):
		if "list" in self.__dict__.keys():
			self.list.extend(["x","y","z"])
		else:
			self.list = ["x","y","z"]
		self.list.sort()

class C(B):
	def __init__(self):
		B.__init__(self)
		if "list" in self.__dict__.keys():
			self.list.extend(["0","1","2"])
		else:
			self.list = ["0","1","2"]
		self.list.sort()

class X(C):
	def __init__(self):
		C.__init__(self)
		if "list" in self.__dict__.keys():
			self.list.extend(["9","8","7"])
		else:
			self.list = ["9","8","7"]
		self.list.sort()

__test__ = { "a":"""
>>> from new import classobj
>>> c = C()
>>> c.list
['0', '1', '2', 'x', 'y', 'z']
>>> x = X()
>>> x.list
['0', '1', '2', '7', '8', '9', 'x', 'y', 'z']
>>> D = classobj("D",(A,C),{})
>>> d = D()
>>> d.list
['0', '1', '2', '7', '8', '9', 'x', 'y', 'z']
>>> E = classobj("E",(C,A),{})
>>> e = E()
>>> e.list
['0', '1', '2', '7', '8', '9', 'x', 'y', 'z']
"""}

if __name__ == "__main__":
	import doctest
	doctest.testmod()

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to