zoukankan      html  css  js  c++  java
  • pytion3--文档字符串:__doc__

    除了#注释外,Python也支持可自动附加在对象上的文档,而且在运行时还可保存查看。从语法上来说,这类注释是写成字符串,放在模块文档、函数以及类语句的顶端。就在任何可执行程序代码前(#注释在其前也没问题)。Python会自动封装这个字符串,也就是成为所谓的文档字符串,使其成为相应对象的__doc__属性。

    用户定义的文档字符串

    例如,考虑下面的文件docstrings.py。其文档字符串出现在文件开端以及其中的函数和类的开头。在这里,文件和函数多行注释使用的是三重引号块字符串,但是任何类型的字符串都能用。

    """
    Module documentation
    Words Go Here
    """
    
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    spam = 40
    
    def square(x):
        """
        fouction documetation
        can we have your liver hten?
        :param x:
        :return:
        """
        return x **2
    
    class emplouee:
        "class documetation"
        pass
    
    print(square(4))
    print(square.__doc__)
    

    运行结果:

    16
    
        fouction documetation
        can we have your liver hten?
        :param x:
        :return:
    

    考虑下面的文件docstrings.py

    """
    Module documentation
    Words Go Here
    """
    
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import docstrings
    
    print(docstrings.__doc__)
    print(docstrings.square.__doc__)
    print(docstrings.emplouee.__doc__)
    

    运行结果:

    Module documentation
    Words Go Here
    
    
        fouction documetation
        can we have your liver hten?
        :param x:
        :return:
        
    class documetation
    
  • 相关阅读:
    ContentProvider
    铃声设置
    TTS技术
    http://www.w3cschool.cc/jqueryui/jqueryui-tutorial.html
    HttpHelper
    .net面试题
    函数和原型
    关于递增运算符
    CSS学习笔记
    CSS/CSS3 如何实现元素水平居中
  • 原文地址:https://www.cnblogs.com/chenlin163/p/7299101.html
Copyright © 2011-2022 走看看