python函数返回函数 [英] python functions returning functions

查看:59
本文介绍了python函数返回函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 functools 中的 partial 函数,但是在一般的python程序(不包括:Haskell,Erlang,Clojure等)中,将函数编写为返回Python中的函数?

I am aware of the partial function in functools, but how common is it in general python programs (not: Haskell, Erlang, Clojure etc ) to write functions to return functions in 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)世界还是出于爱好,学术和兴趣的更多目标?

is it meant for the real (python) world or more for hobby, academic, interest?

推荐答案

修饰符 @classmethod @staticmethod 是两个示例.

这篇关于python函数返回函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆