[BangPypers] Return values

Harish Vishwanath harish.shastry at gmail.com
Sat Sep 20 11:19:02 CEST 2014


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.
- Change the return value to be a list always. Without "consolidated" set,
it will just be [Stats], else [stat1, stat2, stat3..]
- More descriptive return value could be a dict:
{
  'consolidated': True|False
  'value' : []
}


Regards,
Harish

On Sat, Sep 20, 2014 at 2:44 PM, shankha <shankhabanerjee at gmail.com> wrote:

> Can you please tell at how many places you make the call.
>
> That is the problem with these stupid dynamic languages :-)
> On Sep 20, 2014 2:35 PM, "Noufal Ibrahim KV" <noufal at nibrahim.net.in>
> wrote:
>
> >
> > I've recently come across something that I'd like some comments on. It's
> > a stylistic issue so not something that there's an objective answer
> > for. Nevertheless.
> >
> > I have a function that gets some statistics from some source and returns
> > it to the user as a Stats object. Let's call it get_stats. This has a
> > parameter `consolidate`. If consolidate is True, it will combine all the
> > statistics and return just one Stats object. If not, it will return a
> > list of Stats objects.
> >
> > The problem now is that this function sometimes returns a single thing
> > and some times a list. I don't like this since I have to alter my
> > calling code to handle this.
> >
> > I'm surprised that I've never hit this before and I'm not really sure
> > how to handle it.
> >
> > Comments?
> >
> >
> >
> > --
> > Cordially,
> > Noufal
> > http://nibrahim.net.in
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list