Python try…except comma vs 'as' in except

前端 未结 5 1046
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 15:14

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

try:
    pass
except Exception, exception:
    pass

and:

5条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 15:56

    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.

提交回复
热议问题