python获取函数注释 您所在的位置:网站首页 如何提取代码的所有注释 python获取函数注释

python获取函数注释

2024-02-27 09:18| 来源: 网络整理| 查看: 265

使用 help  函数 可以查看 函数的注释内容 但是它也有点"添油加醋"

其实函数的注释被保存在 __doc__属性里面  PS 双下划线

def f(): """这里是f函数""" pass print('=======================') help(f) print('=======================') print(f.__doc__) print('=======================') ''' output 输出 ======================= Help on function f in module __main__: f() 这里是f函数 ======================= 这里是f函数 ======================= '''

另外   三双引号 三单引号均可     ,但 # 的不行

def f(): #这里是f函数 pass print('=======================') help(f) print('=======================') print(f.__doc__) print('=======================') ''' output 输出 ======================= Help on function f in module __main__: f() ======================= None ======================= '''

 应该说help 运行时 使用了 对象的 __doc__

def f(): """f的注释""" help(f) print('=================') f.__doc__ = '------f的新注释------' help(f) ''' output 输出 Help on function f in module __main__: f() f的注释 ================= Help on function f in module __main__: f() ------f的新注释------ '''

可以直接对 __doc__赋值  改变help的结果

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有