On Wed, Jul 6, 2016 at 1:12 PM, Yury Selivanov <yseliva...@gmail.com> wrote:
> This is an interesting idea, but I wonder if instead of using ‘async with’ we 
> can actually augment ‘async for’ to do the async finalization.
>
> We can add an __aiter_close__ special method which will return an awaitable.  
> In this case, ‘async for’ can always look for that method and call it at the 
> end of the iteration.  Async generators will implement the method to make 
> sure that ‘finally’ is always executed (any number of awaits in ‘finally’ is 
> OK; ‘yield’ expressions cannot be used).

I was wondering about that too. This is a fairly substantial change to
how iterators work, though -- currently, it's totally legal and
sometimes very useful to do things like

it = open("...")
# Discard header line (first non-commented line)
for line in it:
    if not line.startswith("#"):
        break
for body_line in it:
    ...

or nested for loops like:

for section_header_line in it:
    section_info = parse_section_header_line(section_header_line)
    if section_info.type == "multi-line":
        for body_line in it:
            if body_line == "-- end of section --":
                break
            else:
                ...

I guess there are a few different options in this design space -- one
could have a dedicated for+with syntax ("async forthwith"?), or an
opt-out utility like

for section_header_line in it:
    for body_line in protect_from_close(it):
        ...

where protect_from_close returns a wrapper object that intercepts
__iter_close__ and ignores it, while passing through other method
calls (__next__/send/throw).

-n

-- 
Nathaniel J. Smith -- https://vorpus.org
_______________________________________________
Async-sig mailing list
Async-sig@python.org
https://mail.python.org/mailman/listinfo/async-sig
Code of Conduct: https://www.python.org/psf/codeofconduct/

Reply via email to