`my_dict = (json.load(f) with open(filename.json, "r") as f)`

Would that be called a generator expression / comprehension context manager?

https://docs.python.org/3/reference/datamodel.html#context-managers

https://docs.python.org/3/library/contextlib.html

PEP 343 added the "with" statement.

Tests that would need to be extended / that may be useful references:

https://github.com/python/cpython/blob/master/Lib/test/test_with.py

https://github.com/python/cpython/blob/master/Lib/test/test_contextlib.py

https://github.com/python/cpython/blob/master/Lib/test/test_contextlib_async.py

https://github.com/python/cpython/blob/master/Lib/test/test_grammar.py#L1701
test_with_statement

https://github.com/python/cpython/blob/master/Lib/test/test_grammar.py#L1864
test_async_with

https://github.com/python/cpython/blob/master/Grammar/python.gram

Would there be any new scope issues?


On Mon, Sep 28, 2020, 11:12 AM Eric Wieser <wieser.eric+nu...@gmail.com>
wrote:

> This reminds me of a previous proposal I can't remember if I hit the list
> with, allowing
>
>     with open(filename.json, "r") as f:
>         my_dict = json.load(f)
>
> to be spelt as a single expression:
>
>     my_dict = (json.load(f) with open(filename.json, "r") as f)
>
> Obviously this would be more useful in comprehensions, but it might
> encourage people not to lazily write `json.load(open("filename.json",
> "r"))` because they want an expression, and end up leaving files open.
>
> Eric
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/QR233SIBDHWDZLKWURUHXG37BDWJHA7Z/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/22UA6LNX547HKZ66JJFWMLXUFY4CZ7YR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to