Here is my decorator code. I'm getting UnboundLocalError for some reason but I couldn't find it.
>>> def validate(schema=None):
def wrap(f):
def _f(*args, **kwargs):
if not schema:
schema = f.__name__
print schema
return f()
return _f
return wrap
>>> @validate()
def some_function():
print 'some function'
>>> some_function()
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
some_function()
File "<pyshell#22>", line 4, in _f
if not schema:
UnboundLocalError: local variable 'schema' referenced before assignment
>>>
So, I thought maybe it's better to post here. I might be missing something.
Thanks.
The compiler can't determine schema
's proper scope. Either use nonlocal schema
(3.x) within _f()
or change the definition of _f()
slightly:
def _f(self, schema=schema, *args, **kwargs):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With