在python中分配错误之前引用

在python中分配错误之前引用,第1张

在python中分配错误之前引用

我认为您错误地使用了“全局”。请参阅Python参考。您应该声明不带全局变量的变量,然后在要访问全局变量的函数内部声明它

global yourvar

#!/usr/bin/pythontotaldef checkTotal():    global total    total = 0

请参阅以下示例:

#!/usr/bin/env pythontotal = 0def doA():    # not accessing global total    total = 10def doB():    global total    total = total + 1def checkTotal():    # global total - not required as global is required    # only for assignment - thanks for comment Greg    print totaldef main():    doA()    doB()    checkTotal()if __name__ == '__main__':    main()

因为

doA()
不修改 全局总数, 所以输出为1而不是11。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5562388.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存