简体   繁体   中英

Python type comparison table

PHP有'PHP类型比较表'是否有类似Python的东西?

Python is strongly typed; such a table is not required except between basic numeric types and basic string types. For numeric types, they are coerced to long (in 2.x) or float as required. For string types, things are not as simple and so unicode (in 2.x) should be used where possible.

>>> 3 > 2.4
True
>>> 'a' < u'あ'
True
>>> u'a' < 'あ'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)

Comparison across incompatible types in 2.x will not act as you expect.

>>> 2 < '3'
True
>>> 3 < '2'
True

Comparison across incompatible types in 3.x will fail.

3>> 2 < '3'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM