Python中的全局变量

时间:2020-02-23 14:42:17  来源:igfitidea点击:

在本教程中,我们将研究Python全局变量。
我们将学习如何定义全局变量,然后如何在函数中访问它们。

Python中的全局变量

全局变量在函数外部定义。
我们可以在Python程序的所有部分中访问全局变量。

Python全局变量示例

让我们看一个声明全局变量的简单示例。
然后,我们将在函数中使用此全局变量。

website = "theitroad.local"

def return_website():
  return website

print(f'My favorite website is {return_website()}')

输出:

My favorite website is theitroad.local

变量""在程序中的函数外部定义。
因此,它成为一个全局变量。

使用全局关键字访问全局变量

如果在函数内部定义了一个具有相同名称的变量,那么我们必须使用global关键字来访问全局变量。
否则,将使用局部变量值。

让我们看一个使用global关键字访问全局变量的简单示例。

website = "theitroad.local"

def print_website():
  global website
  print(f'My favorite website is {website}')

  website = 'Wikipedia.com'
  print(f'My favorite website is {website}')

print_website()

输出:

My favorite website is theitroad.local
My favorite website is Wikipedia.com

如果我们不使用global关键字,程序将抛出错误消息" UnboundLocalError:分配前引用的本地变量"。

最佳做法是避免使用与全局变量相同的名称,并避免名称冲突。

Python globals()函数快速入门

Python globals()函数返回一个表示当前全局符号表的字典。
该词典包含全局变量名称及其值。
我们可以使用此函数来获取有关所有全局变量的快速信息。

Python程序在符号表中维护程序信息。

符号表有两种类型:

  • 本地符号表–存储与程序的本地范围有关的信息。

  • 全局符号表–存储与程序的全局范围有关的信息。

Python符号表包含有关变量名称,方法,类等的详细信息。

Python globals()函数不接受任何参数。

print(globals())

输出:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a99b358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/hyman/Documents/github/theitroad/Python-3/basic_examples/python_globals_example.py', '__cached__': None}

它还会从调用此函数的位置打印python脚本。

让我们看看是否从Python解释器调用了相同的函数的输出。

$python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2016, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print(globals())
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>>

这次输出中没有__file__属性。

globals()字典中的全局变量

如前所述,全局符号表包含有关全局变量的信息。

让我们用一个简单的例子来看一下。

name = 'hyman'

print(globals())

输出:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a99b358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/hyman/Documents/github/theitroad/Python-3/basic_examples/python_globals_example.py', '__cached__': None, 'name': 'hyman'}

全局符号词典也包含" name"变量。

globals()的功能之一是我们可以修改全局字典中的变量。
由于它是字典,因此我们也可以获得特定键的值。

globals()['name'] = 'David'

print(globals()['name'])

输出:David

让我们看看globals()是否包含局部作用域变量。
为此,我们定义一个函数和其中的一些变量。

def fx():
  local = ''
  global gl
  gl = 'Global'

fx()  # calling fx() to set the variables
print(globals())

print(globals()['gl'])

输出:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a99b358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/hyman/Documents/github/theitroad/Python-3/basic_examples/python_globals_example.py', '__cached__': None, 'name': 'David', 'fx': <function fx at 0x10a9331e0>, 'gl': 'Global'}

Global

" gl"变量是全局词典的一部分,因为它的范围是全局的。
而" local"变量由于具有本地范围,因此不属于全局词典。