不同模块中的Python3全局范围 [英] Python3 global scoping in different modules

查看:56
本文介绍了不同模块中的Python3全局范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清一下,来自不同模块的全局变量是如何作用域的?

I would like to clarify, how globals from different modules are scoped?

我没有找到这件事的相关文档,因此我依靠观察,但我想有更多的见解,如果我的发现纯粹是实现巧合,或者我是否可以信任他们?

I failed to find relevant documentation for this matter, thus I am relying on observation, but I would like to have some more insight, if my findings are pure implementation coincidence, or if I can trust them?

测试用例:

module_1.py:

module_1.py:

global_var1 = 'm1_v1'
global_var2 = 'm1_v2'

def f():
 print('f: {}'.format(global_var1))
 print('f: {}'.format(global_var2))

module_2.py:

module_2.py:

import module_1

global_var1 = 'm2_v1'

def g():
 print('g: {}'.format(global_var1))
 print('g: {}'.format(global_var2))

module_1.f()

g()

$ python3 module_2.py

f: m1_v1
f: m1_v2
g: m2_v1
Traceback (most recent call last):
  File "module_2.py", line 11, in <module>
    g()
  File "module_2.py", line 7, in g
    print('g: {}'.format(global_var2))
NameError: name 'global_var2' is not defined

结论:

因此,我的结论是函数将按以下顺序使用全局变量:

Conclusion:

Thus, my conclusion is that a function will use the globals in this order:

  1. 使用函数的模块
  2. 定义函数的模块(这是唯一的答案!)

全局变量不会从导入的模块中渗漏出来.

从其他模块导入的函数带来它们的模块全局变量,就像一个闭包.

Functions imported from other modules bring their module globals like a closure.

我想看一些关于这个问题的综合文档(我没找到……).

I would like to see some comprehensive documentation on this matter (which I failed to find...).

虽然测试这个很好,但我不知道,如果这是一个奇怪的巧合,这种行为不应该被信任,或者这是预期的方式?

While testing this out is nice, I have no idea, if this is a freak coincidence, and this behavior should never be trusted, or this is the expected way?

另外,如果一个函数以某种方式通过第三个模块导入怎么办?如果函数是一个类方法呢?等等等等

Also, what if a function is somehow imported through a 3rd module? what if the function is a class method? Etc.etc.

如果您无法向我指出文档,但您肯定"知道指南,那么我也很感兴趣.

If you can't point me to a documentation, but you know a guideline "for sure", I am interested, as well.

我不会详细说明为什么以及如何使用它,我主要有兴趣更好地了解 python 的工作原理.但是,如果您确实知道更好的解决方案,鉴于手头的信息,我也很想看到它 - 不过它不会被接受为答案.

I won't go in lengths why and how I want to use this, I am primarily interested to better understand the workings of python. However, if you do know a better solution, given the information at hand, I am interested to see that, as well - it will NOT be accepted as an answer, though.

如果 python2 和 python3 之间存在差异,我的主要兴趣是 python3,但也很高兴有关于 python2 的信息.

In case there is a difference between python2 and python3, my main interest is python3, but it is nice to have the info on python2, as well.

推荐答案

TLDR:Python 使用 词法/静态范围 自 Python 2.1 起,包括 Python 3.(参见 名称解析PEP 227)

TLDR: Python uses lexical/static scoping since Python 2.1, including Python 3. (See name resolution and PEP 227)

词法作用域意味着函数只能访问定义它们的封闭作用域,直到其模块的全局命名空间.只有 builtins 在所有模块中可见.模块的全局命名空间不共享,并且在另一个模块中导入或调用函数不会授予它访问该命名空间的权限.

Lexical scoping means functions only have access to the enclosing scopes in which they are defined, up to their module’s global namespace. Only the builtins are visible in all modules. The global namespace of modules is not shared, and importing or calling a function in another module does not give it access to that namespace.

一些值得指出的特性:

  • 局部/函数作用域是在解析函数时定义的.名称可以是本地名称、闭包/非本地名称或全局名称.
  • 全局/模块范围是可变的.可以随时添加、删除或更改名称.
  • 类引入了短期作用域,对函数或其中定义的其他类不可见.

这篇关于不同模块中的Python3全局范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆