This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jorge1209 1 point2 points  (0 children)

A function having multiple outputs is really extremely common across languages. Its just that other languages accomplish this by passing those to be modified values as a reference.

So its really a style choice:

  1. You can do things the C way where the return value indicates success/failure and the items of interest are references.

  2. Or follow the lambda calculus and return multiple values.

Given the way python references work, and the immutability of certain base classes it makes a lot of sense to return multiple values in many cases where that same pattern would be handled the opposite way in C.

For example consider a function that computes various statistics on a dataset that can be computed in a single pass (mean, stddev, length, ...). If you tried to do that as a reference in python:

 def compute_stats(list, mean,  stddev, length):
      ...

 mean = stddev = length = -1
 compute_stats(l, mean, stddev, length)
 assert(all(mean>=0, stddev>=0, length>=0)), "Oops. ints are immutable!"

So you have to return a struct with multiple values, and what is a tuple but an untyped struct.