Hello,

I was thinking of a way to expand the current try-expect block to support
condinitial expect block. I was imagining the new syntax as

try:
    expression block
expect Exception if condition
    expression block

where the expect expression block would only run if the condition is met
else it would raise the exception, for example the following code

try:
    config = get_config()
except Exception:
    if use_default:
        config = default
    else:
        raise

would  be converted to

try:
    config = get_config()
except Exception if use_default:
    config = default

I find it very useful and also I found the same pattern in the following
packages that were installed on my system.

more_itertools/more.py:227
cloudinit/net/__init__.py:364
matplotlib/__init__.py:330
google/protobuf/internal/containers.py:126
networkx/algorithms/connectivity/edge_augmentation.py:296
twisted/internet/pollreactor.py:160
pyqtgraph/opengl/shaders.py:257
setuptools/_vendor/pyparsing.py:2191
setuptools/sandbox.py:253
pip/_internal/utils/misc.py:115
urllib3/poolmanager.py:362
mako/util.py:65
serial/serialposix.py:294
PIL/PngImagePlugin.py:335
click/core.py:1472
numpy/core/memmap.py:216
pyparsing.py:2067

Looking forward to hear some feedback,
Regards
_______________________________________________
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/XHT2JH5PCS73YC2PIX27MJSZY6XVE53V/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to