Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42041
| References | <assp.07983ebbdb.b3538dddac424953a000b1222309380f@exch.activenetwerx.com> |
|---|---|
| Date | 2013-03-27 16:33 -0400 |
| Subject | Re: Decorator help |
| From | Jason Swails <jason.swails@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3827.1364416391.2939.python-list@python.org> (permalink) |
[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 | Next — Next in thread | Find similar | Unroll 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