我应该为 Python 中的全局模块命名什么?

发布于 2024-08-19 04:35:12 字数 609 浏览 8 评论 0原文

我正在用 Python 编写一个应用程序,并且有许多通用变量(例如对主窗口的引用、用户设置和 UI 中的活动项目列表),这些变量必须可以从所有位置访问程序的一部分1。我刚刚意识到我已将模块命名为 globals.py 并且正在导入包含这些变量的对象,并在我的顶部使用 from globals import globals 语句文件。

显然,这是可行的,但我对将全局对象命名为与 Python 内置对象相同的名称有点怀疑。不幸的是,我想不出更好的命名约定。 globalall 也是 Python 内置函数,universal 似乎不精确,state 并不是真正正确的想法。我倾向于 staticenv,尽管两者在计算机术语中都有特定的含义,这表明了不同的概念。

那么,(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷…雨湿花 2024-08-26 04:35:13

我会尝试完全避免这样的全局容器模块,而是将这些变量放入它们自己的模块中,然后可以从系统的所有部分导入它们。

例如,主窗口可能会进入 main.py 中的变量。用户设置可以进入 usersettings.py ,它将提供查看和更改设置的功能。

如果系统的另一部分需要访问用户设置,这是一个简单的问题:

from usersettings import get_setting, set_setting
...
# Do stuff with settings

类似的方法可能用于需要全局访问的其他内容。这会导致更清晰的关注点分离和更可测试的代码,因为您可以单独测试模块,而无需始终依赖 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 into usersettings.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:

from usersettings import get_setting, set_setting
...
# Do stuff with settings

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.

-小熊_ 2024-08-26 04:35:13
`config` or `settings`
`config` or `settings`
以歌曲疗慰 2024-08-26 04:35:13

我将其称为env。有人将其与 os.environ 混淆的风险很小(特别是如果您组织代码以便可以将其称为 myapp.environ)。

我还会将 myapp.environ 公开的所有内容都设为类的属性,这样我就可以在需要时在 setter 中放置断点。

I'd call it env. There's little risk that someone will confuse it with os.environ (especially if you organize your code so that you can call it myapp.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.

孤芳又自赏 2024-08-26 04:35:13
from globals import Globals

这将解决冲突并遵循 PEP 8 建议。

此外,在其他类似情况下,Roget 同义词库也是您的朋友。我总是在附近保留一份副本。

from globals import Globals

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.

陌若浮生 2024-08-26 04:35:13

global 是一个关键字,而不是内置关键字。 'globals' 不是关键字,而是一个内置函数。它可以分配给,但这是不好的做法。像 pylintpychecker 这样的代码检查器可以捕获这些意外分配。 配置怎么样?

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 about config?

暗地喜欢 2024-08-26 04:35:13

顶部顶级

top? top_level?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文