evaluation question

Thomas Passin list1 at tompassin.net
Sun Jan 29 23:57:51 EST 2023


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). 
  Other common constructs that return nothing are append(), sort(), and 
add().  It can be easy to forget this and write

l2 = l1.sort()  # l2 == None

OTOH, you can (by slightly abusing the lambda) use this behavior to make 
a lambda expression print what it's receiving:

 >>> y = lambda x: print(f'Got {x}') or x**2
 >>> z = y(3)
Got 3
 >>> z
9
 >>>



More information about the Python-list mailing list