evaluation question

Python python at bladeshadow.org
Fri Feb 10 14:59:07 EST 2023


On Mon, Jan 30, 2023 at 09:41:03AM -0000, Muttley at dastardlyhq.com wrote:
> >Because print() returns nothing (i.e., the statement x is None is True). 
> 
> I don't understand this. What was the point of the upheaval of converting
> the print command in python 2 into a function in python 3 if as a function
> print() doesn't return anything useful? Surely even the length of the 
> formatted string as per C's sprintf() function would be helpful?

Python is far from the only language that allows functions to return
nothing explicit.  Statically typed languages typically use the void
keyword or something similar to indicate this, and dynamically typed
languages often just don't explicitly return anything.  [Some
languages, like Perl, just implicitly return the value of the last
expression, or something of the sort, but some do not.] In Pascal, in
fact, there are two different types of subroutines to distinguish
between those things: functions return a value, and procedures do not.
Modern languages commonly don't provide this distinction because by
and large it is unnecessary.

Typically the reason NOT to return a value is that the function is
designed to DO something, rather than to calculate some value.
Examples might be updating the appearance of a UI widget, printing
some information to the screen, or twiddling some bits in a register
(i.e. in-place update of a value).  They don't need to return anything
because the "output" is whatever was done.  In some languages it might
be typical to return a status indicating success or failure, but in
many languages this is unnecessary; status may be returned via a
parameter provided for the purpose, or in many languages, success can
be assumed, whereas failure will raise an exception.

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)
    ...



More information about the Python-list mailing list