Suggested python feature: allowing except in context maneger

Liste des GroupesRevenir à cl python 
Sujet : Suggested python feature: allowing except in context maneger
De : dieter.maurer (at) *nospam* online.de
Groupes : comp.lang.python
Date : 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

Date Sujet#  Auteur
13 Jun 24 o Suggested python feature: allowing except in context maneger1dieter.maurer

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal