我应该为 Python 中的全局模块命名什么?
我正在用 Python 编写一个应用程序,并且有许多通用变量(例如对主窗口的引用、用户设置和 UI 中的活动项目列表),这些变量必须可以从所有位置访问程序的一部分1。我刚刚意识到我已将模块命名为 globals.py
并且正在导入包含这些变量的对象,并在我的顶部使用 from globals import globals
语句文件。
显然,这是可行的,但我对将全局对象命名为与 Python 内置对象相同的名称有点怀疑。不幸的是,我想不出更好的命名约定。 global
和 all
也是 Python 内置函数,universal
似乎不精确,state
并不是真正正确的想法。我倾向于 static
或 env
,尽管两者在计算机术语中都有特定的含义,这表明了不同的概念。
那么,(在 Python 中)您会如何称呼包含所有其他模块全局变量的模块呢?
1 我意识到我可以将这些(或包含它们的单个对象)作为变量传递到我调用的每个其他函数中。这最终是不可行的,不仅仅是因为它使启动代码和函数签名非常丑陋。
I'm writing an application in Python, and I've got a number of universal variables (such as the reference to the main window, the user settings, and the list of active items in the UI) which have to be accessible from all parts of the program1. I only just realized I've named the module globals.py
and I'm importing the object which contains those variables with a from globals import globals
statement at the top of my files.
Obviously, this works, but I'm a little leery about naming my global object the same as the Python builtin. Unfortunately, I can't think of a much better naming convention for it. global
and all
are also Python builtins, universal
seems imprecise, state
isn't really the right idea. I'm leaning towards static
or env
, although both have a specific meaning in computer terms which suggests a different concept.
So, what (in Python) would you call the module which contains variables global to all your other modules?
1 I realize I could pass these (or the single object containing them) as a variable into every other function I call. This ends up being infeasible, not just because it makes the startup code and function signatures really ugly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会尝试完全避免这样的全局容器模块,而是将这些变量放入它们自己的模块中,然后可以从系统的所有部分导入它们。
例如,主窗口可能会进入 main.py 中的变量。用户设置可以进入 usersettings.py ,它将提供查看和更改设置的功能。
如果系统的另一部分需要访问用户设置,这是一个简单的问题:
类似的方法可能用于需要全局访问的其他内容。这会导致更清晰的关注点分离和更可测试的代码,因为您可以单独测试模块,而无需始终依赖
globals
模块。I would try to avoid such a global container module altogether, and instead put these variables into their own modules, which can then be imported from all parts of the system.
For example, the main window would probably go into a variable in
main.py
. User settings could go intousersettings.py
which would provide functions to view and change the settings.If another part of the system needs to access the user settings, that's a simple matter of:
A similar approach could probably be used for other stuff that needs to be globally accessible. This leads to clearer separation of concerns and more testable code, since you can test modules in isolation without depending on the
globals
module all the time.我将其称为
env
。有人将其与os.environ
混淆的风险很小(特别是如果您组织代码以便可以将其称为myapp.environ
)。我还会将
myapp.environ
公开的所有内容都设为类的属性,这样我就可以在需要时在 setter 中放置断点。I'd call it
env
. There's little risk that someone will confuse it withos.environ
(especially if you organize your code so that you can call itmyapp.environ
).I'd also make everything exposed by
myapp.environ
a property of a class, so that I can put breakpoints in the setter when the day comes that I need to.这将解决冲突并遵循 PEP 8 建议。
此外,在其他类似情况下,Roget 同义词库也是您的朋友。我总是在附近保留一份副本。
This will fix the conflict and also follows PEP 8 recommendations.
Also, in other cases like this, Roget's Thesaurus is your friend. I always keep a copy nearby.
global
是一个关键字,而不是内置关键字。 'globals' 不是关键字,而是一个内置函数。它可以分配给,但这是不好的做法。像 pylint 和 pychecker 这样的代码检查器可以捕获这些意外分配。配置
怎么样?global
is a keyword, not a built-in. 'globals' is not a keyword, but is a built-in function. It can be assigned to, but is bad practice. Code checkers like pylint and pychecker can catch these accidental assignments. How aboutconfig
?顶部
?顶级
?top
?top_level
?