Python try…except comma vs 'as' in except

前端 未结 5 1034
佛祖请我去吃肉
佛祖请我去吃肉 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:53

    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
    

提交回复
热议问题