[英]Python - Calling a module function which uses globals() but it only pulls globals() from the module not the current python instance
[英]globals() works only for the current module
通過globals()
我可以在當前模塊中看到符號。 導入另一個模塊時,看不到它們。 那么,如何通過globals()
通過名稱訪問另一個模塊中的全局類,或者有可能嗎?
是的, globals()
僅顯示當前模塊的名稱空間。
模塊是具有屬性的對象 ; 您可以訪問其__dict__
屬性 ,或者更好的方法是使用vars()
函數將其名稱空間視為字典:
>>> import string
>>> list(vars(string))
['whitespace', '_string', 'ChainMap', 'ascii_uppercase', 'digits', '__name__', 'punctuation', 'Formatter', 'Template', '__doc__', '__spec__', '_re', 'hexdigits', '__package__', 'ascii_lowercase', '__cached__', 'octdigits', 'ascii_letters', '__builtins__', '_TemplateMetaclass', '__file__', 'capwords', '__loader__', 'printable']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.