2017-12-03 121 views
1

從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' 
+5

https://docs.python.org/3.0/whatsnew /3.0.html#ordering-comparisons –

+3

我投票結束這個問題作爲題外話,因爲它不是一個問題。 – jwodder

+0

我回答了我自己的「問題」(見下文),爲了幫助他人,應該明確鼓勵這個問題。 – Shankari

回答

-1

回答我的問題:有沒有向後兼容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?

-1

無代表不存在的值 在Python3顯示錯誤消息,因爲你不型的參數列表搜索最大的項目NoneType