gpt4 book ai didi

python - 导入模块中的全局变量

转载 作者:行者123 更新时间:2023-11-28 22:25:42 24 4
gpt4 key购买 nike

我有以下代码:一个.py

import two as t 

t.test1()

t.test2()

two.py

class test_class():
def __init__(self,arg):
print("created obj name is {}".format(arg))
self.name = arg

non_global ="initial global"

obj = test_class("test name")



def test1():
print("\t in test 1")
print (obj.name)
global non_global # wont work without global keyword, value still refers to "initial value"
print(non_global) # initial value
obj.name = "changed"
#non_global = "changed global"


def test2():
print("\tin test 2")
print(obj.name)
print(non_global)

结果是:

created obj name is test name
in test 1
test name
initial global
in test 2
changed
changed global

如果我将 test1() 更改为:

def test1():
print("\t in test 1")
print (obj.name)
#global non_global # wont work without global keyword, value still refers to "initial value"
print(non_global) # initial value
obj.name = "changed"
non_global = "changed global"

我在打印行上收到错误 UnboundLocalError: local variable 'non_global' referenced before assignment

如果我评论 non_global = "changed global" 错误就会消失。

我的问题是:

为什么 non_global 而不是 obj?我在 python 3.5

最佳答案

主要区别在于non_global是变量赋值,obj.name是属性赋值。

函数内的变量赋值使该变量成为局部变量,由于 Python 不会在局部范围以外的任何地方查找它,因此 print(non_global) 失败,因为它不是尚未在该范围内定义。这适用于 global 语句,因为通过使用 global 您告诉 Python 不要将它视为局部变量,因此可以从全局范围获取它的值。

在解析函数体时决定变量是否进入局部变量,因此在实际声明之前何时尝试使用它会在运行时出错。

obj.name 另一方面,基本上使用简单的 LEGB lookup 搜索 obj然后用指定的值设置 name 属性。

同样,您也可以在函数体内更新全局可变数据结构(列表、字典等),而无需使用 global

除了 = 之外,*=+= 等增强赋值也可以使变量成为局部变量。

a = []

def func1():
a += ['foo']

def func2():
a.extend(['bar'])

def func3():
a[0] += 'spam'


func1() # Fails with UnboundLocalError: local variable 'a' referenced before assignment

func2() # works fine

func3() # Also works fine because a[0] is not a variable.
# We are telling Python to look for `a` and fetch its 0th item
# and concatenate 'spam' to it.

print(a) # prints ['barspam']

字节码反汇编也可以帮助您指出差异:

>>> import dis
>>> dis.dis(func1)
45 0 LOAD_FAST 0 (a)
3 LOAD_CONST 1 ('foo') # Load a local variable name foo
6 BUILD_LIST 1
9 INPLACE_ADD
10 STORE_FAST 0 (a) # Store local variable named foo
13 LOAD_CONST 0 (None)
16 RETURN_VALUE
>>> dis.dis(func2)
48 0 LOAD_GLOBAL 0 (a) # Load from global scope
3 LOAD_ATTR 1 (extend)
6 LOAD_CONST 1 ('bar')
9 BUILD_LIST 1
12 CALL_FUNCTION 1
15 POP_TOP
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
>>> dis.dis(func3)
51 0 LOAD_GLOBAL 0 (a) # Load from global scope
3 LOAD_CONST 1 (0)
6 DUP_TOPX 2
9 BINARY_SUBSCR
10 LOAD_CONST 2 ('spam')
13 INPLACE_ADD
14 ROT_THREE
15 STORE_SUBSCR
16 LOAD_CONST 0 (None)
19 RETURN_VALUE

# Names of local variables or arguments of each function obtained through its code object
>>> func1.__code__.co_varnames
('a',)
>>> func2.__code__.co_varnames
()
>>> func3.__code__.co_varnames
()

相关:Why am I getting an UnboundLocalError when the variable has a value?

关于python - 导入模块中的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45357838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com