python如何比较函数? [英] How does python compare functions?

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

问题描述

这怎么不引起属性错误?函数对象没有任何比较方法.它会以某种方式使用id()吗?

How come this doesn't rise Attribute error? function object doesn't have any of the comparison methods. Does it use id() somehow?

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__等是一些低级黑客吗?

I understand that it compares addresses, but how? Is it some low level hack in to intercept __lt__, __eq__ etc. ?

推荐答案

函数对象没有定义自己的比较或丰富的比较.取而代之的是,它们继承自类型对象,它们实现了根据对象在内存中的地址进行丰富的比较.

Function objects do not define their own comparisons or rich comparisons. Instead, they inherit from type objects which implement rich comparisons based on the object's address in memory.

是的,它有效地使用了地址,就像内置的 id()函数可以.

So yes, it effectively uses addresses just like the built-in id() function does.

在Python 3中,函数不再可排序.

In Python 3, functions are no longer orderable.

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

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