"Push" attribute from decorator to decorated function in Python
<p>Some basic question from beginner. Is there a way to "push" attribute to a decorated function not using function arguments ?</p>

<pre><code>import sys
from functools import wraps


def decorator_(func):
    @wraps(func)
    def newfunc():
        func.some_attr = 'some_attr'
        func()
    return newfunc

@decorator_
def decorated_function():
    # ??? access some_attr ???
    print some_attr

def main():
    decorated_function()

if __name__ == '__main__':
    sys.exit(main())
</code></pre>

<p>Thanks in advance.</p>

<p>Depending on whether you use Python 2 or 3 you can inject variables into the <code>globals</code> of a function like this:</p>

<h3>Python 2</h3>

<pre><code>func.func_globals["some_attr"] = "some_value"
</code></pre>

<h3>Python 3</h3>

<pre><code>func.__globals__["some_attr"] = "some_value"
</code></pre>

python decorator