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


Groups > comp.lang.python > #38037

Re: confusion with decorators

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 <dihedral88888@googlemail.com>
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 <mailman.1239.1359592452.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw
References <mailman.1239.1359592452.2939.python-list@python.org>
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 <dihedral88888@googlemail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.1276.1359717996.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


Jason Swails於 2013年1月31日星期四UTC+8上午8時34分03秒寫道:
> 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

It sounds that you need a decorator mapper to 
perform the functionality of your designs.

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


Thread

confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-30 19:34 -0500
  Re: confusion with decorators Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-31 05:46 +0000
    Re: confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-31 08:25 -0500
      Re: confusion with decorators Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-01 10:16 +1100
        Re: confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-31 22:13 -0500
    Re: confusion with decorators Chris Angelico <rosuav@gmail.com> - 2013-02-01 02:28 +1100
    Re: confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-31 11:00 -0500
    Re: confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-31 12:53 -0500
    Re: confusion with decorators Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-01 08:31 +1100
  Re: confusion with decorators 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-01 03:26 -0800
  Re: confusion with decorators 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-01 03:26 -0800

csiph-web