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: 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: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: 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 To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: 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 Jason Swails=E6=96=BC 2013=E5=B9=B43=E6=9C=8828=E6=97=A5=E6=98=9F=E6=9C=9F= =E5=9B=9BUTC+8=E4=B8=8A=E5=8D=884=E6=99=8233=E5=88=8608=E7=A7=92=E5=AF=AB= =E9=81=93=EF=BC=9A > On Wed, Mar 27, 2013 at 3:49 PM, Joseph L. Casale wrote: >=20 > I have a class=C2=A0which=C2=A0sets up some class vars, then several meth= ods that are passed in data >=20 > and do work=C2=A0referencing the class vars. >=20 >=20 >=20 >=20 >=20 > I want to decorate these methods, the decorator needs access to the class= vars, so I thought >=20 > about making the decorator its own class and allowing it to accept args. >=20 >=20 >=20 >=20 >=20 > I was hoping to do all the work on=C2=A0in_data from within the decorator= , which requires access >=20 > to several=C2=A0MyClass vars. Not clear on the syntax/usage with this app= roach here, any guidance >=20 > would be greatly appreciated! >=20 >=20 >=20 > My guess is that you don't quite 'get' decorators yet (since I remember s= imilar types of questions when trying to learn them myself). =C2=A0Decorato= rs execute when the class type itself is being built (e.g., when a module i= s first imported at runtime). =C2=A0So decorators will never take instance = variables as arguments (nor should they, since no instance can possibly exi= st when they execute). =C2=A0Bear in mind, a decorator should take a callab= le as an argument (and any number of 'static' parameters you want to assign= it), and return another callable. >=20 >=20 >=20 > I provide an example decorator using the format the I typically adopt bel= ow (where the decorator is a simple function, not a class): >=20 >=20 > def my_decorator(fcn): I might add default parameters here if I am programming=20 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=20 or c/c++ in the future. > =C2=A0 =C2=A0""" Decorator for a function """ >=20 > =C2=A0 =C2=A0def new_fcn(self, *args, **kwargs): > =C2=A0 =C2=A0 =C2=A0 """ This is the new function that we will return. ""= " > =C2=A0 =C2=A0 =C2=A0 # You can access any instance variables here > =C2=A0 =C2=A0 =C2=A0 returnval =3D fcn(self, *args, **kwargs) >=20 > =C2=A0 =C2=A0 =C2=A0 # Do anything else here with instance variables > =C2=A0 =C2=A0 =C2=A0 return returnval # or any other return value you wan= t > =C2=A0 =C2=A0 > =C2=A0 =C2=A0return new_fcn >=20 >=20 > Notice here I define a new_fcn callable function that takes self and an a= rbitrary argument/keyword-argument list, and I return this function (which = does not get called) to replace the function I passed in. =C2=A0You can use= instance variables inside new_fcn since new_fcn is called by instances of = MyClass. =C2=A0This is a very simple type of decorator, but hopefully helps= illustrate what decorators are. =C2=A0There is a particularly good thread = on SO with information about decorators here:=C2=A0http://stackoverflow.com= /questions/739654/understanding-python-decorators >=20 >=20 >=20 > Hope this helps, > Jason