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


Groups > comp.lang.python > #109784

Re: UserList - which methods needs to be overriden?

From Michael Selik <michael.selik@gmail.com>
Newsgroups comp.lang.python
Subject Re: UserList - which methods needs to be overriden?
Date 2016-06-10 13:23 +0000
Message-ID <mailman.135.1465565033.2306.python-list@python.org> (permalink)
References <b245d88e-9b90-bb90-bd2a-13ee82f7a5c8@shopzeus.com> <CAGgTfkMakty7MohzRwLPpfRJikKBC0osTE83T0hZ9Scn+1NBOA@mail.gmail.com> <1f4d1918-1e38-abba-aca7-830d91d35bd4@shopzeus.com> <CAGgTfkMyW6R6Xi0ai9j833Opx7OnbutLc0pYLvVk2zRAjA+AYg@mail.gmail.com> <CAGgTfkPqAY21eBhNJbynA2BJr5M0TNQPb=n6wCqBryDsdBC5XQ@mail.gmail.com>

Show all headers | View raw


On Fri, Jun 10, 2016 at 9:18 AM Michael Selik <michael.selik@gmail.com>
wrote:

>
>
> On Fri, Jun 10, 2016, 4:05 AM Nagy László Zsolt <gandalf@shopzeus.com>
> wrote:
>
>> 2016.06.10. 0:38 keltezéssel, Michael Selik írta:
>>
>> On Thu, Jun 9, 2016 at 5:07 AM Nagy László Zsolt <gandalf@shopzeus.com>
>> wrote:
>>
>>> I would like to create a collections.UserList subclass that can notify
>>> others when the list is mutated.
>>>
>>
>> Why not subclass MutableSequence instead? The ABC will tell you which
>> methods to override (the abstract ones). The mixin methods rely on those
>> overrides.
>>
>> The thing with MutableSequence is that it is quite inefficient. For
>> example, it implements clear() by calling pop() in a loop. It implements
>> extend() by calling append() in a loop. And we all know that the built-in
>> extend() method of the list object is much more efficient.
>>
>
If you find that extend is indeed a bottleneck, there's an easy fix.

class NoisyList(MutableSequence):
    @noisy('extend')
    def extend(self, iterable):
        self.container.extend(iterable)

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


Thread

Re: UserList - which methods needs to be overriden? Michael Selik <michael.selik@gmail.com> - 2016-06-10 13:23 +0000

csiph-web