简体   繁体   English

访问“模块范围”变量

[英]accessing “module scope” vars

I'm currently learning Python, and I have to work on a Python 2.7 project. 我目前正在学习Python,我必须从事Python 2.7项目。

Accessing "module scope" variables in functions of the module itself is a bit confusing for me, and I didn't succeed in finding a satisfying way. 在模块本身的功能中访问“模块范围”变量对我来说有点混乱,我没有成功找到令人满意的方式。

My attempts so far: 到目前为止我的尝试:

Way 1: 方式1:

my_module.py my_module.py

my_global_var = None

def my_func():
    global my_global_var
    my_global_var = 'something_else'

Here I think that confusing local and "module scope" vars may be quite easy. 在这里,我认为混淆本地和“模块范围”变量可能非常容易。

Way 2: 方式2:

my_module.py my_module.py

import my_module

my_global_var = None

def my_func():
    my_module.my_global_var = 'something_else'

Here, the name of "my_module" could not be as easily changed as "way 1" when necessary. 在这里,必要时,“my_module”的名称不能像“方式1”那样容易地改变。 Plus, importing a module into itself sounds quite weird. 另外,将模块导入自身听起来很奇怪。

What would you recommend? 你会推荐什么? Or would you suggest something else? 或者你会推荐别的吗? Thanks. 谢谢。

You probably want to read up on Python's namespaces . 您可能想要阅读Python的命名空间 Way 1 is correct but generally unnecessary, never use 2. An easier approach is to just use a dict (or class or some other object): 方式1是正确的但通常是不必要的,从不使用2.更简单的方法是使用dict(或类或其他对象):

my_globals = {'var': None}

def my_func():
    my_globals['var'] = 'something else'

Assignments always go into the innermost scope and the innermost scope is always searched first, thus the need for the global keyword. 赋值始终进入最内层范围,始终首先搜索最内层范围,因此需要global关键字。 In this case you aren't assigning to a name, so it's unnecessary. 在这种情况下,您没有分配名称,因此没有必要。

Way 1 is the correct way when you absolutely must rebind a global variable. 当您绝对必须重新绑定全局变量时,方法1是正确的方法。 However you should ask yourself why you are modifying a global and whether there is something better you can do (such as encapsulating the behaviour in a class). 但是你应该问问自己为什么要修改全局,以及是否有更好的东西可以做(比如将行为封装在类中)。

Importing a module into itself should be avoided as it is error prone. 应避免将模块导入自身,因为它容易出错。 If the module is also a script you would sometimes need to import __main__ instead, or if the module is part of a package maybe you should be importing foo.my_module . 如果模块也是一个脚本,你有时需要导入__main__ ,或者如果模块是包的一部分,你可能应该导入foo.my_module In short, don't do that. 简而言之,不要那样做。

Importing a module within itself can have unwanted side effects (like evaluating statements more than once.) I would suggest using "Way 1" and a tool like pylint to help verify your code and enforce common practices. 在自身内部导入模块可能会产生不必要的副作用(比如不止一次地评估语句。)我建议使用“Way 1”和像pylint这样的工具来帮助验证代码并强制执行常规做法。

PyLint can be found at: http://www.logilab.org/project/pylint PyLint可以在http://www.logilab.org/project/pylint找到

Avoid setting globals at all. 避免设置全局变量。 You can create new namespaces with classes quite easily, so use class variables if you must. 您可以非常轻松地创建包含类的新命名空间,因此如果必须,请使用类变量。

For anything serious you need a proper design with classes anyways. 对于任何严肃的事情,无论如何你都需要适当的设计。

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

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