Re: Suggested python feature: allowing except in context maneger
On 2024-06-13 23:49, Cameron Simpson via Python-list wrote: On 13Jun2024 19:44, dieter.mau...@online.de wrote: Why not use: ``` try: with open()... ... except FileNotFoundError: ... ``` This is exactly what the OP was expressing dissatisfaction with. I'm -1 on the idea myself - not every combination of things needs additional syntactic support, and doing stuff like merging an `except` with a `wtih` is bound to introduce some weird corner case, complicating its semantics. I agree. If python allowed statement lambdas you could write what you want above within the language (albeit a bit clumsily). It's very handy. jan Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Suggested python feature: allowing except in context maneger
The example exception is not what bothers me. The syntax change is nowhere near as useful as `with` and context managers. They provide an excellent idiom for resource usage and release. Your suggestion complicates the `with` statement and brings only a tiny indentation reduction over the `with`-inside-`try` idiom. It brings no semantic changes or new features. I also don't see the added value. If you desperately want to get rid of an indentation level, you could use an except hook. https://docs.python.org/3/library/sys.html#sys.excepthook -- https://mail.python.org/mailman/listinfo/python-list
Re: Suggested python feature: allowing except in context maneger
On 14Jun2024 09:07, Yair Eshel wrote: Cameron, I'm not really sure I got your point. I've used the "file not found" exception as an example for a behavior typical on context managers. This could be a failure to connect to DB, or threads. It also applies to any kind of possible exception, whether cased by the context manager itself or the lines inside it. Long story short, this syntax change is as useful as context managers are The example exception is not what bothers me. The syntax change is nowhere near as useful as `with` and context managers. They provide an excellent idiom for resource usage and release. Your suggestion complicates the `with` statement and brings only a tiny indentation reduction over the `with`-inside-`try` idiom. It brings no semantic changes or new features. That is why I'm -1: the benefit is triviailly small to my eye. -- https://mail.python.org/mailman/listinfo/python-list
Re: Suggested python feature: allowing except in context maneger
Cameron, I'm not really sure I got your point. I've used the "file not found" exception as an example for a behavior typical on context managers. This could be a failure to connect to DB, or threads. It also applies to any kind of possible exception, whether cased by the context manager itself or the lines inside it. Long story short, this syntax change is as useful as context managers are On Fri, 14 Jun 2024, 01:49 Cameron Simpson, wrote: > On 13Jun2024 19:44, dieter.mau...@online.de > wrote: > >Why not use: > >``` > >try: > > with open()... > >... > >except FileNotFoundError: > > ... > >``` > > This is exactly what the OP was expressing dissatisfaction with. > > I'm -1 on the idea myself - not every combination of things needs > additional syntactic support, and doing stuff like merging an `except` > with a `wtih` is bound to introduce some weird corner case, complicating > its semantics. > > Cheers, > Cameron Simpson > -- https://mail.python.org/mailman/listinfo/python-list
Re: Suggested python feature: allowing except in context maneger
On 13Jun2024 19:44, dieter.mau...@online.de wrote: Why not use: ``` try: with open()... ... except FileNotFoundError: ... ``` This is exactly what the OP was expressing dissatisfaction with. I'm -1 on the idea myself - not every combination of things needs additional syntactic support, and doing stuff like merging an `except` with a `wtih` is bound to introduce some weird corner case, complicating its semantics. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Suggested python feature: allowing except in context maneger
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Suggested python feature: allowing except in context maneger
> On 13 Jun 2024, at 11:01, Yair Eshel via Python-list > wrote: > > I read this is a good place to give some suggestions for features in > python. Best place these days is to raise an idea on https://discuss.python.org/ Beware that this idea has come up in the past and was rejected. Barry -- https://mail.python.org/mailman/listinfo/python-list
Suggested python feature: allowing except in context maneger
Hello. I read this is a good place to give some suggestions for features in python. If not, please let me know. This is an example of a code I normally use in my everyday work: import logging try: with open('sample_data/READM.md') as f: print (len(f.read())) except FileNotFoundError: logging.error("File not found") As you can see I have 2 levels of indentation, which can add some pain to the work with the context manager. This code without context manager, can be replaced by this code: import logging try: f = open('sample_data/READM.md') as f: print (len(f.read())) except FileNotFoundError: logging.error("File not found") finally: f.close() And while this offers less indentations, it skips the usage of the very handy context manager. 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") As "with" applies the behavior of the "try / finally" it feels like a natural part of this syntax. This could provide a cleaner code. If this idea is accepted, there are several things that need to be discussed, like what to do with "else" or "finally" statement following a context manager. I'm not sure about the proper way to handle this. With hopes for an even more readable future Yair -- בברכה, יאיר אשל כהנסקי מתכנת וטכנאי מילים https://www.inspect-element.net/YouAreHere/#/start -- https://mail.python.org/mailman/listinfo/python-list