Hi cootetom,

For decorators you must have a look at the excellent articles/tutorials:
        http://www.artima.com/weblogs/viewpost.jsp?thread=240808 
        http://www.artima.com/weblogs/viewpost.jsp?thread=240845

It covers decorators with (what you want to do) and without parameters.
Regards
Chris

-----Original Message-----
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of cootetom
Sent: 04 April 2011 15:31
To: Django users
Subject: Python Decorators

Hi all, not exactly a django question but here goes... I'm trying to
implement a decorator with one positional argument and one keyword
argument. My wrapper function gets the positional argument just fine
but the keyword argument throws an error "NameError: name 'as_string'
is not defined."

Code is;

def widget(widget_switch, as_string=False):
    def decorator(func):
        def wrapper(*args, **kwargs):
            # logic in here can access the value of 'widget_switch'
but 'as_string' is not defined?
            return func(*args, **kwargs)
        return wrapper
    return decorator

So if I decorate a view function with "@widget('some_widget',
as_string=True)" then the wrapper function is unable to use
'as_string'. I thought this was how python closer worked?

Any ideas?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to