[英]What is the python docstring convention for returning multiple values?
說我有一個函數返回多個值
import numpy as np
def add_and_raisepower(x, y):
"""
1) Add inputs **x** and **y** and return the result.
2) Raise **x** to the power of **y**, multiply this
result by an array of one's and return the result.
:type x: float
:param x: The first input number
:type y: float
:param y: The second input number
:rtype val1: float
:rtype val2: numpy.array(float)
"""
val1 = x + y
val2 = np.ones(100)*(x**y)
return val1, val2
我關心的是:rtype:
在文檔字符串中的注釋; 如果函數返回多個值(如本例所示),應如何在文檔字符串中寫入:rtype:
根據PEP-8)?
通常,對於僅返回一個值的函數, :rtype:
會寫成類似
"""
...
:rtype: int
"""
此處沒有指定返回的變量名,因為沒有關系,因為只有一個返回值。
我了解理想情況下,我應該嘗試將我的功能分解為更簡單的功能,這對於上面的add_and_raisepower
當然是可行的,但是我僅以此為例來說明問題。
在這種情況下,每次結果都是一個元組。 像這樣寫下來:
:rtype: (float, numpy.array(float))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.