I have a decorator like this:
def region_required(view_func):
def new_view(request, ctx = {}, *args, **kw):
import pdb; pdb.set_trace()
ctx['regions'] = Region.objects.all()
return view_func(request, context=ctx, *args, **kw)
return new_view
I uses it to decorate view functions.
Today I noticed something really strange, the ctx (context) argument sometimes have data that belongs to last HTTP request. Then I narrowed it down to this decorator and found that somehow ctx has value when it should be {} here.
So I set a break point, and goes one level up, then I found its caller does NOT pass anything to it at all.
How can an argument has value that is neither passed in by caller nor default value?
You’re using a mutable argument as a default parameter. Every time it’s called without that argument, it will include the one you provided, including prior modifications. Use
Noneas the default value instead, check for it, and if itis None, assign the empty dict in the body of the function.