UserList - which methods needs to be overriden?

Peter Otten __peter__ at
Thu Jun 9 08:58:20 EDT 2016

Nagy László Zsolt wrote:

>>> 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))
> Thanks. It narrows down the list of possible methods to be examined.
>> 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...
> Are you saying that for any new project, I should subclass from list and
> dict, instead of UserList and UserDict?

Using the built-in list or dict is problematic because sometimes the 
subclass methods are ignored when the internal data is accessed (sorry, I 
don't have an example).

I thought you should subclass MutableSequence instead of UserList, but I may 
have been wrong. While you minimize your effort -- just add methods until 
you can instantiate your subclass -- the resulting class will not be very 

How detailed is your change notification? If it's always the same method 
invocation you may be able to create wrappers like

def whatever(self, *args):
        return super().whatever(*args)

automatically, and the base class, be it list or UserList or whatever 
becomes a detail that can be chosen on a case by case basis.

More information about the Python-list mailing list