Addressing the last element of a list
Peter Otten
__peter__ at web.de
Thu Nov 10 10:42:22 EST 2005
Antoon Pardon wrote:
> Write somekind of property so that if you manipulate a.x it would
> manipulate data[-1]
Like that?
>>> def make_prp(index):
... def get(self): return self[index]
... def set(self, value): self[index] = value
... return property(get, set)
...
>>> class List(list):
... first = make_prp(0)
... last = make_prp(-1)
...
>>> items = List([1,2,3])
>>> items.first
1
>>> items.last = 42
>>> items
[1, 2, 42]
However, that's customizing attribute access, not name binding.
Peter
More information about the Python-list
mailing list