evaluation question

Chris Angelico rosuav at gmail.com
Fri Feb 10 18:10:33 EST 2023


On Sat, 11 Feb 2023 at 10:07, Thomas Passin <list1 at tompassin.net> wrote:
>
> On 2/10/2023 4:55 PM, Python wrote:
> > However, Python's print() function is more analogous to C's printf(),
> > which returns the number of characters converted for an entirely
> > different reason... It's precisely so that you'll know what the length
> > of the string that was converted is.  This is most useful with the
> > *snprintf() variants where you're actually concerned about overrunning
> > the buffer you've provided for the output string, so you can realloc()
> > the buffer if it was indeed too small, but it is also useful in the
> > context of, say, a routine to format text according to the size of
> > your terminal.  In that context it really has nothing to do with
> > blocking I/O or socket behavior.
>
> But none of that applies to the Python print() function. There are no
> buffers to overrun, no reason to know the length of the printed string,
> no re-allocating of a buffer.  It's certainly possible that one might
> want to know the actual physical length of a displayed string - perhaps
> to display it on a graphic - but now we're getting into font metrics and
> such things, and we'll be doing something more active than displaying on
> a terminal via stdout.

It's sometimes possible to have that go to stdout (maybe you want to
do columnar text or something), but yeah, you'll generally do that by
formatting first, then doing the measurement, then displaying.

ChrisA


More information about the Python-list mailing list