Issue
Now I want to built a function get_doc( ) which can get the doc of the module Here's the code
def get_doc(module):
exec "import module"
print module.__doc__
And the information returned:
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
get_doc(sys)
NameError: name 'sys' is not defined
Solution
The problem is you're importing "module"
instead of the specified module, and you didn't put the name module
anywhere. A stupid fix to this would be to always using exec
def get_doc(module):
exec "import {}".format(module)
exec "print {}.__doc__".format(module)"
But instead of exec
, i would advise you to use the __import__
function:
def get_doc(module):
module = __import__(module)
print module.__doc__
Which allows more flexibility, and you can modify, use module as you wanted.
Answered By - aIKid Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.