Uses of Python's “from” keyword?

前端 未结 6 630
一整个雨季
一整个雨季 2021-01-11 11:53

Are there any other uses for Python\'s \"from\" keyword aside from import statements?

6条回答
  •  伪装坚强ぢ
    2021-01-11 12:41

    With the finalization of PEP 3134, the from keyword can be used when an exception is generated (raise) as a consequence of catching an exception in a try-except block.

    try:
        
    except  as e:
        raise  from e
    
    

    The keyword from allows to keep track of the caught exception e in the new excaption raised. The exception e will be stored in the attribute __cause__ of the new exception.

提交回复
热议问题