Skip to main content
 首页 » 编程设计

python函数返回函数

2025年05月04日18rubylouvre

<分区>

我知道 functools 中的 partial 函数,但是在一般的 python 程序(不是:Haskell、Erlang、Clojure 等)中编写函数有多普遍Python 中的返回函数?

例如:

>>> def returnfunk(xs): 
...     return lambda x: list(filter(lambda y: x == y, xs)) 
...  
>>> fn = returnfunk(["cat", "dog", "horse"]) 
>>>  
>>> (fn("cow") == []) == True 
True 
>>> (fn("cat") == ['cat']) == True 
True 
>>>  
>>> list(filter(fn, ["zebra", "elephant", "dog", "parrot", "cat"])) 
['dog', 'cat'] 

它是针对真实(python)世界还是更多针对爱好、学术、兴趣?