On Oct 6, 10:57 am, Matt H <[email protected]> wrote:

> One thing: Why was c only being assigned once in my original code?

Marius answered that when he wrote:

> You're doing this assignment once, when wrapping the function (i.e. at module
> import time).  You need to do this for every request, i.e. when f gets
> called.



>
> On Oct 6, 6:35 pm, Marius Gedminas <[email protected]> wrote:
>
> > On Wed, Oct 06, 2010 at 08:42:21AM -0700, Matt H wrote:
> > > Hi.
>
> > > I'm trying to set an attribute on c from a decorator, like so:
>
> > > def dec1():
> > >     def wrap_fn(f):
> > >         c.msg = 'hi'
>
> > You're doing this assignmeny once, when wrapping the function (i.e. at 
> > module
> > import time).  You need to do this for every request, i.e. when f gets
> > called.
>
> > >         return f
> > >     return wrap_fn
>
> > > @dec1
> > > def create(self):
>
> > This will fail; I assume you meant @dec1().  (And since it makes no sense
> > to have a decorator-making function with no arguments, I assume you've
> > reduced your real code to an example and made a mistake.)
>
> > >    return render('create_tmpl')
>
> > Correct code would be something like
>
> >     def dec1():
> >         def wrap_fn(f):
> >             def wrapped_fn(*args, **kw):
> >                 c.msg = 'hi'
> >                 return f(*arg,s **kw)
> >             return wrapped_fn
> >         return wrap_fn
>
> >     @dec1()
> >     def create(self):
> >         return render('create_tmpl')
>
> > Even more correct code would usehttp://pypi.python.org/pypi/decorator,
> > like Wyatt suggested:
>
> >     def dec1():
> >         @decorator
> >         def wrap_fn(f, *args, **kw):
> >             c.msg = 'hi'
> >             return f(*arg,s **kw)
> >         return wrap_fn
>
> >     @dec1()
> >     def create(self):
> >         return render('create_tmpl')
>
> > HTH,
> > Marius Gedminas
> > --
> > You have moved the mouse. NT must be restarted for the changes to take 
> > effect.
>
> >  signature.asc
> > < 1KViewDownload

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to