Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)
edited by

After the lengthy preamble, comes the inevitable question. Which method (do you think) is best?

I've typically found myself going the dictionary route because it involves less set-up work. From the perspective of a type, however, you might be better off going the class route, since that may help you avoid confusing what a dictionary represents.

On the other hand, there are some in the Python community that feel implied interfaces should be preferred to explicit interfaces, at which point the type of the object really isn't relevant since you're basically relying on the convention that the same attribute will always have the same meaning.

So, how do -you- return multiple values in Python?

closed

2 Answers

0 votes
by (106k points)
edited by
 
Best answer

To return multiple values from a function:-

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

To know more about this you can have a look at the following video tutorial:-

0 votes
by (20.3k points)

You just need to return a collection of some sort, like a dictionary or a list. You can leave off the extra syntax and just write out the return values, comma-separated. 

Note: this will technically return a tuple.

def f():

    return True, False

x, y = f()

print(x)

print(y)

Result:

True

False

31k questions

32.4k answers

500 comments

693 users

Browse Categories

...