当前位置:   article > 正文

python global name not defined_python中的“Global name not defined”概念

global name multipartencoder is not defined

不用担心:)欢迎来到Python!它抛出这个错误是因为它正在寻找一个不存在的全局变量——而它不存在的原因是因为您没有达到if type == "accounts"条件!

试试这个:for i in included:

global signs,accounts, regions

global sign_name, acc_name, rg_name

regions = "no region yet"

acc_name = "no acc_name yet"

if type == "regions"

regions = i

rg_name = regions['data']['region']

if type == "accounts"

accounts = i

acc_name = accounts['data']['account']

print("Stopping account " + acc_name + " in region " + rg_name)

这将清除错误,至少让您看到可能出现的其他错误:)

我还将指出,我相信您会从其他人那里听到,您没有理由在这个上下文中声明全局变量。它最初的意思是“找不到全局变量”,因为在您输入global关键字之前,它没有触发if语句,因此它首先检查locals()变量,没有找到它,搜索globals()变量,没有发现它被踢出并出错。

您可以删除global变量,它的工作原理如下:for i in included:

regions = "no region yet"

acc_name = "no acc_name yet"

if type == "regions"

regions = i

rg_name = regions['data']['region']

if type == "accounts"

accounts = i

acc_name = accounts['data']['account']

print("Stopping account " + acc_name + " in region " + rg_name)

另一个简短的提示,永远不要将type作为变量。。。改用type_。原因是type是一个builtinPython函数,如果使用type作为变量,则会意外地给该内置名称起别名。

最后,再把剧本整理一下:# only use "i" when you're using numbers, otherwise just call it

# the name of the data you're using :)

for account_data in included:

regions = "no region yet"

acc_name = "no acc_name yet"

if type_ == "regions"

rg_name = account_data['data']['region']

if type_ == "accounts"

acc_name = account_data['data']['account']

# here's an example of Pythonic string formatting :)

print("Stopping account {} in region {}".format(acc_name, rg_name))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/619963
推荐阅读
相关标签
  

闽ICP备14008679号