Sujet : Suggested python feature: allowing except in context maneger
De : dieter.maurer (at) *nospam* online.de
Groupes : comp.lang.pythonDate : 13. Jun 2024, 19:44:30
Autres entêtes
Message-ID : <mailman.133.1718307111.2909.python-list@python.org>
References : 1 2
User-Agent : VM 8.0.12-devo-585 under 21.4 (patch 24) "Standard C" XEmacs Lucid (x86_64-linux-gnu)
Yair Eshel wrote at 2024-6-13 13:01 +0300:
...
I would like to suggest an alternative syntax, that will, in a sense, apply
the best of both worlds:
>
import logging
with open('sample_data/README.md') as f:
print (len(f.read()))
except FileNotFoundError:
logging.error("File not")
Are you aware that in the case of a `FileNotFoundError`
no context manager is created (the context manager is the `f`
in your code).
Why not use:
try:
with open()...
...
except FileNotFoundError:
...
I do not think that your use case requires a `with` extension.
-- Dieter