简体   繁体   English

使用字典来引用函数内的全局变量(Python)

[英]Use a dictionary to refer to a global variable inside a function (Python)

I'm using a dictionary to refer to a global variable in Python inside a function.我正在使用字典在函数内引用 Python 中的全局变量。 I want to use the function to update the global variable.我想使用该函数来更新全局变量。 (In reality my global variables are more complicated than this.) (实际上我的全局变量比这更复杂。)

global_variable_A=5
global_variable_B=1
dictionary={'A':global_variable_A,'B':global_variable_B}

def f(x,glob):
    global global_variable_A
    dictionary[glob]+=x

f(2,'A')
print(global_variable_A)

This returns 5 rather than 7. I understand why, is there anyway to let Python know that I mean the variable dictionary[glob] to refer to the global rather than the local variable, while still referring to the variable through a dictionary?这返回 5 而不是 7。我明白为什么,无论如何让 Python 知道我的意思是变量dictionary[glob]指的是全局变量而不是局部变量,同时仍然通过字典来指代变量?

Thanks for your time, and apologies if I've missed something completely obvious.感谢您的时间,如果我错过了一些完全明显的东西,我深表歉意。

If you think you need to do this, there's a design flaw in your code.如果您认为需要这样做,则说明您的代码存在设计缺陷。 I really wouldn't recommend doing this.我真的不建议这样做。

global_a = 5

def add_to_global(key, value):
    globals()[key] += value

add_to_global("global_a", 2)
print(global_a)

Output:输出:

7

When you assign a value to a name name = 5 , you're creating a reference to 5 that you can use the identifier name to access.当您为名称name = 5分配值时,您正在创建对5的引用,您可以使用标识符name访问该引用。 Normally, if you then have some code with a narrower scope, you can either use that reference通常,如果您有一些范围更窄的代码,您可以使用该引用

def f():
    print(name)

or create a local reference using the same identifier, potentially to an unrelated value或使用相同的标识符创建本地引用,可能指向不相关的值

def g():
    name = 100
    print(name)

The global keyword allows you to instead manipulate the identifier as if you weren;t in the more narrow scope, allowing you to reassign the global name to a different reference: global关键字允许您像操作标识符一样操作标识符;不在更窄的范围内,允许您将全局名称重新分配给不同的引用:

def h():
    global name
    name = 100

h()
print(name) # 100

However, when you use a reference to create another reference, there isn't any relation ship between those two references.但是,当您使用一个引用创建另一个引用时,这两个引用之间没有任何关系。 So所以

name = 5
l = [name]

leaves us with two references to the value 5 : one from the identifier name , and one from the first position of l .给我们留下了两个对值5引用:一个来自标识符name ,另一个来自l的第一个位置。 Crucially, those two references are not related;至关重要的是,这两个参考文献并不相关。 we can change one without changing the other.我们可以改变一个而不改变另一个。

name = 6
print(l)  # [5]

One way to accomplish what you want is to use a boxed type.实现您想要的一种方法是使用盒装类型。 That means that you create an object that points to another object.这意味着您创建了一个指向另一个对象的对象。 All of your references can then point to the first object (the box) and you can freely change what it points to (the thing "inside" the box), by only updating a single reference:然后,您的所有引用都可以指向第一个对象(盒子),并且您可以自由地更改它指向的内容(盒子“内部”的东西),只需更新一个引用:

class Box:
    def __init__(self, value):
        self.value = value

box = Box(5)
l = [box]
box.value = 10
print(l[0].value)  # 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM