maxthon()在python2中失败 - >使用`>`不支持python3转换

从python 2.x - > python 3.x转换时,我发现内置max函数的行为发生了这种变化。我没有发现它记录在任何迁移问题的标准位置。 https://eev.ee/blog/2016/07/31/python-faq-how-do-i-port-to-python-3/ http://python-future.org/compatible_idioms.htmlmaxthon()在python2中失败 - >使用`>`不支持python3转换

我该如何解决这个问题?

的Python 2.x的:

In [1]: max([None, None, None]) 

In [2]:

Python 3.x都有:

In [3]: max([None, None, None]) 

---------------------------------------------------------------------------

TypeError

Traceback (most recent call last) <ipython-input-3-f82c85b9875c> in <module>()

----> 1 max([None, None, None])

TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'

回答:

回答我的问题:有没有向后兼容max,但它是值得商榷的,试图找到最大None真的没有意义。

我比较的条目是时间戳,我知道它们永远不会是负面的。所以我更改我的代码返回0而不是None,所以max转向max([0,0,0])哪些工作。

如果您无法对您的数据作出这样的保证,您可以改为返回-sys.maxsize

In [7]: max([-sys.maxsize, -sys.maxsize, -sys.maxsize]) 

Out[7]: -9223372036854775807

注意sys.maxsizesys.maxint,这是一个记录的变化。 What is sys.maxint in Python 3?

回答:

无代表不存在的值 在Python3显示错误消息,因为你不型的参数列表搜索最大的项目NoneType

以上是 maxthon()在python2中失败 - &gt;使用`&gt;`不支持python3转换 的全部内容, 来源链接: utcz.com/qa/259376.html

回到顶部