On this page of the official Python documentation about decorators and 'compound statements', this sample of code is given:
[Begin of sample]
@f1(arg)
@f2
def func(): pass
is equivalent to:
def func(): pass
func = f1(arg)(f2(func))
[End of sample]
However, I don't understand 'func = f1(arg)(f2(func))'. I've never seen a call like this before, and I have no idea of what it means. Is it multiple calls using different arguments, each pair of brackets containing one argument ('arg' in the first, 'f2(func)' in the second), or is it something else? I need to understand this in order to be able to study decorators. Also, does this work in Python 2.7? One of the sites I consulted on decorators was about Python 3.2.
What you need to know for this is that functions are first class objects - you can pass functions around just like ints or strs.
f1 returns a function, so what you've posted is similar to:
def func(): pass
f1_ret = f1(arg)
f2_ret = f2(func)
func = f1_ret(f2_ret)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With