1

A simple decorator that does nothing can be done like follows:

def decorator(func):
    return func # don't need to call!

@decorator
def print_word(word='python'):
    print (word)

And that will run fine:

>>> print_word()
python

But then if I wrap that in an inner function, it is called liked so:

def decorator(func):
    def _func(*args, **kwargs):
        print ("Args: %s | Kwargs: %s" % (args, kwargs))
        return func(*args, **kwargs) # <== Here I need to "call" it with ()
    return _func

@decorator
def print_word(word='python'):
    print (word)

>>> print_word('ok')
Args: ('ok',) | Kwargs: {}
ok

Why is it different in those two approaches? How does calling _func "bind it" to _func(*args, **kwargs) ?

carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • Your decorator is essentially performing the assignment `print_word = _func`. `func` is the reference to the original definition of `print_word`. Is that what you are asking about? – chepner Oct 28 '19 at 01:12
  • 1
    What is what different? And what do you mean by "bind"? – smac89 Oct 28 '19 at 01:16
  • I tried to figure out where you're confused. You might also want to read through [the reference answer on decorators](https://stackoverflow.com/a/1594484). – Karl Knechtel Oct 28 '19 at 01:51

1 Answers1

1
def decorator(func):

Because it is a decorator, we expect it to return something that can be called.

    return func # don't need to call!

Right, because func is the "something that can be called" which we will return. The decorator has no effect.

then if I wrap that in an inner function, it is called liked so

What we are doing with _func is creating a new function that will be returned by the decorator, and defining its behaviour in terms of the passed-in func. So we don't call func when the decorator is invoked; but we do call it inside the body of _func, because that's how we make func execute when _func is executed.

Why is it different in those two approaches?

I don't understand the question. What is the same about them? They are both defining a decorator, okay. They do it in completely different ways.

How does calling _func "bind it"

I don't understand what you mean about binding here and _func doesn't get called directly by name in this code. It gets called when print_word is called, because the decorator has replaced print_word with a locally created _func. (Every time the decorator is used, a separate function, with the same _func name, is made. Is that what you're stumbling on?)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153