我想保留&在Python 2.5,2.7和3.2中都使用異常的錯誤值。try ... except ... as Python 2.5中的錯誤 - Python 3.x
在Python 2.5和2.7(但不3.x中),這個工程:
try:
print(10 * (1/0))
except ZeroDivisionError, error: # old skool
print("Yep, error caught:", error)
在Python 2.7和3.2(而不是2.5),這個工程:
try:
print(10 * (1/0))
except (ZeroDivisionError) as error: # 'as' is needed by Python 3
print("Yep, error caught:", error)
是有什麼代碼可以在2.5,2.7和3.2下運行?
感謝
你能不能升級2.5代碼至少2.7,最好是3? – Ben
'除了(ZeroDivisionError)作爲錯誤:'在Python 2.7中工作正常http://ideone.com/KfHBs –
Python 3明確不向後兼容。爲什麼只限於兩種語言支持的有限語法子集? – Cameron