返回值和范围解析的Python iFelfelse表达式

2024-04-18 01:43:00 发布

您现在位置:Python中文网/ 问答频道 /正文

问题1

在rust中,我可以编写如下代码:

let foo = if ... {
  1
} else if ... {
  2
} else {
  3
};

这里,foo被分配了if-elseif-else表达式的返回值

类似的东西在Python中可能吗

问题2

这个python代码中的外部变量“foo”更新了吗

foo = "hello"

if cond1:
   foo = "world"
else:
   pass

# if cond1 is true, what is the value of foo now? "hello" or "world"