On 1/29/2023 4:15 PM, elvis-85...@notatla.org.uk wrote:
On 2023-01-28, Louis Krupp <lkr...@invalid.pssw.com.invalid> wrote:
On 1/27/2023 9:37 AM, mutt...@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
>>>

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to