[BangPypers] Return values

Noufal Ibrahim KV noufal at nibrahim.net.in
Sat Sep 20 12:48:42 CEST 2014

On Sat, Sep 20 2014, Harish Vishwanath wrote:

> Couple of approaches:
> - Break the api into two. get_stats and get_stats_consolidated. This way,
> the caller who doesn't know what is your default value of "consolidated"
> argument is will not be confused.

This explodes the API. It's similar to having two sort functions
'sort_increasing', and 'sort_decreasing' instead of just a single sort
function with a flag to indicate ordering.

> - Change the return value to be a list always. Without "consolidated" set,
> it will just be [Stats], else [stat1, stat2, stat3..]

That's what I'd do if it were C. An array. However, in python, when I
know that I'm going to return a list with only one item, I'd prefer to
just return it. Otherwise, there will be stuff like 

  if consolidated:
     stats = stats.pop()

and other such nasties in the code.

> - More descriptive return value could be a dict:
> {
>   'consolidated': True|False
>   'value' : []
> }

That's an option though not one that occurred to me. However, then I'd
have to unpack this dictionary every time I called the function and
optionally, do checking too.

I'm not shooting down your ideas. These are what occurred to me too. I'm
just looking for something "neater".



More information about the BangPypers mailing list