[zz]Python try…except comma vs 'as' in except

http://stackoverflow.com/questions/2535760/python-try-except-comma-vs-as-in-except

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

try:
   
pass
exceptException, exception:
   
pass

and:

try:
   
pass
exceptExceptionas 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?

 

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

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

 

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.

posted @ 2012-03-01 15:11  bettermanlu  阅读(403)  评论(0编辑  收藏  举报