python - python如何比较函数?

标签 python sorting

这怎么不升属性错误?函数对象没有任何比较方法。它是否以某种方式使用 id() ?

fun1 = lambda:x
fun2 = lambda:x
print fun1 == fun1 # True
print fun1 == fun2 # False
print fun1 > fun2 # True
print fun1 < fun2 # False
print fun1 > 1 # True

我知道它会比较地址,但是如何比较? 拦截 __lt__、__eq__ 等是否有一些低级黑客攻击?

最佳答案

Function objects不要定义自己的比较或丰富的比较。相反,它们继承自 type objects实现rich comparisons based on the object's address in memory .

所以是的,它有效地使用地址,就像内置的 id()功能。

在 Python 3 中,函数不再是可排序的。

关于python - python如何比较函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7942346/

相关文章:

python - 在 Spyder 2.1 上安装/导入 sklearn 模块? (Ubuntu)

C++ 对指针 vector 进行排序,其中对象表示矩阵坐标

php - 如何制作按多种条件排序并在 PHP 中运行的 SQLite 查询

python - 我可以在 numpy.random.seed() 中输入哪些数字?

python - Django:如何测试 'HttpResponsePermanentRedirect'

python - Python 中的二分匹配

c++ - leetcode : can someone explain why this method works? 中的最大数

python - 类 python 的实例的 getattr 查找失败

arrays - 对包含另一个可选项目的可选项目数组进行排序

python - Pandas DataFrame 按分类列排序,但按特定类排序