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


Groups > comp.lang.python > #37974

confusion with decorators

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 <jason.swails@gmail.com>
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 <jason.swails@gmail.com>
To python list <python-list@python.org>
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 <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>
Newsgroups comp.lang.python
Message-ID <mailman.1239.1359592452.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | NextNext 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