local variable referenced before assignment for decorator
Question: I'm using decorator with the functional syntax as it's described
[here](http://www.python.org/dev/peps/pep-0318/#current-syntax).

I loop over a list of dict. In this loop I wrap a generic function with a
decorator taking a parameter. Then i call the wrapped function with the
current dict as parameters.

My problem is that i get an `local variable 'generic_evaluator' referenced
before assignment` error.

Here is my code:

**The concerned decorator:**

    
    
    def log(logfile_name):
        def inner_log(func):
            def wrapped(*args, **kwargs):
                import os
                ret = func()
                # business code...
            return wraps(func)(wrapped)
        return inner_log
    

And here the place where I wrap `generic_evaluator` function with the `log`
decorator.

    
    
    for evaluation in generic_evaluations:
        generic_evaluator = log(evaluation['suffix'])(generic_evaluator)
        generic_evaluator(evaluation['suffix'], evaluation['id'])
    

**EDIT**

It'll be probably more clear with this piece of code in addition:

    
    
    @tictoc
    def generic_evaluator(suffix_url, id):
        xml = etree.parse(get_resource(base_url + "/" + suffix_url + "/" + str(id)))
        links = fetch_urls_from_xml(xml)
        return links
    


Answer: If `generic_evaluator` is a global function, and you actually want to rebind
the global name `generic_evaluator`, declare this name as global:

    
    
    global generic_evaluator
    

If you don't want to rebind the global name, use a different local name and
initialise it:

    
    
    local_generic_evaluator = generic_evaluator
    

[This answer](http://stackoverflow.com/questions/9264763/unboundlocalerror-in-
python/9264845#9264845) gives an explanation of what's going on.

