class variables for subclasses tuple

alainpoint at yahoo.fr alainpoint at yahoo.fr
Wed Mar 8 05:34:59 EST 2006


As an supplement to my previous post, please find hereunder a snippet
for my unsuccessful attempt (commented out snippet does not work):
def superTuple(*attribute_names):
	nargs = len(attribute_names)
	class T(tuple):
		def __new__(cls, *args):
			return tuple.__new__(cls, args)
		class __metaclass__(type):
			x=property(lambda self:0)
			y=property(lambda self:1)
			z=property(lambda self:2)
		x=property(lambda self:self[0])
		y=property(lambda self:self[1])
		z=property(lambda self:self[2])
	#for idx, attr_name in enumerate(attribute_names):
	#	print 'attr name',attr_name, idx
	#	setattr(T.__metaclass__,attr_name, property(lambda cls:idx))
	#for idx, attr_name in enumerate(attribute_names):
	#	print 'attr name',attr_name
	#	setattr(T,attr_name, property(lambda self:self[idx]))
	return T
if __name__ == '__main__':
	Point=superTuple('x','y','z')
	p=Point(4,7,9)
	assert p.x==p[0]
	assert p.y==p[1]
	assert p.z==p[2]
	assert Point.x==0
	assert Point.y==1
	assert Point.z==2

Alain




More information about the Python-list mailing list