higher order function python walkthrough

陌路散爱 提交于 2019-12-02 10:08:31

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