Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196249

Suggested python feature: allowing except in context maneger

From dieter.maurer@online.de
Newsgroups comp.lang.python
Subject Suggested python feature: allowing except in context maneger
Date 2024-06-13 19:44 +0200
Message-ID <mailman.133.1718307111.2909.python-list@python.org> (permalink)
References <CAAtpb8kxWfLVKYW3w3uJRyyQi-3FPxZHYfS2BLe0R6UYSitgZw@mail.gmail.com> <26219.12286.674908.902341@ixdm.fritz.box>

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Suggested python feature: allowing except in context maneger dieter.maurer@online.de - 2024-06-13 19:44 +0200

csiph-web