Comparing None with built-in types using arithmetic operators?

后端 未结 2 1427
情话喂你
情话喂你 2020-12-09 15:25
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>         


        
2条回答
  •  醉话见心
    2020-12-09 16:15

    Some interesting quotes from http://bytes.com/topic/python/answers/801701-why-none-0-a

    In early Python, the decision was made that the comparison of any two objects was legal and would return a consistent result. So objects of different types will compare according to an ordering on their types (an implementation dependent, unspecified, but consistent ordering), and objects of the same type will be compared according to rules that make sense for that type.

    Other implementations have the right to compare an integer and None differently, but on a specific implementation, the result will not change.

    Python 3 will raise an exception on such comparisons.

    and

    The problem is the typical one; Python did not originally have a Boolean type, and the retrofit resulted in weird semantics. C has the same issue.

提交回复
热议问题