Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我尝试运行此代码时
"""Hello World""" print globals()[__doc__]
为什么我会收到此错误?
Traceback (most recent call last): File "D:\myProjects\python\Python-13.py", line 3, in <module> print globals()[__doc__] KeyError: 'Hello World'
上下文:我只想要当前模块的文档字符串
你需要print globals()['__doc__'].
print globals()['__doc__']
当前模块的文档字符串是__doc__. 在您的代码中,您试图使用该字符串作为模块全局字典中的键。
__doc__
要清楚,要打印文档字符串,只需执行print __doc__.
print __doc__