Voglio mantenere & utilizzare il valore di errore di un'eccezione in entrambi Python 2.5, 2.7 e 3.2.provare ... tranne ... come errore in Python 2.5 - Python 3.x
In Python 2.5 e 2.7 (ma non 3.x), questo funziona:
try:
print(10 * (1/0))
except ZeroDivisionError, error: # old skool
print("Yep, error caught:", error)
In Python 2.7 e 3.2 (ma non in 2.5), questo funziona:
try:
print(10 * (1/0))
except (ZeroDivisionError) as error: # 'as' is needed by Python 3
print("Yep, error caught:", error)
Is esiste un codice per questo scopo che funzioni in entrambi 2.5, 2.7 e 3.2?
Grazie
si fa a non aggiornare il vostro 2.5 codice per almeno 2,7 e preferibilmente 3? – Ben
'except (ZeroDivisionError) come errore:' funziona bene in python 2.7 http://ideone.com/KfHBs –
Python 3 non è espressamente compatibile all'indietro. Perché limitarti a un sottoinsieme limitato di sintassi supportato da entrambe le lingue? – Cameron