Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109741 > unrolled thread

Re: UserList - which methods needs to be overriden?

Started byPeter Otten <__peter__@web.de>
First post2016-06-09 14:58 +0200
Last post2016-06-09 14:58 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: UserList - which methods needs to be overriden? Peter Otten <__peter__@web.de> - 2016-06-09 14:58 +0200

#109741 — Re: UserList - which methods needs to be overriden?

FromPeter Otten <__peter__@web.de>
Date2016-06-09 14:58 +0200
SubjectRe: UserList - which methods needs to be overriden?
Message-ID<mailman.110.1465477129.2306.python-list@python.org>
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 
efficient.

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):
    try:
        return super().whatever(*args)
    finally:
        self.changed()

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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web