我最近决定编写一个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
会有助于做到这一点,但事实并非如此。你知道吗
你可以用
cls.__module__
在sys.modules
中查找。你知道吗相关问题 更多 >
编程相关推荐