How does __setitem__() work?
Alex
cut_me_out at hotmail.com
Mon Jun 19 18:08:52 EDT 2000
If you do this:
class Test:
def __setattr__(self, attribute, value):
print 'setting', attribute, 'to', value
self.__dict__[attribute] = value
def __setitem__(self, index, value):
print 'setting index', index, 'to', value
## THE_BLANK
t = Test(); t.list = [1,2,3]
t[0] = 7
The result is
setting list to [1, 2, 3]
setting index 0 to 7
You have to assign to an 'item' of the instance itself for the
__setitem__ method to be called. Assigning to 'items' of members won't
involve the instance itself.
Alex.
More information about the Python-list
mailing list