Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do print and f-string perform evaluation at different times?

Does the evaluation of variables in print and f-string happen at different times?

I expected it to be the same output for both.

def foo(x):
    x.append([5])
    return x

y, z = [1], [3]
print('y:', y, 'z:', z)
# y: [1] z: [3]

print('y:', y, 'str(y):', str(y), 'foo(y):', foo(y))  
# y: [1, [5]] str(y): [1] foo(y): [1, [5]]

print(f'z: {z} foo(z): {foo(z)} z: {z}')
# z: [3] foo(z): [3, [5]] z: [3, [5]]

Can someone explain what is happening?

like image 796
Teebu Avatar asked Oct 28 '25 05:10

Teebu


1 Answers

In both cases, the expressions are evaluated left-to-right. In the f-string case, each is converted (with str) immediately after evaluation, but in the case of print (which is a normal function call), all of them are evaluated (and so y is mutated) before any of them are converted to strings inside print. Explicitly writing str(y) in the call to print will produce the same results.

like image 121
Davis Herring Avatar answered Oct 29 '25 20:10

Davis Herring



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!