Python Forum
Explanation of except ... as :
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explanation of except ... as :
Hi, i trying to figure out how the except work is...
in this python code
    raise Exception("a", "b")
except Exception as e:
as e
in here mean the "e" will catch/cover the Exception class isn't it ? so does it mean the "e" variable will become an object like this ?
e = Exception("a", "b")
so how could the
give an output
a a ('a',)
respectively....doesnt mean if we try to
will give us an sort of output like this
<__main__.e object at 0x0000016BF4C0F880>
When you print() an object, it will generally look for a __str__() method of that object. You should only get a print of the object's address for objects that don't have a __str__ method, or in its absence, a __repr__ method.

If you make your own class and don't provide either method, you'll get that odd default string.

Exception objects have a __str__ method, so that is what is printed. For them, it prints out the passed in information.
Fernando_7obink and Serafim like this post
print() should only print things like this "<__main__.e object at 0x0000016BF4C0F880>" if the thing being printed does not have a more natural string representation. In Python everything is an object, even numbers. You would be very disappointed if this happened.
x = 3+5
<int object at x0000016BF4C0F880>
If you write classes that have a reasonable string representation you should write dunder methods __str__ and/or __repr__ so you can get better information displayed from a print.
Serafim likes this post

Possibly Related Threads…
Thread Author Replies Views Last Post
  A better explanation of the last post Led_Zeppelin 9 1,110 Sep-20-2022, 05:08 PM
Last Post: deanhystad
  Operator meaning explanation Sherine 3 1,185 Jul-31-2021, 11:05 AM
Last Post: Sherine
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 1,842 Jan-28-2021, 05:05 PM
Last Post: buran
  .remove() from a list - request for explanation InputOutput007 3 1,503 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Explanation of the left side of this statement please rascalsailor 3 1,699 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Need explanation of one line of code Fliberty 6 2,362 Feb-18-2020, 12:50 AM
Last Post: Fliberty
  explanation of code hikerguy62 2 1,745 Aug-01-2019, 01:37 PM
Last Post: hikerguy62
  While loop explanation rdgbl 1 1,710 Dec-18-2018, 01:03 AM
Last Post: stullis
  Lamda function explanation mrcool4 4 2,697 Jul-04-2018, 10:44 AM
Last Post: mrcool4
  Deeper explanation of the "for i" please mtndewlove 6 3,339 Oct-07-2017, 09:56 PM
Last Post: mtndewlove

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020