views:

276

answers:

3

What is the difference between ',' and 'as' in except statements, eg:

try:
    pass
except Exception, exception:
    pass

and:

try:
    pass
except Exception as exception:
    pass

Is the second syntax legal in 2.6? It works in CPython 2.6 on Windows but the 2.5 interpreter in cygwin complains that it is invalid.

If they are both valid in 2.6 which should I use?

+3  A: 

the "as" syntax is the preferred one going forward, however if you're code needs to work with older Python versions (2.6 is the first to support the new one) then you'll need to use the comma syntax.

Alex Gaynor
+5  A: 

http://www.python.org/dev/peps/pep-3110/

Summary: In Python 2.6+, use the as syntax, since it is far less ambiguous.

Amber
It would be worth adding an extract or summary
Will
+1  A: 

Yes it's legal. I'm running Python 2.6

try:
    [] + 3
except Exception as x:
    print "woo hoo"

>>> 
woo hoo

>>>
inspectorG4dget