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

Reply via email to