Jibin
Jibin

Reputation: 3102

globals dict error

When I try to run this code

"""Hello World"""

print globals()[__doc__]

Why do I get this error ?

Traceback (most recent call last):
File "D:\myProjects\python\Python-13.py", line 3, in <module>
print globals()[__doc__]
KeyError: 'Hello World'

Context : I just want the doc string of current module

Upvotes: 0

Views: 890

Answers (2)

Roman Bodnarchuk
Roman Bodnarchuk

Reputation: 29737

You need print globals()['__doc__'].

Upvotes: 6

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76876

The docstring of the current module is __doc__. In your code you are trying to use that string as key in the global dictionary of the module.

To be clear, to print the docstring, just do print __doc__.

Upvotes: 5

Related Questions