You probably already saw the syntax but it is not common in the "except"
part:

For example, the following:

catch_everything = True

try:
  raise Exception()
except Exception if catch_everything else ():
  print("Caught!")


It is the same as:

catch_everything = True
exception_to_catch = Exception if catch_everything else ()

try:
  raise Exception()
except exception_to_catch:
  print("Caught!")


Basically the argument for the "except" keyword can be a variable and
therefore dynamic in nature.

The () is because "except" accepts a tuple of exception classes and ()
is the empty tuple so it is accepted.


On Thu, May 19, 2022 at 06:45:59AM -0000, nadav.mis...@gmail.com wrote:
Awesome! never seen this syntax used before and the above libraries are not 
using it.

Can you elaborate on the else part ? Why is it needed for the syntax to be 
correct and could you put any expression in the parenthesis ?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XFFIZ4PELOBZWIRRPBS3R5CY5IXSDWWX/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/76SKEIBWVFKQAUGDBLNMQY25ZXUIJPVV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to