Member-only story
Python: Cool features in python 3.11
Hi everyone, In this article I will brief about what new updates/changes are there in python 3.11 and I think you going to love them as they will make your life easier. lets dive in -
1. Exception Notes
Now you can also add your notes to the Exception. You can attach extra info to your existing exception and re raise it.
This help you to deal with it differently and it also helpful in testing packages as well(like hypothesis).
Lets see an example —
def main() -> None:
try:
raise TypeError("The Type Error")
except TypeError as error:
error.add_note("Other Information")
raise
if __name__ == '__main__':
main()
2. Exception Group
With the use of base Exception and ExceptionGroup
, You can easily aggregate/combine exceptions together.
The traceback of ExceptionGroup
contains a tree of information about all individual exceptions.
Lets see an example —
async def func1():
raise ValueError
async def func2():
raise TypeError
async def main_func():
results = await asyncio.gather(
func1(),
func2()…