Working with Multiple Data Types
A function can return multiple values by packaging them into a single tuple object using the comma syntax in the return statement.
One Result with Several Parts
Many functions need to communicate more than one piece of information to the code that called them. A validation function might need to provide both a success status and an error message. A data-processing function might need to provide both a result and information about the operation. Python handles this by packaging the values into one tuple object.
A function that appears to return several values is returning one tuple containing those values. The tuple gives the caller one returned object whose elements can be accessed together or distributed into separate variables.
The Return Tuple
The syntax for returning multiple values uses a return statement followed by the values separated by commas. The values are packaged into a tuple. Parentheses may be used around the values, but the comma is the important part that creates the tuple. This means the returned result is one tuple object, not several independent return channels.
The function returns one tuple containing two elements: the status value 404 and the message "Page not found".Unpacking in Order
Tuple unpacking distributes the elements of one returned tuple into multiple variables in a single assignment statement. Python first executes the function and receives its tuple. It then compares the number of variables on the left side with the number of tuple elements. Finally, it assigns each element to the corresponding variable in order.
def get_error_details(): return 404, "Page not found" errnum, errstr = get_error_details()
Reading a Validation Result
A validation function returns a success status and an error message. How can the caller work with both results separately?
Package: The function returns the status and message as elements of one tuple.
Match: The caller writes two variables because the returned tuple contains two elements.
Assign: The first returned element goes to success, and the second goes to message.
The caller can use success and message as separate variables after one tuple-unpacking assignment.
Indexing or Unpacking
| Approach | What it does | Useful when |
|---|---|---|
| Indexing | Keeps the returned tuple as one object and accesses elements with bracket notation | You want the values together or need only some values |
| Unpacking | Distributes tuple elements into separate variables in one assignment | You know how many values are returned and want to use them individually |
The first part of this example keeps the tuple in details and uses indexing to retrieve its elements. The second assignment immediately unpacks the returned tuple. Unpacking is generally preferred when the number of returned values is known and the values should be used as individual variables. Indexing remains useful when the tuple should stay together or only selected elements are needed.
Mistakes with Tuple Returns
Treating a tuple return as several independent return channels
The function packages the values into one tuple object before the caller receives them.
Fix:
Receive the tuple as one object, or unpack its elements into variables.Using the wrong number of variables during unpacking
Tuple unpacking requires the variables on the left side to match the number of returned tuple elements.
Fix:
Use a matching number of variables, or keep the tuple together and access the needed elements by index.Confusing indexing with unpacking
Indexing accesses elements through positions, while unpacking assigns elements to variables in order.
Fix:
Use unpacking when the known returned values should be handled as separate variables.Forgetting that order controls assignment
Python assigns each element to the variable in the same position.
Fix:
Place the variables in the same order as the values returned by the function.
Practice the Choice
A function returns a tuple containing a processing result and metadata about how long the operation took. Describe which approach you would choose in each situation: keep both pieces together for later use, access only the first piece, or work with both pieces through descriptive variable names.
Hints
- Keeping the returned values together suggests treating the tuple as one object.
- Accessing only one element can be done with bracket notation.
- Using both values as individual variables is the main use case for unpacking.
What do you think happens?
A function returns two tuple elements. If the caller writes two variables on the left side of the assignment, how are the elements assigned?
Reveal answer
Answer: The first element goes to the first variable and the second goes to the second variable.
Tuple unpacking matches the number of variables with the number of tuple elements and assigns corresponding elements in order.
The Complete Pattern
- A function can communicate multiple values by packaging them into one tuple.
- The comma in the return statement is what matters for creating the tuple; parentheses are optional.
- Tuple unpacking distributes returned elements into separate variables in order.
- Use unpacking when the number of returned values is known and the values should be handled individually.
- Use indexing when you want to keep the tuple together or need only some of its elements.
Key Takeaways
- Functions return one tuple object when multiple values are separated by commas in a return statement.
- Tuple unpacking assigns tuple elements to separate variables in corresponding order.
- The number of variables used for unpacking must match the number of returned tuple elements.
- Indexing and unpacking are both valid, but unpacking is preferred when known values should be used individually.
- Indexing is useful when values should remain together or only selected tuple elements are needed.