获取python文件的doc字符串
如果我只有文件名,有没有办法获取 python 文件的文档字符串?例如,我有一个名为 a.py 的 python 文件。我知道它有一个文档字符串(之前已强制执行),但不知道其内部结构,即它是否有任何类或主要等?我希望我不会忘记一些很明显的事情 如果我知道它有一个主要功能,我可以通过使用 import 的方式来完成它,
filename = 'a.py'
foo = __import__(filename)
filedescription = inspect.getdoc(foo.main())
我不能只这样做:
filename.__doc__ #it does not work
Is there a way of getting the doc string of a python file if I have only the name of the file ? For instance I have a python file named a.py. I know that it has a doc string ( being mandated before) but don't know of its internal structure i.e if it has any classes or a main etc ? I hope I not forgetting something pretty obvious
If I know it has a main function I can do it this way that is using import
filename = 'a.py'
foo = __import__(filename)
filedescription = inspect.getdoc(foo.main())
I can't just do it this way:
filename.__doc__ #it does not work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该做...
或者更简单...
You should be doing...
or yet simpler...
如果您需要您已经所在的模块的文档字符串:
And if you need the docstrings of the module your are already in :