Python functions can return multiple values by using tuples. A tuple is a data structure that can hold multiple elements, and it's often used to group related values together. When a function returns multiple values, it actually returns a single tuple containing those values.
Here's an example of a function that returns multiple values:
def get_user_info(): name = "Alice" age = 30 email = "[email protected]" return name, age, email # Calling the function and unpacking the returned tuple user_name, user_age, user_email = get_user_info() print("Name:", user_name) print("Age:", user_age) print("Email:", user_email)
In this example, the get_user_info()
function returns three values: name
, age
, and email
. These values are automatically packed into a tuple, which is then unpacked when the function is called, assigning each value to a separate variable.
Keep in mind that you can also return multiple values using other data structures like lists or dictionaries, but tuples are often preferred for this purpose because they are lightweight and immutable.
In Python, you can elegantly ignore some return values of a function using an underscore (_
). The underscore is often used as a convention to indicate that the value is being intentionally ignored. This is particularly useful when you're only interested in some of the return values of a function and don't need to use them.
Here's an example:
def get_data(): return 10, 20, 30 result1, _, result3 = get_data() print(result1) # 10 print(result3) # 30
In this example, the function get_data()
returns a tuple (10, 20, 30)
. We use _
as a placeholder for the second value that we're not interested in. This makes it clear that the value is being ignored intentionally.
Keep in mind that this convention is just a convention and doesn't have any special meaning in the Python language itself. Some Python linters and tools might recognize _
as a variable that's intended to be ignored and may not raise warnings or linting errors for unused variables named _
.
If you want to annotate a Python function with multiple possible return types, including None
, you can use the Optional
type from the typing
module. The Optional
type indicates that the value can be of the specified type(s) or None
. Here's how you can use it:
from typing import Optional def my_function(input_value: int) -> Optional[str]: if input_value > 0: return "Positive" elif input_value < 0: return "Negative" else: return None # Return None for 0 result = my_function(5) if result is not None: print(result.upper()) else: print("Input is zero")
In this example, the my_function
can return a string value ("Positive" or "Negative") or None
. The type hint Optional[str]
indicates that the return value can be either a string or None
.
When you call the function, you can use the returned value as you would with any other optional value in Python, checking for None
before performing operations.
Remember that using type annotations and type hints like this can be helpful for static analysis using tools like mypy
to catch potential type-related issues in your code.