Python Decorators: background

Viewed 100

Through searching I have found many tutorials that explain how it works but not why it works that way. I know now how to implement all the 3 main use cases:

  • 1st: no arguments involved
  • 2nd: the function that I want to decorate has arguments
  • 3rd: the decorator itself has arguments

I find the first and second use case very intuitive: Python sees a decorator and it says "okay decorator, here's your function - hands over the argument -, now gimme something that I can use instead".

1st use case: "You gave me a function that takes no arguments? Works, thanks".

2nd use case: "You gave me a function that just takes *args and **kwargs? Works, thanks

Now the 3rd use case with example code:

def mydecorator(msg=""):
    def wrapper2(func):
        def wrapper(*args, **kwargs):
            func(*args, **kwargs)
            print "You know nothing about Python"
            print msg
        return wrapper
    return wrapper2

@mydecorator(msg="No, really, nothing.")
def printHello(name="Jon Snow"):
    print "Hello " + name

I don't understand the internal logic of this one. In this case python cannot say "Hey decorator, here's the function, gimme something that I can use instead". This time python says "Here's your msg argument, now gimme a function that I can use instead of the function which I haven't told you (because I gave you msg instead)". So how can the decorator now get/work with func if it gets msg instead of func?

0 Answers
Related