Python试试…除逗号与“ as”外

问题:Python试试…除逗号与“ as”外

在except语句中“,”和“ as”之间有什么区别,例如:

try:
    pass
except Exception, exception:
    pass

和:

try:
    pass
except Exception as exception:
    pass

2.6中的第二种语法合法吗?它可以在Windows上的CPython 2.6中运行,但是cygwin中的2.5解释器抱怨它无效。

如果它们在2.6中均有效,那我应该使用哪个?

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?


回答 0

权威性文件是PEP-3110:捕获异常

摘要:

  • 在Python 3.x中,使用as要求对异常分配给一个变量。
  • 在Python 2.6+中,请使用as语法,因为它的模棱两可远,并且与Python 3.x向前兼容。
  • as不支持Python 2.5和更早版本的情况下,请使用逗号版本。

The definitive document is PEP-3110: Catching Exceptions

Summary:

  • In Python 3.x, using as is required to assign an exception to a variable.
  • In Python 2.6+, use the as syntax, since it is far less ambiguous and forward compatible with Python 3.x.
  • In Python 2.5 and earlier, use the comma version, since as isn’t supported.

回答 1

是的,这是合法的。我正在运行python 2.6

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

>>> 
woo hoo

更新:使用该as语法还有另一个原因。,正如其他人指出的那样,使用会使事情变得更加模棱两可。这就是与众不同的原因。从Python 2.6开始,有multicatch一个功能允许您在一个代码except块中捕获多个异常。在这种情况下,说起来更具表现力和Python风格

except (exception1, exception2) as e

而不是说

except (exception1, exception2), e

仍然可以工作

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

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

>>> 
woo hoo

Update: There is another reason to use the as syntax. Using , makes things a lot more ambiguous, as others have pointed out; and here’s what makes the difference. As of Python 2.6, there is multicatch which allows you to catch multiple exceptions in one except block. In such a situation, it’s more expressive and pythonic to say

except (exception1, exception2) as e

rather than to say

except (exception1, exception2), e

which would still work


回答 2

“ as”语法是首选的语法,但是,如果您的代码需要使用较旧的Python版本(第一个支持新版本的2.6是),则需要使用逗号语法。

the “as” syntax is the preferred one going forward, however if your 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.


回答 3

如果要支持所有python版本,可以使用如下sys.exc_info()函数:

try:
    a = 1/'0'
except (ZeroDivisionError, TypeError):
    e = sys.exc_info()[1]
    print(e.args[0])

(来源:http : //python3porting.com/noconv.html

If you want to support all python versions you can use the sys.exc_info() function like this:

try:
    a = 1/'0'
except (ZeroDivisionError, TypeError):
    e = sys.exc_info()[1]
    print(e.args[0])

(source:http://python3porting.com/noconv.html)


回答 4

从Python 3.7开始(不确定其他版本),不再支持’逗号’语法:

源文件exception_comma.py

try:
    result = 1/0
except Exception, e:
    print("An error occurred")
    exit(1)

exit(0)
  • $ python --version --> Python 2.7.10
$ python exception_comma.py
An error occurred
  • $ python3 --version --> Python 3.7.2
$ python3 exception_comma.py
  File "exception_comma.py", line 3
    except Exception, e:
                    ^
SyntaxError: invalid syntax

As of Python 3.7 (not sure about other versions) the ‘comma’ syntax is not supported any more:

Source file exception_comma.py:

try:
    result = 1/0
except Exception, e:
    print("An error occurred")
    exit(1)

exit(0)
  • $ python --version --> Python 2.7.10
$ python exception_comma.py
An error occurred
  • $ python3 --version --> Python 3.7.2
$ python3 exception_comma.py
  File "exception_comma.py", line 3
    except Exception, e:
                    ^
SyntaxError: invalid syntax