【发布时间】:2014-03-25 17:01:05
【问题描述】:
我想用 Python 写一个类似这样的 javascript 语句:
if (variable1 && variable2) {
// do something only if both variables exist
}
我正在尝试:
if not (variable1 and variable2) is None:
# do something only if both variables exist
但它不起作用...当我调试时,变量 2 未定义,但该函数仍试图运行。怎么了?
【问题讨论】:
-
为什么这些变量永远不存在?
-
未定义是什么意思?
-
它们是有时在之前的 if 语句中创建的变量,在我的例子中,变量 2 被传递并没有定义。我收到此错误:
UnboundLocalError: local variable 'variable2' referenced before assignment
标签: python