automatically assigning names to indexes

simonwittber at gmail.com simonwittber at gmail.com
Tue Jul 12 04:03:42 EDT 2005


> >>> class Vector(tuple):
> ...     x = property(lambda self: self[0])
> ...     y = property(lambda self: self[1])
> ...     z = property(lambda self: self[2])
> ...
> >>> Vector("abc")
> ('a', 'b', 'c')
> >>> Vector("abc").z
> 'c'
> >>> Vector("abc")[2]
> 'c'
>

Aha! You have simultaneously proposed a neat solution, and shown me a
bug in my class! (It shouldn't accept strings)

Thanks.

Sw.




More information about the Python-list mailing list