higher order function python walkthrough

送分小仙女□ 提交于 2019-12-04 06:14:35

问题


def tracer(fn):    
    def traced(x):
       print('Calling',fn,'(',x,')')
       result=fn(x)
       print('Got',result,'from',fn,'(',x,')')
       return result
    return traced

def fact(n):   
 if n ==0:
    return 1
 return n * fact(n-1)

new_fact = tracer(fact)  
new_fact(2)

I used this code on pythontutor.com to better understand higher order functions, but i'm having difficulty understanding why new_fact(2) in step 8 is mapped to traced? In other words, how does the traced function know the argument is 2?


回答1:


In Python, functions are objects too. When you call the tracer() function, it returns the nested traced() function; it is in fact creating a new copy of that function:

return traced

You stored that returned function object in new_fact, then called it:

>>> tracer(fact)
<function traced at 0x10644c320>
>>> new_fact = tracer(fact)
>>> new_fact
<function traced at 0x10644c398>
>>> new_fact(2)
('Calling', <function fact at 0x10644c230>, '(', 2, ')')
('Got', 2, 'from', <function fact at 0x10644c230>, '(', 2, ')')
2

You can do this with any function; store a reference to a function in another name:

>>> def foo(): print 'foo'
... 
>>> bar = foo
>>> bar()
foo


来源:https://stackoverflow.com/questions/18287260/higher-order-function-python-walkthrough

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!