Can Python3 class instances be forced to be global objects at initialisation?

橙三吉。 提交于 2019-12-23 06:28:04

问题


Even though global Python objects tend to be bad, I more or less forced to use them with module curses. I currently have this:

class Window:
    def __init__(self, title, h, w, y, x):
        self.window = curses.newwin(h, w, y, x)
        self.window.box()
        self.window.hline(2, 1, curses.ACS_HLINE, w-2)
        self.window.addstr(1, 2, title)
        self.window.refresh()

global top_win
top_win = Window('Top window', 6, 32, 3, 6)

I am wondering whether it would be possible to get rid of the global line by adding something to the class definition or initialisation?

class Window:
    def __init__(self, title, h, w, y, x):
        # Some global self magic
        self.window = curses.newwin(h, w, y, x)
        self.window.box()
        self.window.hline(2, 1, curses.ACS_HLINE, w-2)
        self.window.addstr(1, 2, title)
        self.window.refresh()

top_win = Window('Top window', 6, 32, 3, 6)

回答1:


Global class pattern

Despite the reflexive downvotes whenever the word global is seen, I resolved my issue anyhow by applying the equally controversial global class pattern.

class win:
    pass

class Window:
    def __init__(self, title, h, w, y, x):
        self.window = curses.newwin(h, w, y, x)
        self.window.box()
        self.window.hline(2, 1, curses.ACS_HLINE, w-2)
        self.window.addstr(1, 2, title)
        self.window.refresh()

win.top = Window('Top window', 6, 32, 3, 6)

This results in win.top being accessible anywhere in my Python script, just like any global variable would have been, but then nice and neat in a more controlled manner.

This is quite handy for defining new curses windows inside the main() routine which is typically wrapped inside a curses.wrapper(main). Follow this link for a full blown Python3 curses example.



来源:https://stackoverflow.com/questions/45052970/can-python3-class-instances-be-forced-to-be-global-objects-at-initialisation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!