python - UnboundLocalError: Decorator with default parameters -


here decorator code. i'm getting unboundlocalerror reason 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, thought maybe it's better post here. might missing something.

thanks.

the compiler can't determine schema's proper scope. either use nonlocal schema (3.x) within _f() or change definition of _f() slightly:

def _f(self, schema=schema, *args, **kwargs): 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -