我想隐式地将当前全局名称空间的引用传递给函数,以便编辑它。我觉得这是可能的,因为exec()
这样做了,但这可能太特殊了。你知道吗
你知道吗垃圾邮件.py你知道吗
# This will fail because globals() grabs the spam.py globals
# I would like to to grab the globals from wherever it was called from
def writeOtherGlobals( implicitGlobals=globals() ):
print "Got sausages", implicitGlobals['sausages']
implicitGlobals['sausages'] = 10
你知道吗鸡蛋.py你知道吗
from spam import writeOtherGlobals
sausages = 5
writeOtherGlobals()
print sausages # I want this to print 10
您可以使用'dir(module)`方法,排除uuu变量(忽略名称等),或者您可能希望在模块末尾添加一个变量,如下所示:
所以现在你可以
所有的globals现在都已打印出来,您可以像在原始类中调用globals()一样访问每个globals。你知道吗
注意你不需要知道
module.py
的名字,只要你的模块有一个._globals
它的名字就行了此函数将为调用它的上下文返回全局变量的字典。你知道吗
相关问题 更多 >
编程相关推荐