On Sat, Jun 04, 2022 at 07:31:58AM -0000, Steve Jorgensen wrote:
> A contrived use case:
>
> with open('document.txt', 'r') as io:
> (line1, line2, *) = io
>
with open('document.txt', 'r') as io:
line1 = io.readline()
line2 = io.readline()
It would be lovely if readlines() took a parameter to specify the number
of lines to return:
line1, line2 = io.readlines(2) # Doesn't work :-(
but alas and alack, the readlines() method has exactly the wrong API for
that. I don't know what use the hint parameter is for readlines, it
seems totally useless to me, and the wrong abstraction, counting
bytes/characters instead of lines.
Maybe we could add a keyword only argument?
line1, line2 = io.readlines(count=2)
Or there's always the explicit:
line1, line2 = [io.readline() for j in (1, 2)]
No need for new syntax for something so easy.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/CJHUXBIQ2FJL332AS22YBULI2CPF2IT4/
Code of Conduct: http://python.org/psf/codeofconduct/