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 -- http://mail.python.org/mailman/listinfo/python-list