Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin.stu.neva.ru!news2.euro.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*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; 'def': 0.10; 'exercise': 0.13; '(the': 0.15; 'a(object):': 0.16; 'hopeful': 0.16; 'inheritance': 0.16; 'presume': 0.16; 'skip:@ 20': 0.16; 'stumbling': 0.16; 'to:name:python list': 0.16; '\xa0def': 0.16; '\xa0this': 0.16; 'trying': 0.21; 'decorators': 0.22; 'new,': 0.22; 'skip:_ 20': 0.22; 'raise': 0.24; 'thanks!': 0.26; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'correct': 0.28; 'trouble': 0.28; 'subsequently': 0.29; 'workaround': 0.29; 'skip:& 10': 0.29; 'class': 0.29; "i'm": 0.29; 'that.': 0.30; 'code': 0.31; 'point': 0.31; '(and': 0.32; 'running': 0.32; 'to:addr :python-list': 0.33; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'jason': 0.35; 'something': 0.35; 'add': 0.36; 'but': 0.36; 'method': 0.36; 'subject:with': 0.36; 'does': 0.37; 'some': 0.38; 'to:addr:python.org': 0.39; 'hello,': 0.39; 'skip:u 10': 0.60; 'behavior': 0.64; 'here': 0.65; 'goal': 0.74; 'avoid.': 0.84; 'basically,': 0.84; 'clearer': 0.84; 'duplication': 0.84; 'do:': 0.91; 'redefining': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=cN7Wx0QddB8BgPqzuUPPYsn2ko+MRY7++Y2s4w46I9w=; b=uFI4IiK0InFoKeWMlSBlgCJhp7IVqV/tsFYteKQu4Vj+Exa+sT+n6gTIUE+SHavPfP Kp2P+IcLKz6ouAiDqhPdUwBax+VQsEOGRZL5o1tc5rCXaGCJMhGkBmmYd8Ks172zSnNZ V+tmZfZRFIEI3PN4jR+6Vw0T7kVR99WkcLuPFRxd/kvtBohC/VNqYV0ZRdmuJF/TW7q0 u3LTRWWRsQHy94xc2QyqwhADuVZr1tJs9bMa8YcEgn2oVV22kKTnETN/9AYCDOVWZOmh IFcsU0tKc/KvlDikvAv1vV4sUOhBFsU2qKeoRgpMIlDHdtjEN+9PYYnshnhTcsm4i0/v a8OQ== MIME-Version: 1.0 X-Received: by 10.42.121.14 with SMTP id h14mr4865790icr.27.1359592443307; Wed, 30 Jan 2013 16:34:03 -0800 (PST) Date: Wed, 30 Jan 2013 19:34:03 -0500 Subject: confusion with decorators From: Jason Swails To: python list Content-Type: multipart/alternative; boundary=20cf301b66719f672e04d48ac8fc 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: , Newsgroups: comp.lang.python Message-ID: Lines: 97 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359592452 news.xs4all.nl 6927 [2001:888:2000:d::a6]:48279 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37974 --20cf301b66719f672e04d48ac8fc Content-Type: text/plain; charset=ISO-8859-1 Hello, I was having some trouble understanding decorators and inheritance and all that. This is what I was trying to do: # untested class A(object): def _protector_decorator(fcn): def newfcn(self, *args, **kwargs): return fcn(self, *args, **kwargs) return newfcn @_protector_decorator def my_method(self, *args, **kwargs): """ do something here """ class B(A): def _protector_decorator(fcn): def newfcn(self, *args, **kwargs): raise MyException('I do not want B to be able to access the protected functions') return newfcn The goal of all that was to be able to change the behavior of my_method inside 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. I presume this is because once the decorator is applied to my_method in class A, A.my_method is immediately bound to the new, 'decorated' function, which is subsequently inherited (and not decorated, obviously), by B. Am I correct here? My workaround was to simply copy the method from class A to class B, after which B._protector_decorator decorated the methods in B. While this doesn't make the use of decorators completely pointless (the decorators actually do something in each class, it's just different), it does add a bunch of code duplication which I was at one point hopeful to avoid. I'm still stumbling around with decorators a little, but this exercise has made them a lot clearer to me. Thanks! Jason --20cf301b66719f672e04d48ac8fc Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello,

I was having some trouble understandin= g decorators and inheritance and all that. =A0This is what I was trying to = do:

= # untested
class A(object):=
=A0 =A0def _protector_decorator(fcn):
=A0 =A0 =A0 def newfcn(self, *args, **kwargs):=
=A0 =A0 =A0 =A0 =A0return fcn(sel= f, *args, **kwargs)
=A0 =A0 =A0 re= turn newfcn

= =A0 =A0@_protector_decorator
=A0 = =A0def my_method(self, *args, **kwargs):
=A0 =A0 =A0 """ do something here """<= /font>

= class B(A):
=A0 =A0def _protector_= decorator(fcn):
=A0 =A0 =A0 def ne= wfcn(self, *args, **kwargs):
=A0 =A0 =A0 =A0 =A0raise MyException('I do= not want B to be able to access the protected functions')
=
=A0 =A0 =A0 return newfcn

The goal of all that was to be able to change the behavio= r of my_method inside class B simply by redefining the decorator. Basically= , what I want is B.my_method() to be decorated by B._protector_decorator, b= ut in the code I'm running it's decorated by A._protector_decorator= .

I presume this is because once the decorator is applied= to my_method in class A, A.my_method is immediately bound to the new, '= ;decorated' function, which is subsequently inherited (and not decorate= d, obviously), by B.

Am I correct here? =A0My workaround was to simply copy = the method from class A to class B, after which B._protector_decorator deco= rated the methods in B. =A0While this doesn't make the use of decorator= s completely pointless (the decorators actually do something in each class,= it's just different), it does add a bunch of code duplication which I = was at one point hopeful to avoid.

I'm still stumbling around with decorators a little= , but this exercise has made them a lot clearer to me.

=
Thanks!
Jason

--20cf301b66719f672e04d48ac8fc--