Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

The safe way of using read_lines() is:

    lines = read_lines()
    try:
        # use lines
    finally:
        lines.close()

or

    with contextlib.closing(read_lines()) as lines:
        # use lines

And it is in no way better than using "with open()" directly.

I think it is better to not add such sing to the stdlib because it only makes 
an illusion of safety but actually removes safety guards.

If we want using generators in expressions we need to add support for "with" in 
expressions and comprehensions.

    data = json.load(f) with open(path, 'rb') as f
    lines = (line.strip() for path in files with open(path) as f for line in f)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue46304>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to