python函式返回多個值

2021-06-21 20:36:08 字數 757 閱讀 8219

python函式返回多個值的方法 。

python可以返回多個值,確實挺方便

函式裡的return只能返回乙個值,但是返回型別是沒是限制的

因此,我們可以「返回乙個 tuple型別,來間接達到返回多個值」。

在robot framework source code 時的例子:

**:

def __init__(self, cells):

self.cells, self.comments = self._parse(cells)

def _parse(self, row):

data =

comments =

for cell in row:

cell = self._collapse_whitespace(cell)

if cell.startswith('#') and not comments:

elif comments:

else:#-- www.jbxue.com

return self._purge_empty_cells(data), self._purge_empty_cells(comments)

當時__init__是類的建構函式,將得到有_parse解析回來的多個返回值,self._purge_empty_cells(data) 賦給self.cells, self._purge_empty_cells(comments)賦給self.comments

就這麼簡單 :)

python函式返回多個值

函式可以返回多個值嗎?答案是肯定的。比如在遊戲中經常需要從乙個點移動到另乙個點,給出座標 位移和角度,就可以計算出新的新的座標 import math defmove x,y,step,angle 0 nx x step math.cos angle ny y step math.sin angle...

python函式多個返回值

python函式可以返回多個值嗎?答案是肯定的。比如在遊戲中經常需要從乙個點移動到另乙個點,給出座標 位移和角度,就可以計算出新的新的座標 import math def move x,y,step,angle 0 nx x step math.cos angle ny y step math.si...

讓函式返回多個值

讓函式返回多個值 include using namespace std int func int a,int b,int c int main int func int a,int b,int c 輸出 a 1b 2c 3a 1b 4c 27 我們知道函式只能返回乙個值,那麼假如有的時候我們需要函...