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


Groups > comp.lang.python > #42041

Re: Decorator help

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!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; '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; 'cc:addr:python-list': 0.11; 'def': 0.12; 'thread': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; 'appreciated!': 0.16; 'args.': 0.16; 'callable': 0.16; 'callable.': 0.16; 'illustrate': 0.16; 'it),': 0.16; 'referencing': 0.16; '\xa0def': 0.16; '\xa0this': 0.16; '\xa0you': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'trying': 0.19; '(where': 0.19; '8bit%:5': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'decorators': 0.24; 'replace': 0.24; '\xa0so': 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'sets': 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; '(since': 0.31; 'are.': 0.31; 'class': 0.32; 'another': 0.32; 'quite': 0.32; 'guess': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; '(e.g.,': 0.36; 'in.': 0.36; 'instances': 0.36; 'similar': 0.36; 'should': 0.36; 'clear': 0.37; 'being': 0.38; 'skip:& 10': 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; '8bit%:6': 0.40; 'hope': 0.61; 'helps': 0.61; 'new': 0.61; 'simple': 0.61; 'first': 0.61; 'here:': 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; 'hoping': 0.75; 'decorate': 0.84; 'adopt': 0.91; '\xa0there': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=BYKfalBVqQONOvKZnxsTGF4yc2TT6SRta7TmGkBzq3k=; b=omplhxkwGbtVoqDk0yaTgqMUCSbNPdS2dq+faT9UNvWc/AdKd71SFBaaA9XG5GN5xW 0wwCdcBDxCna2irilImk2E32UtqeyVMuuzxVcY27BAM4DYWFqB2Sm2fGX6Uil1+9Yz6K OijX+WE+67qGa8J6XclLV6hUi+RDEGbCyel6QPpUZhs0SX2Fpg7QvTxt4bSXXjaXUfxt CLjpPxjb1LUkf7Slpa6+C16VgInJr/G5pH99VmDvFMULpXqhBcfyqDteJkEhGPncC9NT Yi7u9XT+ZXpWV2ZsyTmznOLb+IEF9IiTRKlr/mBlob1Cr+GdtoYFK18R0O/t0U40h5y5 f2Sg==
MIME-Version 1.0
X-Received by 10.50.153.232 with SMTP id vj8mr5359762igb.2.1364416389044; Wed, 27 Mar 2013 13:33:09 -0700 (PDT)
In-Reply-To <assp.07983ebbdb.b3538dddac424953a000b1222309380f@exch.activenetwerx.com>
References <assp.07983ebbdb.b3538dddac424953a000b1222309380f@exch.activenetwerx.com>
Date Wed, 27 Mar 2013 16:33:08 -0400
Subject Re: Decorator help
From Jason Swails <jason.swails@gmail.com>
To "Joseph L. Casale" <jcasale@activenetwerx.com>
Content-Type multipart/alternative; boundary=e89a8f23551f31ef8404d8edf273
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>
Newsgroups comp.lang.python
Message-ID <mailman.3827.1364416391.2939.python-list@python.org> (permalink)
Lines 114
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364416392 news.xs4all.nl 6938 [2001:888:2000:d::a6]:34083
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42041

Show key headers only | View raw


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

On Wed, Mar 27, 2013 at 3:49 PM, Joseph L. Casale <jcasale@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):
   """ 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 | NextNext 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