A sample method that returns multiple values from a python function
- 2020-04-02 13:14:39
- OfStack
Python can return multiple values, which is handy
A return in a function can return only one value, but the return type is not restricted
Therefore, we can "return a tuple type to indirectly return multiple values."
Example is my example in 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:
comments.append(cell[1:])
elif comments:
comments.append(cell)
else:
data.append(cell)
return self._purge_empty_cells(data), self._purge_empty_cells(comments)
Then s/s is the constructor of the class, which will get multiple return values with the return of _parse parse, self._purge_empty_cells(data) to self.cells, and self._purge_empty_cells(comments) to self.comments
Simple as that :)