How does python compare functions?

后端 未结 2 1716
谎友^
谎友^ 2020-11-29 04:58

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 = la         


        
2条回答
  •  -上瘾入骨i
    2020-11-29 05:04

    __eq__, __lt__ et al. don't implement comparisons in Python, they just let you override it.

    The Python language reference states:

    Most other objects of built-in types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program.

    It's possible this is done by comparing object ids but this isn't specified by the language.

    I'm not sure what the rationale is behind making any object comparable to any other object, but it's a built-in feature of the language – the reference does mention it makes any list sortable, which makes the definition of comparing two dictionaries easier.

提交回复
热议问题