python函数返回函数

标签 python function functional-programming

<分区>

我知道 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)世界还是更多针对爱好、学术、兴趣?

最佳答案

装饰器 @classmethod@staticmethod 是两个例子。

关于python函数返回函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11728511/

相关文章:

Java 相当于 Python 的 str.strip().split()?

python - 在 PsychoPy 中记录所有按键以及等待按键?

c++ - 将数组传递给函数和在 main 中声明数组有什么区别?

scala - 编写功能代码,查找功能示例

functional-programming - type1 -> type2 -> type3 和 type1 -> (type2 -> type3) 有什么区别?

python - 在 Python 中调用具有正确参数的方法列表

python - 是否有 List 的协变可变版本?

function - Clojure - 测试函数表达式的相等性?

c - 使用 void 函数指向数组

java - 如何在 Java 中处理仿函数对象内的已检查异常