文档字符串

Python 中级:面向开发者

Jasmin Ludolf

Senior Data Science Content Developer

文档字符串

  • 描述函数的文本块

  • 帮助用户了解如何使用函数

Help on built-in function round in module builtins:

round(number, ndigits=None)

Round a number to a given precision in decimal digits. The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as the number. ndigits may be negative.
Python 中级:面向开发者

访问文档字符串

# 访问信息(包括文档字符串)
print(help(round))
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

None
Python 中级:面向开发者

访问文档字符串

# 只访问文档字符串
print(round
Python 中级:面向开发者

访问文档字符串

# 只访问文档字符串
print(round.
Python 中级:面向开发者

访问文档字符串

# 只访问文档字符串
print(round.__
Python 中级:面向开发者

访问文档字符串

# 只访问文档字符串
print(round.__doc
Python 中级:面向开发者

访问文档字符串

# 只访问文档字符串
print(round.__doc__)
Round a number to a given precision in decimal digits.

The return value is an integer if ndigits is omitted or None.  Otherwise
the return value has the same type as the number.  ndigits may be negative.

$$

$$

  • .__doc__: "dunder-doc" 属性
Python 中级:面向开发者

创建文档字符串

def average(values):

# 单行文档字符串 """计算序列的平均值,并四舍五入到两位小数。"""
average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python 中级:面向开发者

访问文档字符串

# 访问我们的文档字符串
print(average.__doc__)
Find the mean in a sequence of values and round to two decimal places.
Python 中级:面向开发者

更新文档字符串

# 更新函数的文档字符串
average.__doc__ = "Calculate the mean of values in a data structure, rounding the results to 2 digits."


print(help(average))
Help on function average in module __main__:

average(values)
    Calculate the mean of values in a data structure, rounding the results to 2 digits.
Python 中级:面向开发者

多行文档字符串

def average(values):
    """
    Find the mean in a sequence of values and round to two decimal places.






    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python 中级:面向开发者

多行文档字符串

def average(values):
    """
    Find the mean in a sequence of values and round to two decimal places.

    Args:




    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python 中级:面向开发者

多行文档字符串

def average(values):
    """
    Find the mean in a sequence of values and round to two decimal places.

    Args:
        values (list): A list of numeric values.



    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python 中级:面向开发者

多行文档字符串

def average(values):
    """
    Find the mean in a sequence of values and round to two decimal places.

    Args:
        values (list): A list of numeric values.

    Returns:
        rounded_average (float): The mean of values, rounded to two decimal places.
    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python 中级:面向开发者

访问文档字符串

# 帮助
print(help(average))
Help on function average in module __main__:

average(values)
    Find the mean in a sequence of values and round to two decimal places.

        Args:
            values (list): A list of numeric values.

        Returns:
            rounded_average (float): The mean of values, rounded to two decimal places.
Python 中级:面向开发者

Passons à la pratique !

Python 中级:面向开发者

Preparing Video For Download...