简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM