Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit on the number of values returned by a Python function/method

I was working with some code and realised that things became a lot easier when a single method was returning 4 values. Working with traditional languages like C++ and Java where methods return only a single value, I came to wonder

Is there a limit on the number of values returned by a Python function?

Stuff like this works effortlessly. Is there a limit to it?

def hello():
    return 1, 2, 3, 4, 5

a, b, c, d, e = hello()
print a, b, c, d, e


Stragely enough, I didn't find any answer to this question

like image 768
bholagabbar Avatar asked Mar 04 '17 18:03

bholagabbar


People also ask

How many return values can be returned from a python function?

A function is not restricted to return a variable, it can return zero, one, two or more values. This is the default property of python to return multiple values/variables which is not available in many other programming languages like C++ or Java.

How many objects can a method return in Python?

Python doesn't return values, it returns objects. To that end, a function can only return one object. That object can contain multiple values, or even refer to other objects, so while your function can pass back multiple values to the caller, it must do so inside of a single object.

How do you limit a value in Python?

NumPy: Limit ndarray values to min and max with clip() clip() or clip() method of ndarray . By specifying the minimum and maximum values in the argument, the out-of-range values are replaced with those values. This is useful when you want to limit the values to a range such as 0.0 ~ 1.0 or 0 ~ 255 .

How many values can be returned by a method?

You can return only one value in Java. If needed you can return multiple values using array or an object.


2 Answers

If you return multiple values, you actually return a tuple. Indeed:

return 1,4,2,5

is short for:

return (1,4,2,5)

So the question boils down to how many items a tuple can carry. The answer can be obtained with:

import sys

sys.maxsize

It is usually 231-1=2 147 483 647 on a 32-bit system and 263-1=9 223 372 036 854 775 807 on a 64-bit system. The strange thing is: it is not 232-1 (or 264-1) so that means that technically speaking if you have a 32-bit machine, and your have 4 GiB in place, the list itself can only fill up half of the memory (of course the elements in the list can be responsible to fill up the remaining half).

Of course the elements also need to fit into memory: if you have no space left to store five million elements, your program will crash anyway.

like image 182
Willem Van Onsem Avatar answered Oct 11 '22 07:10

Willem Van Onsem


Python functions only return a single value: a reference to an object. That object may be of any type, including a tuple (which is what's constructed with return 1, 2, 3, 4, 5). The size of the object is only limited by your computer's available memory.

like image 23
TigerhawkT3 Avatar answered Oct 11 '22 07:10

TigerhawkT3