请问两个test不是都为局部变量吗?为什么会报错呢?

hying__ 2019-06-24 10:05:21
请问两个test不是都为局部变量吗?为什么会报错呢?
>>> def test():
test=90
return test

>>> def main():
test=test()
print(test)


>>> main()
[color=#FF0000]Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
main()
File "<pyshell#20>", line 2, in main
test=test()
UnboundLocalError: local variable 'test' referenced before assignment
[/color]
...全文
79 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小羽Jary 2019-06-24
  • 打赏
  • 举报
回复
下面main()里面不能用test这个变量名来接受test()这个方法的返回值,不然就会重写test,这时候test就只是一个变量不是函数所以会报错
hying__ 2019-06-24
  • 打赏
  • 举报
回复
可是为什么不在函数里操作就不会报错呢?
例如:直接
>>>test=test()

>>>test
90
就不会报错
hying__ 2019-06-24
  • 打赏
  • 举报
回复
我明白了,谢谢啦。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧