Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Selik Newsgroups: comp.lang.python Subject: Re: UserList - which methods needs to be overriden? Date: Thu, 09 Jun 2016 22:38:28 +0000 Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de l2PB33SeEBo25jI6U53eIAvYQHuhscvxxj2tkrfV32dg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '*args,': 0.07; 'override': 0.07; 'wrapper': 0.07; 'subclass': 0.09; 'subject:which': 0.09; 'def': 0.13; 'thu,': 0.15; '2016': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'to:name:python- list@python.org': 0.20; 'to:2**1': 0.21; '(the': 0.22; 'decorator': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; '**kwargs)': 0.29; '**kwargs):': 0.29; 'index,': 0.29; 'value)': 0.29; 'skip:_ 10': 0.32; 'class': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'skip:n 10': 0.62; 'func(*args,': 0.84; 'nagy': 0.84; 'abc': 0.91; 'subject:needs': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=OyDeaGPjM1YzGfywHRj2ujRZ+JKtE25enCd74o5v6qg=; b=LgDl3FbmC+gN5aszPHzbOiMzBr6fQlAUsLMPfHqVo5e1GoQ5IJvkpB64aM0j+Hlviu v1GYGJCZrYDMdp3X+oVPMFFmmU/KKU9on5HQG6p4Vqyga39AO76r8X7uuvEUEzX8HpZl qhRciWzOnKnP4Ug3z9gkHlV8InrpWbcLN3RI+vppEfSoOuYdq6+ri3acLUM6XNEQK2Xj +DGx7HF5ZT0057rXyCBDp6iMZSaEUcP2WOfPj5X11PwjiQjydTgtrwN4RTUsVFyrFhHl ZbDjXXHGifo3KLLpmK/CVNY8hLDiEOvu5XRhSRlGFLLDqituAj8X3mctSzja3akW0ekp wTFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=OyDeaGPjM1YzGfywHRj2ujRZ+JKtE25enCd74o5v6qg=; b=MN5hh28wijlQmM6ZDxOGb40ShX6BoM6gdukS1nGVMGeaKdfsoe/j1IZr9tlF+OBh2v Pbg47NCYZ2weJ+AomHYH6R6g0mprjwdeS42RoRuL1RujX0fMSMbijwS33w/tEZZj3R7m 0UVpJMaBPAqaVRNpxNHUHYM1X7uWrFRTwyo29wiYsKIrQ1Vcej1fRXIgDjxLMaiyqcje uS3iBPJfr26W4+pEqotRNdQ7SpRidLw0WoGhGc363Utt37h1pKBvn1378ca6J4XNZECq p8MyfIU4uP7iUeHGwMsta9pBw+6hTkx+QOWwnZj5afpPDdp2Gq0oBg6ejX/2OIZIY3BB 09hw== X-Gm-Message-State: ALyK8tJ4O0VFy0pY3re9wEl86fj2xiRz/rBnvOW5MfJ2XGNKUlbY2KEDSe75roM7oSftQ0qE1TILm4fzBHpZhQ== X-Received: by 10.140.162.67 with SMTP id i64mr12420702qhi.103.1465511918036; Thu, 09 Jun 2016 15:38:38 -0700 (PDT) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109764 On Thu, Jun 9, 2016 at 5:07 AM Nagy L=C3=A1szl=C3=B3 Zsolt 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. from collections.abc import MutableSequence def noisy(message): def decorator(func): def wrapper(*args, **kwargs): print(message) return func(*args, **kwargs) return wrapper return decorator class NoisyList(MutableSequence): def __init__(self, *args, **kwargs): self.contents =3D list(*args, **kwargs) def __getitem__(self, index): return self.contents[index] def __len__(self): return len(self.contents) @noisy('set') def __setitem__(self, index, value): self.contents[index] =3D value @noisy('del') def __delitem__(self, index): del self.contents[index] @noisy('insert') def insert(self, index, value): self.contents.insert(index, value)