Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37975
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <davea@davea.name> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '"""': 0.05; 'compiler': 0.05; 'things.': 0.05; '*args,': 0.07; 'class,': 0.07; 'correct.': 0.07; 'decorator': 0.07; 'function,': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'executes': 0.09; 'here?': 0.09; 'inherited': 0.09; 'pointless': 0.09; 'def': 0.10; 'exercise': 0.13; '(the': 0.15; 'value.': 0.15; '(note,': 0.16; 'a(object):': 0.16; 'hopeful': 0.16; 'inheritance': 0.16; 'presume': 0.16; 'shorthand': 0.16; 'skip:@ 20': 0.16; 'stumbling': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'appropriate': 0.20; 'trying': 0.21; 'decorators': 0.22; 'new,': 0.22; 'defined': 0.22; 'skip:_ 20': 0.22; "i've": 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.28; 'correct': 0.28; 'trouble': 0.28; 'once,': 0.29; 'subsequently': 0.29; 'workaround': 0.29; 'definition': 0.29; 'probably': 0.29; 'class': 0.29; "i'm": 0.29; 'that.': 0.30; 'ends': 0.30; 'function': 0.30; 'code': 0.31; 'point': 0.31; '(and': 0.32; 'running': 0.32; 'skip:_ 30': 0.32; 'to:addr:python-list': 0.33; 'skip:b 20': 0.34; 'clear': 0.35; 'jason': 0.35; 'pm,': 0.35; 'something': 0.35; 'add': 0.36; 'but': 0.36; 'method': 0.36; 'subject:with': 0.36; 'execute': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'hello,': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'skip:u 10': 0.60; 'expert': 0.62; 'behavior': 0.64; 'here': 0.65; 'received:74.208': 0.71; 'goal': 0.74; 'avoid.': 0.84; 'basically,': 0.84; 'clearer': 0.84; 'duplication': 0.84; 'received:74.208.4.194': 0.84; 'do:': 0.91; 'redefining': 0.91 |
| Date | Wed, 30 Jan 2013 20:12:39 -0500 |
| From | Dave Angel <davea@davea.name> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: confusion with decorators |
| References | <CAEk9e3ocvL6ikZYv1+sv_DZHdi6NFxW71=ztPcyCH=JVTt_f=g@mail.gmail.com> |
| In-Reply-To | <CAEk9e3ocvL6ikZYv1+sv_DZHdi6NFxW71=ztPcyCH=JVTt_f=g@mail.gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:KpnTGV0bPVtBcDSOgaki/uyb97QySPxhibdU/MLEsNT JC06i/vS15Aw10/YWvJdflQqaeOLNWRjJ07QlMsTAXc6iffFgd ePxvuVGUwkmVP3VfDLghtdi7ohJ3vqEKzbgTPpPCjC25I95Bvd zz3XeixSVmW3yKwr4GZ0cxWTWwSPkllXlZU7+Kzf6QbpePHKMX WKm0lrgm8cvUFLQ26fSZMn9mdSTMnXazBI6mhHcXk1T0PNEhu3 lLc8sMWLw9g4cDlwCJPoLNfU/NuOoVa1RTe5lI2ehmlDn8sLnl iOyiDs6y3zz4SzOl57vxJWpD2aEoA0ZHNcJVMl3G8odSOCRbw= = |
| 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.1240.1359594784.2939.python-list@python.org> (permalink) |
| Lines | 66 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1359594784 news.xs4all.nl 6918 [2001:888:2000:d::a6]:53402 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:37975 |
Show key headers only | View raw
On 01/30/2013 07:34 PM, Jason Swails wrote:
> 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.
>
>
I'm certainly not the expert on decorators; I've only used them for
simple things. But I think I can clear up one misconception.
The decorator function will execute while *compiling* the class A, and
the one in class B is unreferenced.
The decorator @_protector_decorator is shorthand for something like
mymethod = _protector_decorator(mymethod)
So by the time the compiler ends with class A, the mymethod has its
final value.
(Note, I've not used a decorator that was defined inside a class, so I'm
probably missing the appropriate A. or self. or cls. overrides.)
But the order of definition is still correct. A decorator executes
once, just after a function is completed.
--
DaveA
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: confusion with decorators Dave Angel <davea@davea.name> - 2013-01-30 20:12 -0500 Re: confusion with decorators Duncan Booth <duncan.booth@invalid.invalid> - 2013-02-04 10:19 +0000
csiph-web