zoukankan      html  css  js  c++  java
  • python获取函数注释 __doc__

    使用 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的结果

  • 相关阅读:
    【调试】关于F9
    【vue】vue变量定义的位置
    【js】MVVM模型
    RE最全面的正则表达式----数字篇
    Spider & CrawlSpider
    论小脚本的重要性
    论小脚本的简单性3
    论小脚本的简单性2
    论小脚本的简单性
    git的常用命令
  • 原文地址:https://www.cnblogs.com/ansver/p/10003727.html
Copyright © 2011-2022 走看看