On Mon, Apr 20, 2020 at 03:28:09PM -0700, Andrew Barnert via Python-ideas wrote:

> Admittedly, such cases are almost surely not that common, but I 
> actually have some line-numbering code that did something like this 
> (simplified a bit from real code):
> 
>     yield from enumerate(itertools.chain(headers, [''], body, [''])
> 
> … but then I needed to know how many lines I yielded, and there’s no 
> way to get that from enumerate, so instead I had to do this:

Did you actually need to "yield from"? Unless your caller was sending 
values into the enumerate iterable, which as far as I know enumerate 
doesn't support, "yield from" isn't necessary.

    for t in enumerate(itertools.chain(headers, [''], body, ['']):
        yield t
    lines = t[0]


>     counter = itertools.count()
>     yield from zip(counter, itertools.chain(headers, [''], body, [''])
>     lines = next(counter)

That gives you one more than the number of lines yielded.



-- 
Steven
_______________________________________________
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/ISJ6TSOELMQFVANV2DXFIXTYXR6WVCQR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to