Python中的“未定义全局名称"概念 [英] 'Global name not defined' concept in python

查看:178
本文介绍了Python中的“未定义全局名称"概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Python,并且已经阅读了有关此错误的博客,但是我仍然无法清楚地理解这一点.这是我正在编写的代码的片段:

I am learning Python and have read blogs about this error, but I am still not able to understand this clearly yet. This is a snippet of the code I am writing:

for i in included:
    global signs,accounts, regions
    global sign_name, acc_name, rg_name
    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)

NameError:未定义全局名称'acc_name'.

NameError: global name 'acc_name' is not defined.

我正在使用Python 2.7 如果有人可以帮助我理解Python中全局名称和初始化的概念,那就太好了. 预先感谢.

I am using Python 2.7 If anyone can help me understand the concept of global names and initiation in Python, it would be great. Thanks in advance.

推荐答案

不用担心:)欢迎使用Python!之所以抛出该错误,是因为它正在寻找不存在的全局变量,而它不存在的原因是因为您没有达到if type == "accounts"条件!

No worries :) welcome to Python! It's throwing that error because it's looking for a global variable that doesn't exist -- and the reason it doesn't exist is because you're not hitting the if type == "accounts" condition!

尝试一下:

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)

这将清除错误,至少可以让您看到可能还会弹出哪些其他错误:)

That will clear the error and at least let you see what other bugs may be popping up :)

我还要指出,因为我敢肯定您会收到其他人的来信,因此您没有理由在这种情况下声明全局变量.最初说找不到全局变量"是因为在输入global关键字之前,它没有在if语句上触发,因此首先它检查了locals()变量,但没有找到它,搜索globals()变量,但没有发现它被踢和出现错误.

I'll also point out, as I'm sure you will hear from others, there's no reason for you to be declaring global variables in this context. It was initially saying "can't find global variable" because before you put in the global keywords, it wasn't triggering on the if statement and so first it checked the locals() variables, and not finding it, searched for the globals() variables, and not finding it kicked and error.

您可以删除global变量,这样可以正常工作:

You can remove the global variables and it will work fine like so:

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_.原因是typebuiltin Python函数,如果将type用作变量,则会无意中对该内置名称起别名.

Another quick note, never the type as a variable... use type_ instead. The reason is type is a builtin Python function and if you use type as a variable you are accidentally aliasing that builtin name.

最后,只是要清理脚本多一点:

Finally, just to clean up the script a little more:

# 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))

这篇关于Python中的“未定义全局名称"概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆