为什么这个变量现在在Python中是跨模块全局的?

发布于 2024-10-06 03:09:45 字数 1145 浏览 3 评论 0原文

我将稍微简化代码,希望有人能对此有所启发,并且如果我所做的事情是错误的,也许可以给我一个更Pythonic的方法。

dbcontrollers.py 内部

import autosettings
import aerialcontroller
import controller
import gobject
import gtk
import utils

# Create the settings object so we can call and edit settings easily
SETTINGS = autosettings.autoSettings()
CONTROLLERS = {}

class dBControllers(object):

    def __init__(self):

        # This works happily so nothing wrong in autosettings module
        print SETTING.last_port
        port_to_use = SETTINGS.last_port

if __name__ == "__main__":

    # Create an object that holds all the GTK stuff (when not simplified)
    dBControllers = dBControllers()
    aerialController = aerialcontroller.aerialController()

Aerocontroller.py

class aerialController(object):

    def __init__(self):

        self.motor_number = str(SETTINGS.aerial_motor_number)
        CONTROLLERS[self.motor_number] = self

但是当我运行这个时,我得到“NameError:全局名称'SETTINGS'未定义”。

我真的不明白这是如何工作的,跨模块共享全局变量。我认为这不是做这样的事情的正确方法,所以有人可以指出我正确的方向。

谢谢。

PS 简单地编写示例代码会很有帮助——对于 python 来说,它仍然是一个相对较新的人。

I shall simplify the code slightly in the hopes that someone can shed some light on this and maybe give me a more pythonic approach if what im doing is wrong.

Inside dbcontrollers.py

import autosettings
import aerialcontroller
import controller
import gobject
import gtk
import utils

# Create the settings object so we can call and edit settings easily
SETTINGS = autosettings.autoSettings()
CONTROLLERS = {}

class dBControllers(object):

    def __init__(self):

        # This works happily so nothing wrong in autosettings module
        print SETTING.last_port
        port_to_use = SETTINGS.last_port

if __name__ == "__main__":

    # Create an object that holds all the GTK stuff (when not simplified)
    dBControllers = dBControllers()
    aerialController = aerialcontroller.aerialController()

Inside aerialcontroller.py

class aerialController(object):

    def __init__(self):

        self.motor_number = str(SETTINGS.aerial_motor_number)
        CONTROLLERS[self.motor_number] = self

But when I run this, I get "NameError: global name 'SETTINGS' is not defined".

I don't really understand how this is supposed to work, sharing global variables accross modules. I assume this is not the correct way to do things like this, so can someone point me in the right direction.

Thank-you.

P.S. Simply code examples would be beneficial - still a relative newcomer to python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

离鸿 2024-10-13 03:09:45

Python 没有办法在模块之间共享名称。如果您想从多个模块访问某些内容,则可以在单独的模块中声明它(例如“设置”),然后将其导入到您想要的任何位置。

settings.py:aerialcontroller.py

SETTINGS = autosettings.autoSettings()
CONTROLLERS = {}

from settings import SETTINGS, CONTROLLERS
class aerialController(object):

    def __init__(self):
        self.motor_number = str(SETTINGS.aerial_motor_number)
        CONTROLLERS[self.motor_number] = self

​只要您只想修改设置,就可以执行from settings import SETTINGS。如果您想将其完全替换为一组新设置 (SETTINGS = ...),您需要导入设置,并将其引用为 settings.SETTINGS

这是设计使然:如果模块从导入它们的脚本中获取变量,那么在读取代码时就很难看出它们来自哪里(并且从其他地方导入模块可能不起作用)。通过这种方式,您始终可以看到变量来自每个文件中的位置(好吧,几乎总是:请不要过度使用 fromimport *)。

Python doesn't have a way to share a name across modules. If there's something you want access to from multiple modules, you declare it in a separate module (e.g. "settings"), and then import it wherever you want it.

settings.py:

SETTINGS = autosettings.autoSettings()
CONTROLLERS = {}

aerialcontroller.py

from settings import SETTINGS, CONTROLLERS
class aerialController(object):

    def __init__(self):
        self.motor_number = str(SETTINGS.aerial_motor_number)
        CONTROLLERS[self.motor_number] = self

N.B. Doing from settings import SETTINGS will work as long as you only want to modify SETTINGS. If you want to completely replace it with a new set of settings (SETTINGS = ...) you need to do import settings, and refer to it as settings.SETTINGS.

This is by design: if modules got variables from the script that imported them, it would be much harder to see where they came from when reading the code (and importing the module from somewhere else might not work). Doing it this way, you can always see where the variable came from within each file (well, almost always: please don't overuse from <name> import *).

浅笑依然 2024-10-13 03:09:45

我认为最明智的方法是让您的 aerialController 类采用设置和控制器对象:

class aerialController(object):

    def __init__(self, settings, controllers):
        self.motor_number = str(settings.aerial_motor_number)
        controllers[self.motor_number] = self

...然后,在另一个模块中:

if __name__ == "__main__":
    dBControllers = dBControllers()
    aerialController = aerialcontroller.aerialController(SETTINGS, CONTROLLERS)

您可能可以更好地构建它并传递电机编号中,调用者对控制器进行分配,但这偏离了主题......

I think the most sensible way is to make your aerialController class take a settings and controllers object:

class aerialController(object):

    def __init__(self, settings, controllers):
        self.motor_number = str(settings.aerial_motor_number)
        controllers[self.motor_number] = self

...and then, in the other module:

if __name__ == "__main__":
    dBControllers = dBControllers()
    aerialController = aerialcontroller.aerialController(SETTINGS, CONTROLLERS)

You could probably structure this even better and have the motor number passed in, and the caller do the assignment to the controllers, but that's straying off topic...

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