如何在 Python 中获取对当前模块属性的引用 [英] How to get a reference to current module's attributes in Python
问题描述
我正在尝试做的在命令行中看起来像这样:
<预><代码>>>>导入我的模块>>>名称 = dir(mymodule)如何从 mymodule
自身内部获取对 mymodule
中定义的所有名称的引用?
像这样:
# mymodule.py名称 = dir(__thismodule__)
只需使用 globals()
<块引用>globals() — 返回一个字典代表当前的全局符号桌子.这永远是字典当前模块的(在一个函数或方法,这是模块它被定义的地方,而不是模块从它被调用).
http://docs.python.org/library/functions.html#globals
What I'm trying to do would look like this in the command line:
>>> import mymodule
>>> names = dir(mymodule)
How can I get a reference to all the names defined in mymodule
from within mymodule
itself?
Something like this:
# mymodule.py
names = dir(__thismodule__)
Just use globals()
globals() — Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).
http://docs.python.org/library/functions.html#globals
这篇关于如何在 Python 中获取对当前模块属性的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!