Dav*_*ein 4 python python-import python-3.x
我最近问了一个标题为“python find the type of a function”的问题,并得到了非常有用的答案。这是一个相关的问题。
假设我导入我编写的 *.py 文件,这些导入导致f成为我定义的函数之一。现在我写信给我的 python 解释器x = f。后来,我想看到 的完整定义f,最好还有注释,只知道x。这可能吗?python 是否记得定义是从哪个文件导入的,这当然不足以给出 的完整定义f,除非可以找到实际的相关定义?
如果您为您评论的某个函数添加别名,内置help(object)将为您提供正确的文档k- 同样inspect.getsource(k)- 他们知道k此时您的变量名称别名是哪个函数。
看:
例子:
# reusing this code - created it for some other question today
class well_documented_example_class(object):
"""Totally well documented class"""
def parse(self, message):
"""This method does coool things with your 'message'
'message' : a string with text in it to be parsed"""
self.data = [x.strip() for x in message.split(' ')]
return self.data
# alias for `parse()`:
k = well_documented_example_class.parse
help(k)
Run Code Online (Sandbox Code Playgroud)
印刷:
Help on function parse in module __main__:
parse(self, message)
This method does coool things with your 'message'
'message' : a string with text in it to be parsed
Run Code Online (Sandbox Code Playgroud)
同样适用于inspect.getsource(k):
# from /sf/answers/3663358401/
import inspect
print(inspect.getsource(k))
Run Code Online (Sandbox Code Playgroud)
印刷:
def parse(self, message):
"""This method does coool things with your 'message'
'message' : a string with text in it to be parsed"""
self.data = [x.strip() for x in message.split(' ')]
return self.data
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2787 次 |
| 最近记录: |