Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"""': 0.05; '*args,': 0.07; 'class,': 0.07; 'decorator': 0.07; 'function,': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'here?': 0.09; 'inherited': 0.09; 'pointless': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'exercise': 0.13; '(the': 0.15; 'a(object):': 0.16; 'cc:name:python list': 0.16; 'hopeful': 0.16; 'inheritance': 0.16; 'presume': 0.16; 'stumbling': 0.16; 'trying': 0.21; 'decorators': 0.22; 'new,': 0.22; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'thanks!': 0.26; "doesn't": 0.28; 'correct': 0.28; 'trouble': 0.28; 'subsequently': 0.29; 'workaround': 0.29; 'class': 0.29; "i'm": 0.29; 'that.': 0.30; 'code': 0.31; 'point': 0.31; '(and': 0.32; 'running': 0.32; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'from:addr:googlemail.com': 0.35; 'jason': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'add': 0.36; 'but': 0.36; 'method': 0.36; 'subject:with': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'some': 0.38; 'received:209.85.214': 0.39; 'hello,': 0.39; 'your': 0.60; 'skip:u 10': 0.60; 'behavior': 0.64; 'here': 0.65; 'sounds': 0.71; 'goal': 0.74; 'avoid.': 0.84; 'basically,': 0.84; 'clearer': 0.84; 'duplication': 0.84; 'received:209.85.214.184': 0.84; 'received :mail-ob0-f184.google.com': 0.84; 'do:': 0.91; 'redefining': 0.91 X-Received: by 10.50.45.226 with SMTP id q2mr72472igm.0.1359717988086; Fri, 01 Feb 2013 03:26:28 -0800 (PST) Newsgroups: comp.lang.python Date: Fri, 1 Feb 2013 03:26:27 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 123.192.32.215 MIME-Version: 1.0 Subject: Re: confusion with decorators From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359717996 news.xs4all.nl 6840 [2001:888:2000:d::a6]:40870 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38037 Jason Swails=E6=96=BC 2013=E5=B9=B41=E6=9C=8831=E6=97=A5=E6=98=9F=E6=9C=9F= =E5=9B=9BUTC+8=E4=B8=8A=E5=8D=888=E6=99=8234=E5=88=8603=E7=A7=92=E5=AF=AB= =E9=81=93=EF=BC=9A > Hello, >=20 >=20 > I was having some trouble understanding decorators and inheritance and al= l that. =C2=A0This is what I was trying to do: >=20 >=20 >=20 > # untested > class A(object): > =C2=A0 =C2=A0def _protector_decorator(fcn): >=20 > =C2=A0 =C2=A0 =C2=A0 def newfcn(self, *args, **kwargs): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return fcn(self, *args, **kwargs) > =C2=A0 =C2=A0 =C2=A0 return newfcn >=20 >=20 >=20 > =C2=A0 =C2=A0@_protector_decorator > =C2=A0 =C2=A0def my_method(self, *args, **kwargs): > =C2=A0 =C2=A0 =C2=A0 """ do something here """ >=20 >=20 >=20 > class B(A): > =C2=A0 =C2=A0def _protector_decorator(fcn): > =C2=A0 =C2=A0 =C2=A0 def newfcn(self, *args, **kwargs): >=20 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0raise MyException('I do not want B to b= e able to access the protected functions') > =C2=A0 =C2=A0 =C2=A0 return newfcn >=20 >=20 >=20 > The goal of all that was to be able to change the behavior of my_method i= nside class B simply by redefining the decorator. Basically, what I want is= B.my_method() to be decorated by B._protector_decorator, but in the code I= 'm running it's decorated by A._protector_decorator. >=20 >=20 >=20 > I presume this is because once the decorator is applied to my_method in c= lass A, A.my_method is immediately bound to the new, 'decorated' function, = which is subsequently inherited (and not decorated, obviously), by B. >=20 >=20 >=20 > Am I correct here? =C2=A0My workaround was to simply copy the method from= class A to class B, after which B._protector_decorator decorated the metho= ds in B. =C2=A0While this doesn't make the use of decorators completely poi= ntless (the decorators actually do something in each class, it's just diffe= rent), it does add a bunch of code duplication which I was at one point hop= eful to avoid. >=20 >=20 >=20 > I'm still stumbling around with decorators a little, but this exercise ha= s made them a lot clearer to me. >=20 >=20 > Thanks! > Jason It sounds that you need a decorator mapper to=20 perform the functionality of your designs.