evaluation question

Chris Angelico rosuav at gmail.com
Fri Feb 10 16:30:22 EST 2023


On Sat, 11 Feb 2023 at 07:36, Python <python at bladeshadow.org> wrote:
> If it's the case that you simply want to know the length of the string
> that will be printed, you can, rather than expecting the I/O function
> to tell that to you, figure it out for yourself ahead of time, e.g.
> instead of:
>
>     username = "John Smith"
>     job = "Python programmer"
>
>     # this doesn't work as desired
>     len = print(f"{username} has occupation {job}.")
>     print(len)
>     ...
>
> You would do this instead:
>
>     message = f"{username} has the occupation {job}."
>     message_length = len(message)
>     print(message)
>     print(message_length)
>     ...
>

It's worth noting WHY output functions often return a byte count. It's
primarily for use with nonblocking I/O, with something like this:

buffer = b".............."
buffer = buffer[os.write(fd, buffer):]

It's extremely important to be able to do this sort of thing, but not
with the print function, which has a quite different job.

ChrisA


More information about the Python-list mailing list