automatically assigning names to indexes

simonwittber at simonwittber at
Tue Jul 12 08:04:35 CEST 2005

I know its been done before, but I'm hacking away on a simple Vector

class Vector(tuple):
    def __add__(self, b):
        return Vector([x+y for x,y in zip(self, b)])
    def __sub__(self, b):
        return Vector([x-y for x,y in zip(self, b)])
    def __div__(self, b):
        return Vector([x/b for x in self])
    def __mul__(self, b):
        return Vector([x*b for x in self])

I like it, because it is simple, and can work with vectors of any

However, I'd like to add attribute access (magically), so I can do

v = Vector((1,2,3))
print v.x
print v.y
print v.z

as well as:

print v[0]
print v[1]
print v[2]

Has anyone got any ideas on how this might be done?


More information about the Python-list mailing list