gpt4 book ai didi

python - Python 中 x = y = z 的背后是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 07:29:07 25 4
gpt4 key购买 nike

我在 python 中遇到过类似的东西,但不确定如何理解这段代码:

somedict['some_key'] = some_val = form.some_field.data

最佳答案

x = y = z 等同于 x = z; y = z(是的,按此顺序),除了 z 仅计算一次。

或者,就 Python“编译”代码而言:

>>> compiled_code = compile("x = y = z", filename='con', mode='single')
>>> import dis
>>> dis.dis(compiled_code)
1 0 LOAD_NAME 0 (z)
2 DUP_TOP
4 STORE_NAME 1 (x)
6 STORE_NAME 2 (y)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE

关于python - Python 中 x = y = z 的背后是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com