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


Groups > comp.lang.python > #109741

Re: UserList - which methods needs to be overriden?

Path csiph.com!feeder.erje.net!1.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: UserList - which methods needs to be overriden?
Date Thu, 09 Jun 2016 14:58:20 +0200
Organization None
Lines 38
Message-ID <mailman.110.1465477129.2306.python-list@python.org> (permalink)
References <b245d88e-9b90-bb90-bd2a-13ee82f7a5c8@shopzeus.com> <njbdhk$pv2$1@ger.gmane.org> <917ca9c4-aef1-6681-ec70-62de8916117d@shopzeus.com> <njbp5d$mn9$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace news.uni-berlin.de AAq2D3VJ3iIFkDnf9O3Ruws8Xz1fSwiheKqdSQwrInWw==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'resulting': 0.04; 'finally:': 0.05; 'ignored': 0.05; '*args):': 0.09; 'dict': 0.09; 'invocation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'subject:which': 0.09; 'def': 0.13; '(sorry,': 0.16; 'example).': 0.16; 'instantiate': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrappers': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'project,': 0.18; 'thanks.': 0.18; '>>>': 0.20; 'saying': 0.22; 'class,': 0.22; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'checking': 0.27; 'becomes': 0.30; 'skip:s 30': 0.31; 'class': 0.33; 'source': 0.33; 'add': 0.34; 'list': 0.34; 'basis.': 0.35; 'sometimes': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'thought': 0.37; 'detail': 0.38; 'anything': 0.38; 'data': 0.39; 'whatever': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'skip:n 10': 0.62; 'accessed': 0.66; 'dict,': 0.84; 'nagy': 0.84; 'this...': 0.84; 'subject:needs': 0.93
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9048.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <njbp5d$mn9$1@ger.gmane.org>
X-Mailman-Original-References <b245d88e-9b90-bb90-bd2a-13ee82f7a5c8@shopzeus.com> <njbdhk$pv2$1@ger.gmane.org> <917ca9c4-aef1-6681-ec70-62de8916117d@shopzeus.com>
Xref csiph.com comp.lang.python:109741

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web