关于何时应该在函数中使用全局变量的经验法则

The rule of thumb as to when we should use a global variable in a function

当我用函数编写游戏代码时,我经常对哪个变量是全局变量感到困惑。

我听说全球化变量不是一个很好的实践,所以我尝试通过不全球化来最小化数量,并且只全球化错误消息告诉我的变量。但是这样做很烦人,而且浪费时间。

谁能告诉我什么时候应该在函数中全局变量,

什么时候不需要?

这是我的意思的示例(函数):

import turtle
from random import randint as rd
from time import sleep
delay = 0.1

wn = turtle.Screen()
wn.setup(400,400)
wn.tracer(0)

player = turtle.Turtle('square')
player.penup()
player.goto(0,-170)

rock = turtle.Turtle('circle')
rock.shapesize(0.5,0.5)
rock.penup()
rock.goto(rd(-190,190),200)

rocks = [rock]

pen = turtle.Turtle(visible=False)
pen.penup()
pen.goto(0,150)

def go_left(): # No globalizing here
    if player.xcor() >= -170:
        player.setx(player.xcor()-10)

def go_right(): # No globalizing here
    if player.xcor() <= 170:
        player.setx(player.xcor()+10)

def move_rocks(): # No globalizing here
    for rock in rocks:
        rock.sety(rock.ycor()-rd(0,2))

def loop_rocks():
    global count # rocks not globalized here
    for rock in rocks:
        if rock.ycor() < -200:
            count += 1
            rock.goto(rd(-190,190),200)

def add_rocks():
    global count # rocks not globalized here
    if count == len(rocks) and len(rocks) <= 15:
        rock = turtle.Turtle('circle')
        rock.shapesize(0.5,0.5)
        rock.penup()
        rock.goto(rd(-190,190),200)
        rocks.append(rock)
        count = 0

def write_score():
    global time,score,high_score # pen not globalized here
    time += 1
    if time == 500:
        score += 1
        time = 0
    if score > high_score:
        high_score = score

    pen.clear()
    pen.write(f'Score: {score}   High Score: {high_score}',align='center',font=('Arial',10,'bold'))

def hit(): # No globalizing here
    for rock in rocks:
        if player.distance(rock) < 15:
            return True

def die():
    global score,rocks # player not globalized here
    sleep(1)
    for rock in rocks:
        rock.goto(300,300)
    rocks = rocks[:1]
    rocks[0].goto(rd(-190,190),200)
    player.goto(0,-170)
    score = 0

wn.listen()
wn.onkeypress(go_left,'Left')
wn.onkeypress(go_right,'Right')

score = 0
high_score = 0
count = 0
time = 0

while True:
    if hit():
        die()
    move_rocks()
    loop_rocks()
    add_rocks()
    write_score()
    wn.update()

风格规则不是语言规则。 IE。你不应该使用 eval(),但在语言中确实如此。

tell me the rule of thumb as to when we should global a variable in a function, and when it is not necessary?

何时使用和不使用 global 的规则很简单,但即使是网络上的教程也会出错。

  1. global关键字不应用于创建全局 变量。

(是的,这部分是 样式 规则。)当您在函数外部定义顶级变量时,Python 使其成为全局变量。 (您 不要 为此使用 global 关键字。)当您 赋值给函数内的变量时,Python 假定它是函数的局部变量。当您想要更改以后的假设时,您只需要 global 关键字,以便您可以从函数内重新分配 (=) 全局变量。您不需要 global 声明来检查全局变量。您不需要它来调用可能会更改其内部状态或内容的全局变量的方法:

  1. 当您想要重新分配 (=) 一个时,您只需要 global 关键字 函数内的全局变量。

global 声明用于任何重新分配全局变量的函数。它位于对变量的第一次引用、访问或赋值之前。为了简单和风格,全局语句放在函数的开头。

像 "You should never use global variables" 这样的语句是一种风格规则,适用于大多数编程语言——您可以应用它 if/when。如果你绝对做不到,也不要为此难过,只是:

  1. 评论您正确使用的所有全局变量。

全局常量不是问题:

  1. 如果全局常量是真正的常量,则它们永远不需要 global 关键字。

@juanpa.arrivillaga 的 go_left() 示例将附加值作为参数而不是全局变量,没有考虑到 go_left() 是一个 回调 并且 turtle 事件分配函数不提供额外的参数。 (他们应该,但他们没有。)我们可以使用 lambda 表达式(或来自 functoolspartial 函数)来解决这个问题,但是当使用这个顺便说一下,lambda 也不是特别好的风格,恕我直言。

@martineau 关于 "making them attributes of a class that the class' methods can access"(又名 class 变量 )的建议很好,但没有说明的是它的意思是 subclassing Turtle 或 wrapping 海龟实例与另一个 class。

我个人对可变全局变量的问题是它们在多线程世界中存在问题。

虽然这不是答案,但我只是想指出在从外部作用域/全局变量隐藏名称时要注意的另一件事。 cdlane 在 中写道

You don't need the global declaration to examine a global variable.

我认为它比这更进一步,因为你不能 使用 global 关键字,因为它是一个声明。正如 cdlane 已经说过的,它用于将局部作用域(例如函数或 class)中的变量声明为全局作用域,这样您就可以从局部作用域为这些变量赋新值。您甚至可以使用 gobal 关键字从局部范围声明 new 全局变量,尽管正如 cdlane 指出的那样,这不是一个好主意。下面是一些突出显示这些行为的代码:

a = c = 1  # define global variables 'a' and 'b' and assign both the
# value 1.


class Local:
    def __init__(self):
        print(a)  # if you only want to examine the global variable 'a',
        # you don't need any keywords
        self.declare_variables()

    def declare_variables(self):
        global a, b  # declare 'a' and 'b' as global variables, such that any
        # assignment statements in this scope refer to the global variables
        a = 2  # reassign a new value to the already existing global
        # variable 'a'.
        b = 3  # assign a value to the previously undeclared global variable
        # 'b'. you should avoid this.
        c = 4  # this does not affect the global variable 'c', since the
        # variable 'c' in this scope is assumed to be local by default.


local = Local()
print(a, b, c)  # the vaules of the gobal variables 'a' and 'b' have changed,
# while 'c' remains unaffected.

到目前为止没有什么新鲜事。但是,当您 shadowing the names 来自全局变量,但仍在同一范围内的其他地方访问全局变量时,这就会成为一个问题。

  • 如果您在尝试访问该全局变量之前声明一个隐藏全局变量名称的变量,则在该声明之后对该变量名称的所有引用都将引用局部变量多变的。我认为这可能是更糟糕的情况,因为这可能未被发现并且不会产生任何错误,但是 return 错误的结果。
  • 如果您尝试声明一个新的局部变量,或使用具有相同变量名称的 global 关键字 after 您已经在相同的变量名称中引用了该变量名称范围,它将分别导致 UnboundLocalErrorSyntaxError
def reference_global_before_local_declaration():
    print(a)  # first reference the global variable 'a'. this statement would
    # work on its own if 'a' wouldn't be redeclared to be a local variable 
    # later on.
    a = 5  # redeclare 'a' to be a local variable and assign it the value 5.


reference_global_before_local_declaration()


def reference_global_before_global_declaration():
    print(a)  # first reference the global variable 'a'. this statement would
    # work on its own if 'a' wouldn't be declared to be a global variable
    # again later on.
    global a  # declare 'a' to be a global variable again.


reference_global_before_global_declaration()


def reference_global_after_local_declaration():
    a = 'text'  # redeclare 'a' to be a local variable of type string and
    # assign it the value 'text'.
    b = a + 1  # here, 'a' was supposed to reference the global variable 
    # 'a', but is now referencing the local variable 'a' instead, due to 'a'
    # being declared in the same scope and shadowing the name of the gobal 
    # variable 'a'.


reference_global_after_local_declaration()

据我所知,避免这种情况的唯一方法是使用 globals() 函数,尽管这确实无法达到所有目的,我不推荐这样做。然而,我会推荐阅读 PEP 3104 - Access to Names in Outer Scopes,它讨论了这些类型的问题并提出了一个解决方案,但最终从未实施。

def reference_global_before_local_declaration():
    print(globals()['a'])
    a = 5


reference_global_before_local_declaration()


def reference_global_before_global_declaration():
    print(globals()['a'])
    global a


reference_global_before_global_declaration()


def reference_global_after_local_declaration():
    a = 'text'
    b = globals()['a'] + 1


reference_global_after_local_declaration()