Python practical tips: use global keyword Detailed

Python practical tips: use global keyword Detailed

This article describes the use of python global keyword explain, paper sample code described in great detail, has a certain reference value of learning for all of us to learn or work, a friend in need can refer to the following:

Want a better understanding of global keyword, you must first be familiar with the concept of python global variables and local variables. The role of global keyword can declare a local variable as a global variable, look at the following example

First, the variable scope description

1, local variables

1 def a():
2 ## 菊部变量 - ,-
3 local = 1
4 print(local)
5 ## 全局无法使用,只有自己可用
6 print(lobal)

2, the global variable


1 global_str = '123'
2 def a():
3 ## 全菊变量 - ,-
4 print(global_str)
5 ## 全菊变量大家都可以用
6 print(global_str)

3, built-in scope

Variables defined in the system stationary module, as builtin predefined variables within the module. Here it is not introduced.

4, nested scopes

With relatively small, is mainly used in the closure, see chestnuts

在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784-758-214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容

1 def out():
2 ## 函数内变量,但对于下级函数就是全局变量,对于外部来说就是局部变量
3 en = 1
4 def inside():
5 print(en)
6 return en
7 return inside()
8 out()

Two, global key role

1, between the role of global function keys

1 def out():
2 ## global关键字作用
3 global en
4 en = 1
5 ## 调用other可以打印en,去掉global会报错。
6 other()
7 print(out.__globals__)
8 def other():
9 print(en)
10 out()

2, is the same in the class, not more than that, for fear of more and more chaotic, the class can use self.

3, other file import, and then if a good definition of it imported from other files? What happens, look at the chestnut, a file is still in the code above chestnuts

1 from report.a import *
2 out()
3 print(out.__globals__['en'])

result:

1 1
2 1

Okay okay, no error.

4, then the question is, if you want to continue to define the function in another file, is not it can continue to use global variables in the global file a declared it?

1 from report.a import out
2 def b():
3 print(out.__globals__['en'])
4 ## 直接飙红了。 -,-
5 print(en)
6 b()

Sure enough, the error:

1 Traceback (most recent call last):
2 File "/Users/dangfuli/Documents/PycharmProjects/asp/report/b.py", line 9, in <module>
3 b()
4 File "/Users/dangfuli/Documents/PycharmProjects/asp/report/b.py", line 6, in b
5 print(out.__globals__['en'])
6 KeyError: 'en'

That is, if introduced into the bag, defined by the global variable is not added to the global globals inside.

5, above, uses a lot of places, FUNC. Globals , this running about, look at instances

1 def out():
2 ## global关键字作用
3 global en
4 en = 1
5 other()
6 print(out.__globals__)
7 ## 调用other可以打印en
8 
9 def other():
10 print(en)
11 
12 out()
13 
14 ## 返回结果
15 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10306a7b8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/xxx/Documents/PycharmProjects/xxx/report/a.py', '__cached__': None, 'out': <function out at 0x101fcee18>, 'other': <function other at 0x1033f8c80>, 'en': 1}

Can be seen, after the current function in a global, EN variable is added to the inside globals

Let's look into other places to go

1 # a.py
2 def out():
3 ## global关键字作用
4 global en
5 en = 1
6 other()
7 print(out.__globals__['en'])
8 ## 调用other可以打印en
9 
10 def other():
11 print(en)
12 #############上面是a文件,下面是b文件#############
13 
14 # b.py
15 from report.a import out
16 
17 def b():
18 ## globals里面没有en那个全局变量
19 out()
20 print(b.__globals__['en'])
21 
22 b()

Look at the results:

1 1
2 Traceback (most recent call last):
3 File "/Users/xxx/Documents/PycharmProjects/xxx/report/b.py", line 8, in <module>
4 b()
5 File "/Users/xxx/Documents/PycharmProjects/xxx/report/b.py", line 6, in b
6 print(b.__globals__['en'])
7 KeyError: 'en'

It can be seen running to import a file, b en print file when on the error.

That's all for this article, we want to be helpful to learn, if you can find the article also wish to point a praise, have any comments or opinions are welcome to comment!

Guess you like

Origin blog.51cto.com/14568144/2445571