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: global name 'acc_name' is not defined.

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.

解决方案

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!

try this:

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

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.

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)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值