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


Groups > comp.lang.python > #42356

Re: Decorator help

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'parameters': 0.04; 'argument': 0.05; '"""': 0.07; 'assign': 0.07; 'variables': 0.07; 'subject:help': 0.08; '*args,': 0.09; 'arguments': 0.09; 'decorator': 0.09; 'function,': 0.09; 'imported': 0.09; 'methods,': 0.09; 'mind,': 0.09; 'to:addr:comp.lang.python': 0.09; 'translate': 0.10; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'thread': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; 'appreciated!': 0.16; 'args.': 0.16; 'c/c++': 0.16; 'callable': 0.16; 'callable.': 0.16; 'illustrate': 0.16; 'it),': 0.16; 'subclassing': 0.16; '\xc2\xa0you': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'trying': 0.19; '(where': 0.19; 'example': 0.22; 'programming': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'header:User- Agent:1': 0.23; 'decorators': 0.24; 'replace': 0.24; 'define': 0.26; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '(which': 0.31; '(since': 0.31; 'are.': 0.31; 'class': 0.32; 'another': 0.32; 'quite': 0.32; 'guess': 0.33; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; '(e.g.,': 0.36; 'in.': 0.36; 'instances': 0.36; 'similar': 0.36; 'should': 0.36; 'clear': 0.37; 'being': 0.38; 'from:addr:googlemail.com': 0.38; 'jason': 0.38; 'list,': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'guidance': 0.39; 'itself': 0.39; 'called': 0.40; 'hope': 0.61; 'helps': 0.61; 'new': 0.61; 'simple': 0.61; 'first': 0.61; 'save': 0.62; 'making': 0.63; 'information': 0.63; 'skip:n 10': 0.64; 'provide': 0.64; 'within': 0.65; 'here': 0.66; 'mar': 0.68; 'default': 0.69; 'hoping': 0.75; 'decorate': 0.84; 'adopt': 0.91; '2013': 0.98
X-Received by 10.50.27.42 with SMTP id q10mr391352igg.1.1364699186072; Sat, 30 Mar 2013 20:06:26 -0700 (PDT)
Newsgroups comp.lang.python
Date Sat, 30 Mar 2013 20:06:25 -0700 (PDT)
In-Reply-To <mailman.3827.1364416391.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 <assp.07983ebbdb.b3538dddac424953a000b1222309380f@exch.activenetwerx.com> <mailman.3827.1364416391.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: Decorator help
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.org" <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.4009.1364699195.2939.python-list@python.org> (permalink)
Lines 90
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364699195 news.xs4all.nl 6926 [2001:888:2000:d::a6]:44862
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42356

Show key headers only | View raw


Jason Swails於 2013年3月28日星期四UTC+8上午4時33分08秒寫道:
> On Wed, Mar 27, 2013 at 3:49 PM, Joseph L. Casale <jca...@activenetwerx.com> wrote:
> 
> I have a class which sets up some class vars, then several methods that are passed in data
> 
> and do work referencing the class vars.
> 
> 
> 
> 
> 
> I want to decorate these methods, the decorator needs access to the class vars, so I thought
> 
> about making the decorator its own class and allowing it to accept args.
> 
> 
> 
> 
> 
> I was hoping to do all the work on in_data from within the decorator, which requires access
> 
> to several MyClass vars. Not clear on the syntax/usage with this approach here, any guidance
> 
> would be greatly appreciated!
> 
> 
> 
> My guess is that you don't quite 'get' decorators yet (since I remember similar types of questions when trying to learn them myself).  Decorators execute when the class type itself is being built (e.g., when a module is first imported at runtime).  So decorators will never take instance variables as arguments (nor should they, since no instance can possibly exist when they execute).  Bear in mind, a decorator should take a callable as an argument (and any number of 'static' parameters you want to assign it), and return another callable.
> 
> 
> 
> I provide an example decorator using the format the I typically adopt below (where the decorator is a simple function, not a class):
> 
> 
> def my_decorator(fcn):

I might add default parameters here  if I am programming 
in python to save the troubles of subclassing
similar decorators.

But that is only the stylish problem in python.

I might need to translate the decorator part into cython 
or c/c++ in the future.


>    """ Decorator for a function """
> 
>    def new_fcn(self, *args, **kwargs):
>       """ This is the new function that we will return. """
>       # You can access any instance variables here
>       returnval = fcn(self, *args, **kwargs)
> 
>       # Do anything else here with instance variables
>       return returnval # or any other return value you want
>    
>    return new_fcn
> 
> 
> Notice here I define a new_fcn callable function that takes self and an arbitrary argument/keyword-argument list, and I return this function (which does not get called) to replace the function I passed in.  You can use instance variables inside new_fcn since new_fcn is called by instances of MyClass.  This is a very simple type of decorator, but hopefully helps illustrate what decorators are.  There is a particularly good thread on SO with information about decorators here: http://stackoverflow.com/questions/739654/understanding-python-decorators
> 
> 
> 
> Hope this helps,
> Jason

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


Thread

Re: Decorator help Jason Swails <jason.swails@gmail.com> - 2013-03-27 16:33 -0400
  Re: Decorator help 88888 Dihedral <dihedral88888@googlemail.com> - 2013-03-30 20:06 -0700
  Re: Decorator help 88888 Dihedral <dihedral88888@googlemail.com> - 2013-03-30 20:06 -0700

csiph-web