carl.hiass
carl.hiass

Reputation: 1774

Argument passing in decorated function

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) ?

Upvotes: 1

Views: 46

Answers (1)

Karl Knechtel
Karl Knechtel

Reputation: 61526

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?)

Upvotes: 1

Related Questions