gpt4 book ai didi

python - 我们可以在条件下分配吗?

转载 作者:IT老高 更新时间:2023-10-28 21:36:35 26 4
gpt4 key购买 nike

条件可以赋值吗?

例如

if (a=some_func()):
# Use a

最佳答案

为什么不试试呢?

>>> def some_func():
... return 2
...
>>> if (a = some_func()):
File "<stdin>", line 1
if (a = some_func()):
^
SyntaxError: invalid syntax

所以,不。

更新: This is possible (with different syntax) in Python 3.8

if a := some_func():

关于python - 我们可以在条件下分配吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2603956/

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