In Python 2.x, why is operator> supported between functions and int?

In Python 2.x, the following code throws an error, as expected:

>>> def a(x): return x+3 
...
>>> a+4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'function' and 'int'

However, the following is allowed:

>>> a < 4
False

Why is the + operator not defined for the function and int, but <is there an operator?

+4
source share
1 answer

In Python 2, all objects are ordered.

CPython ( Python) None , . , , ; function Zippedeedoodah. .

Python . :

<, >, ==, >=, <= != . . , . , .

Python 3 , , :

>>> def a(x): return x+3 
... 
>>> a < 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: function() < int()
+13

Source: https://habr.com/ru/post/1540873/


All Articles