You can use x = func()[0] to return the first value, x = func()[1] to return the second, and so on.
If you want to get multiple values at a time, use something like x, y = func()[2:4].
You can use x = func()[0] to return the first value, x = func()[1] to return the second, and so on.
If you want to get multiple values at a time, use something like x, y = func()[2:4].
One common convention is to use a "_" as a variable name for the elements of the tuple you wish to ignore. For instance:
def f():
return 1, 2, 3
_, _, x = f()
Videos
I'm in 11th grade. My class is having a big discussion over this. So I chose to ask you all what is the truth.
Can functions in python return more than 1 value?
I told my teacher no they can't because the function returns a tuple containing multiple values so it's technically a single object that is getting returned (when we write something like return v1,v2 ) and when we are assigning those values to variables, we are just unpacking tuples.
I really need your help. What do you think?
Edit: Thank you all for your answers. I have come to the conclusion that this debate is more semantic/linguistic than technical. I have decided to stick to what my teacher teaches me just so I can get the correct answer on the school tests.
Hello developers, I want to know about how can I return multiple values from a function. I have read few articles but still confused.
Named tuples were added in 2.6 for this purpose. Also see os.stat for a similar builtin example.
>>> import collections
>>> Point = collections.namedtuple('Point', ['x', 'y'])
>>> p = Point(1, y=2)
>>> p.x, p.y
1 2
>>> p[0], p[1]
1 2
In recent versions of Python 3 (3.6+, I think), the new typing library got the NamedTuple class to make named tuples easier to create and more powerful. Inheriting from typing.NamedTuple lets you use docstrings, default values, and type annotations.
Example (From the docs):
class Employee(NamedTuple): # inherit from typing.NamedTuple
name: str
id: int = 3 # default value
employee = Employee('Guido')
assert employee.id == 3
For small projects I find it easiest to work with tuples. When that gets too hard to manage (and not before) I start grouping things into logical structures, however I think your suggested use of dictionaries and ReturnValue objects is wrong (or too simplistic).
Returning a dictionary with keys "y0", "y1", "y2", etc. doesn't offer any advantage over tuples. Returning a ReturnValue instance with properties .y0, .y1, .y2, etc. doesn't offer any advantage over tuples either. You need to start naming things if you want to get anywhere, and you can do that using tuples anyway:
def get_image_data(filename):
[snip]
return size, (format, version, compression), (width,height)
size, type, dimensions = get_image_data(x)
IMHO, the only good technique beyond tuples is to return real objects with proper methods and properties, like you get from re.match() or open(file).
Push them onto the stack
This really only applies to stack-based languages, and perhaps Assembly. The good thing about this approach is that its somewhat more natural to work with the returned values. If you do divmod, you can pop once to get the quotient, pop the next element to get the remainder. The downside is that if you want to call a function that returns multiple values and immediately pass them on, it might be a bit cumbersome, depending on the language.
Custom types
In languages such as Java and Kotlin, which really love their nominal typing, you would make a new class specific to that one function. For example, divmod might return a DivmodResult object with fields quotient and remainder (Kotlin does have a Pair class in its standard library, but it's dropped actual tuples now).
Advantages:
- When you have to return 4 or more values, it really helps to have a class specifically for that purpose if you have a statically typed language. I'd rather see a return type of
FooResultthan some enormous tuple like(int, String, BarProducer, String, int)
Disadvantages:
- Making a whole class for a single function is annoying. Some languages do have syntactic sugar for this (e.g.
data classin Kotlin) but it's so much easier to use something like tuples or lists instead. In Java, you'll have to make getters and setters and constructors for a minor class that might only be used in one or two places.