KyleBlog.cn 文章 标签 关于
文章 标签 关于

Python代码获取指定函数的源码

inspect.getsource

通过inspect.getsource,我们可以获取各种类型的源码(module, class, method, function)。

代码示例:获取函数源码

import inspect

def hello():
  return 'hello'

print(inspect.getsource(hello))

执行上述代码后,控制台输出hello函数的源码:

def hello():
  return 'hello'

本文为kyleblog.cn原创,转载请注明出处:https://www.kyleblog.cn/posts/python_func_source

发布日期:2023-03-09 联系作者