Python从另一个脚本获取函数内部的局部变量
我有以下内容的脚本: first.py:Python从另一个脚本获取函数内部的局部变量,python,variables,if-statement,Python,Variables,If Statement,我有以下内容的脚本: first.py: bob = 0 if bob = 0: bob + 1 from first import bob print bob second.py: bob = 0 if bob = 0: bob + 1 from first import bob print bob 如何让second.py返回1而不是0?您在first.py中的语法错误,无法执行您想要的操作。一个等号用于赋值,而不是比较,并且实际上不会将一个等号添加到变量中的结果
bob = 0
if bob = 0:
bob + 1
from first import bob
print bob
second.py:
bob = 0
if bob = 0:
bob + 1
from first import bob
print bob
如何让
second.py
返回1
而不是0
?您在first.py中的语法错误,无法执行您想要的操作。一个等号用于赋值,而不是比较,并且实际上不会将一个等号添加到变量中的结果保存
bob = 0
if bob == 0:
bob += 1
你问题的标题有点误导人。您没有任何局部变量或函数,而是两个模块中的全局变量。@Blckknght编辑!OP,运行
second.py
时是否没有出现错误?如果bob=0,我会假设会抛出一个语法错误。@jmu303:我会的,但是错误的措辞可能会使提问者误解正在发生的事情。也许所显示的代码是他们实际工作的一个过于简化的版本,并且他们在实际代码中确实有函数和局部变量