访问“modu”子类的全局变量

2024-06-16 12:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近决定编写一个helper类来输出模块中的一些信息。看起来是这样的: 你知道吗

class VarPrinter(object):
    @classmethod
    def save(cls):
        globals = cls.globals
        #do stuff with globals...
            for i in things_to_output:
            ...

# subclass in another module
# e.g. in foo.py
class FooPrinter(VarPrinter):
    things_to_output = ('foo', 'bar')
    globals = globals()

问题是:有没有一种方法可以获得一个模块的globals()数组,其中VarPrinter是子类的,这样我就不需要像globals = globals()那样显式地传递它了。换句话说,我想要的解决方案的行为就像我在子类的模块中重复了基类的代码一样。起初我认为eval会有助于做到这一点,但事实并非如此。你知道吗


Tags: 模块toinhelper信息outputobjectfoo