How do you control _all_ items added to a list?

Fuzzyman fuzzyman at gmail.com
Mon Feb 28 09:07:46 EST 2005


Xif wrote:
> Hi
>
> I want to have control over all items added to a list.
>
> The first attempt was to subclass list and override its .append()
> method.
>
> Problem is, there are plenty of other ways the list can have items
> added to it - e.g. extend, insert - and theyr'e not at all affected
by
> the changes I made to append.
>
> Is there some "base" item-adding method that all other item-adding
> methods use, so I can override it and have the changes affect all
> item-adding functions?
>

If you subclass list then you will need to override all methods that
can set items. This is because the built in methods work directly with
the internal structure and are implemented in C. It's a bit of a
pain... but not that much.

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

> Thanks,
> Xif




More information about the Python-list mailing list