Decorator

Terry Reedy tjreedy at udel.edu
Thu Feb 9 18:18:57 EST 2017


On 2/9/2017 2:03 AM, ast wrote:
> Hi
>
> In python courses I read, it is explained that
>
> @decor
> def f():
>    pass
>
> is equivalent to:
>
> def f():
>    pass
>
> f = decor(f)
>
> But that's not always true. See this code

Which is why the official docs now say 'roughly equivalent'
'''
For example, the following code

@f1(arg)
@f2
def func(): pass

is roughly equivalent to

def func(): pass
func = f1(arg)(f2(func))

except that the original function is not temporarily bound to the name func.
'''

Perhaps "There are also cases in which evaluating 'fi(arg)' before 
rather than after the def statement makes a difference." should be added.


-- 
Terry Jan Reedy




More information about the Python-list mailing list