UserList - which methods needs to be overriden?

Peter Otten __peter__ at web.de
Thu Jun 9 05:40:03 EDT 2016


Nagy László Zsolt wrote:

> I would like to create a collections.UserList subclass that can notify
> others when the list is mutated. I'm not sure which methods I need to
> override. I have checked the sources, and it seems difficult to figure
> out which methods needs to be overriden. For example,
> MutableSequence.extend is just a for loop that calls append(). Also, the
> clear() method just calls pop() until the sequence becomes empty. But
> UserList overrides extend, so if I ever want to know when the list is
> modified, I need to override extend() too. There are lots of base
> classes (UserList, MutableSequence, Sequence ) etc and some mutating
> methods might be inherited from those. I'll also have to do this for
> UserDict (which is a MutableMapping, which is a Mapping).
> 
> Is there a way to easily find out which methods of are mutating the
> collection? Other than going over the source and checking all (normal
> and inherited) methods?

How about

set(dir(collections.UserList)) - set(dir(collections.Sequence))

> And finally: set is a built in container type, but we don't have a
> UserSet in collections. Was it ever proposed to be added to collections?

I thought it was the other way round and UserList/UserDict were only kept 
for backwards-compatibility, but don't find anything in the docs to support 
this...



More information about the Python-list mailing list