evaluation question

Rob Cliffe rob.cliffe at btinternet.com
Mon Jan 30 14:00:04 EST 2023



On 30/01/2023 09:41, Muttley at dastardlyhq.com wrote:
> On Sun, 29 Jan 2023 23:57:51 -0500
> Thomas Passin <list1 at tompassin.net> wrote:
>> On 1/29/2023 4:15 PM, elvis-85792 at notatla.org.uk wrote:
>>> On 2023-01-28, Louis Krupp <lkrupp at invalid.pssw.com.invalid> wrote:
>>>> On 1/27/2023 9:37 AM, Muttley at dastardlyhq.com wrote:
>>>
>>>>>>>> eval("print(123)")
>>>>> 123
>>>
>>> Does OP expect the text to come from the eval or from the print?
>>>
>>>>>> x = print( [i for i in range(1, 10)] )
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>>>>> x
>>>    (nothing printed)
>> 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?
>
That's a fair question, or rather 2 fair questions.
There is an explanation of why the change was made at
     https://snarky.ca/why-print-became-a-function-in-python-3/
In brief: (a) the print() function is more flexible and can be used in 
expressions
                (b) Python's syntax was simplified by dropping the 
special syntax used by the print statement.
sys.stdout.write() does return the number of characters output (you 
could use this instead of print() if you need this;
remember to add a '\n' character at the end of  a line).  I guess the option
of making print() do the same either was not considered, or was 
rejected, when print was made a function.
Best wishes
Rob Cliffe



More information about the Python-list mailing list