Overriding adding items to container

Jyrinx jyrinxat at mindspringdot.com
Tue Feb 12 22:48:44 EST 2002


I have a class derived from list (v2.2 is cool ...), and I want to
override its behavior so that a function is called whenever an object is
added to the list (in particular, I want to create a weak reference to
the parent). To wit:

>>> bob = myList()
>>> joe = myItem()
>>> bob.append(joe) # (or insert(joe), or bob[0:0]=[joe], etc.)
>>> joe.parent is bob
1

Is there any good, general way to do this? It seems I can override
__setitem__(), append(), insert(), and others, but it would seem more
elegant just to be able to change one thing. Is this possible? Thanks!

Jyrinx
jyrinxat at mindspringdot.com (spam-proofed)



More information about the Python-list mailing list