Conditional decoration
MRAB
python at mrabarnett.plus.com
Mon Jun 18 18:43:47 EDT 2012
On 18/06/2012 23:16, Roy Smith wrote:
> Is there any way to conditionally apply a decorator to a function?
> For example, in django, I want to be able to control, via a run-time
> config flag, if a view gets decorated with @login_required().
>
> @login_required()
> def my_view(request):
> pass
>
A decorator is just syntactic sugar for function application after the
definition.
This:
@deco
def func():
pass
is just another way of writing:
def func():
pass
func = deco(func)
Not as neat, but you can make it conditional.
More information about the Python-list
mailing list