This example is taken from Beazley, Python Essential Reference 4e, pg:101.
How is he doing:
func(*args, **kwargs)
where 'func' is the square-function which takes 1 argument. Earlier in the chapter he sqawks about how the position and number of arguments must match in a call/definition or a TypeError would be raised.
Also,
@trace
def square(x):
...
square = trace(square)
trace returns 'callf' so this is equivalent to writing: square = callf
which is fine because since square refers to a new-function-object, you can
call it with *args, **kwargs
. But, then in callf
he does func(*args...
Given that we just made 'square' refer to some other object, how is the original square accessible inside? What mechanism is coming into play?
@trace
def square(x):
return x*x
enable_tracing = True
if enable_tracing:
debug_log = open("debug.log","w")
def trace(func):
if enable_tracing:
def callf(*args,**kwargs):
debug_log.write("Calling %s: %s, %s\n" %
(func.__name__, args, kwargs))
r = func(*args,**kwargs) #????????
debug_log.write("%s returned %s\n" % (func.__name, r))
return r
return callf
else:
return func