简体   繁体   English

声明全局变量

[英]Declaring global variables

I have 2 question regarding global variables: 关于全局变量,我有两个问题:

  1. Why can't I declare a list as a global variable as so: global list_ex = [] ? 为什么不能这样将列表声明为全局变量: global list_ex = []
  2. I have already defined a global variable that I am trying to use in a function, but can't: 我已经定义了要在函数中使用的全局变量,但是不能:

     global column def fx_foo(cols): common = set(cols).intersection(set(column)) #Error Here!! 

When I try to access column inside the function, I get an error: 当我尝试访问函数内的列时,出现错误:

NameError: global name 'column' is not defined NameError:全局名称'列'未定义

You are not using global correctly. 您没有正确使用global You don't need to use it at all . 你并不需要在所有使用它。

You need to actually set a global column variable, there is none right now. 您实际上需要设置一个全局column变量,现在没有。 global does not make the variable available. global不使变量可用。 Just create a global column first: 只需首先创建一个全局column

column = []

then refer to it in your function. 然后在您的函数中引用它。 That is what the NameError exception is trying to tell you; 那就是NameError异常试图告诉您的; Python cannot find the global column variable, you didn't assign anything to the name so it doesn't exist. Python无法找到全局column变量,您没有为该名称分配任何内容,因此该名称不存在。

You only need to use global if you want to assign to a global column in your function : 仅在要分配函数中的全局column时才需要使用global

def somefunction():
    global column
    column = [1, 2, 3]

Here the global keyword is needed to distinguish column from a local variable in the function. 在这里,需要global关键字来区分column和函数中的局部变量。

Compare: 相比:

>>> foo = 1
>>> def set_foo():
...     foo = 2
...
>>> set_foo()
>>> foo
1

to

>>> foo = 1
>>> def set_foo():
...     global foo
...     foo = 2
...
>>> set_foo()
>>> foo
2

The first form only set a local variable, the second form set the global variable instead. 第一种形式仅设置局部变量,第二种形式则设置全局变量。

The keyword global means you are explicitly using a variable declared outside the scope of a function. 关键字global表示您正在显式使用在函数范围之外声明的变量。

Your variable must be declared normally: 您的变量必须正常声明:

column = []

and declared global in the function that uses it 并在使用它的函数中声明为global

def fx_foo(cols):
    global column
    common = set(cols).intersection(set(column))

It is used to allow python to distinguish between new local variables and reused global variables. 它用于允许python区分新的局部变量和重用的全局变量。

this will work : 这将工作:

column =[]

def fx_foo(cols):
    global column
    common = set(cols).intersection(set(column)) 

but this will work even without global as column will be considered as nonlocal here 但这即使在没有global情况下也可以使用,因为这里的column将被视为非本地的

column =[]
def fx_foo(cols):
    common = set(cols).intersection(set(column)) 

I think It is more interesting to assign data to column if You want to display global feature (as You can use column from nonlocals without global declaration if you don't assign anything to it) 我认为,如果要显示global功能,则将数据分配给column会更有趣(因为如果不分配任何内容,则可以使用非本地列而不使用全局声明)

column =[]
def fx_foo(cols):   
    global column     
    column = set(cols).intersection(set(column)) 

or 要么

def fx_foo(cols): 
    column =[]          
    global column 
    column = set(cols).intersection(set(column)) 

Global variables need to be declared global inside of the function not on the outside. 全局变量需要在函数内部而不是外部声明为全局。 A variable declared outside a function is by default global. 默认情况下,在函数外部声明的变量是全局变量。

Functions don't need to declare a variable as global if they only need to access it. 如果只需要访问变量,函数就不需要将其声明为全局变量。 The global declaration is just if you need to modify the global variable. 全局声明仅在您需要修改全局变量时使用。

Therefore, in your case, I suggest that you look into what column actually is when you're passing it to the function, because I think the problem may have something to do with that. 因此,在您的情况下,建议您在将其传递给函数时查看实际的列,因为我认为问题可能与此有关。

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

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