Example of how a python function returns multiple values

Source: Internet
Author: User
This article mainly introduces how python functions return multiple values. you can use python to return multiple values, which is really convenient.
Only one value can be returned for return in the function, but the return type is not limited.
Therefore, we can "return a tuple type to indirectly return multiple values ".
The example is my example of the robot framework source code:

The code is as follows:


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)

At that time, _ init _ is the class constructor, and he will get multiple return values parsed by _ parse, self. _ purge_empty_cells (data) is assigned to self. cells, self. _ purge_empty_cells (comments) is assigned to self. comments
That's simple :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.