I have been trying to understand in general how the "as" keyword in python works so far and have learnt 3 cases for it
Case 1: import foo as bar
Case 2: with foo() as bar:
Case 3: except Exception as e:
I understand how the first 2 cases work, case 1 works assigning a package name to a different name, case 2 works using __entry__ and __exit__ methods within a class (say for ensuring resources close), where __entry__ returns the class bar is being assigned to.
However, case 3 seems different, I was looking through the docs which didn't seem to help and couldn't find much of an explanation anywhere. In this case Exception.args works differently to e.args and I have been trying to work out how this works behind the scenes, i.e. whatever methods may be called, or why it behaves differently
Exceptionis the exception class, andeis the exception object.as ebasically assigns the nameeto the instance of the exception that has been caught, so that you can refer to it (e.g. for logging) within theexceptblock.