python中返回结果为true_为什么在Python中0<()的计算结果为True?

我无意中输入了time.clock>> repr(time.clock)

''

>>> time.clock

True

此外:

^{pr2}$

相反:>>> 1

TypeError: can only compare to a set

问题:除了为什么之外,空的list、tuple或{}的值大于任何数字,是否有实际意义或目的?

更新:Viktor指出,默认情况下会比较内存地址:

>>> map(lambda _:(id(0),'

[(31185488L, '

279477880L), (31185488L, '

尽管看起来是顺序,这是不正确的。在Martijn Pieters指出:Without an explicit comparison operator defined, Python 2 compares by

Numbers and Type-names, with numbers having the lowest precedence.

这并没有提示调用的确切内部方法是什么。另请参阅这篇有用但inconclusive SO thread:

在IPython 2.7.5 REPL中>>> type(type(()).__name__)

Out[15]: str

>>> type(()) < 10

Out[8]: False

>>> 10 < type(())

Out[11]: True

#as described

>>> type(()) < type(())

Out[9]: False

>>> type(()) == type(())

Out[10]: True

However:

>>> 'somestr' .__le__(10)

Out[20]: NotImplemented

>>> 'somestr' .__lt__(10)

Out[21]: NotImplemented

>>> int.__gt__

Out[25]:

>>> int.__lt__

Out[26]:

>>> int.__lt__(None)

Out[27]: NotImplemented

#.....type(...), dir(...), type, dir......

#An 'int' instance does not have an < operator defined

>>> 0 .__lt__

Out[28]: AttributeError: 'int' object has no attribute '__lt__'

#int is actually a subclass of bool

>>>int.__subclasses__()

Out: [bool]

#str as the fallback type for default comparisons

>>> type(''.__subclasshook__)

Out[72]: builtin_function_or_method

>>> dir(''.__subclasshook__)

Out[73]:

['__call__',

'__class__',

'__cmp__',

'__delattr__',

'__doc__',

'__eq__',

'__format__',

'__ge__',

'__getattribute__',

'__gt__',

'__hash__',

'__init__',

'__le__',

'__lt__',

'__module__',

'__name__',

'__ne__',

'__new__',

'__reduce__',

'__reduce_ex__',

'__repr__',

'__self__',

'__setattr__',

'__sizeof__',

'__str__',

'__subclasshook__']

#IPython is subclassing 'str'

>>> str.__subclasses__()

Out[84]: [IPython.utils.text.LSString]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值