Python: Have an action happen within every single function of a python file
Question: Sorry the title isn't very clear but it is kind of hard to explain. So I am
wondering how you can have a certain action happen within every single
function of a python file. I want a user to type 'paper' inside any function
in the entire python file and I cannot figure out how to do it. Here is an
example:

    
    
    def a():
        raw_input()
        print "Test"
    
    def b():
        raw_input()
        print "Test 2"
    

How can I have it setup so a user can type 'paper' in these two functions
(realistically more than 30+) and then it would print the statement "you have
a paper"

Thank you so much for the help!


Answer: If you have something you want to apply to every function, you should use a
decorator. I'm not exactly sure what you're trying to accomplish, but this
should demonstrate:

    
    
    >>> def youhaveapaper(function):
        def wrapper(*args):
            response = input('whatchu got?\n>>> ')
            if response == 'paper':
                print('you have a paper')
            return function(*args)
        import functools
        functools.update_wrapper(wrapper, function)
        return wrapper
    
    >>> @youhaveapaper
    def somefunction(x):
        """return the square of x"""
        return x**2
    
    >>> y = somefunction(5)
    whatchu got?
    >>> paper
    you have a paper
    >>> y
    25
    

As you can see, `somefunction` did not need to be changed, it just needed
`@youhaveapaper` placed before the definition.

