• Re: Suggested python feature: allowing except in context maneger

    From Barry Scott@21:1/5 to All on Thu Jun 13 14:40:36 2024
    On 13 Jun 2024, at 11:01, Yair Eshel via Python-list <python-list@python.org> 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dieter.maurer@online.de@21:1/5 to Yair Eshel on Thu Jun 13 19:44:30 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to dieter.maurer@online.de on Fri Jun 14 08:49:48 2024
    On 13Jun2024 19:44, dieter.maurer@online.de <dieter.maurer@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 <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Yair Eshel on Sat Jun 15 12:56:35 2024
    On 14Jun2024 09:07, Yair Eshel <guruyaya@gmail.com> 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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Albert-Jan Roskam@21:1/5 to All on Sun Jun 16 23:41:52 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From j@21:1/5 to Cameron Simpson via Python-list on Mon Jun 17 12:35:54 2024
    On 2024-06-13 23:49, Cameron Simpson via Python-list wrote:
    On 13Jun2024 19:44, dieter.maurer@online.de <dieter.maurer@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 <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)