[BangPypers] Return values

Balachandran Sivakumar benignbala at gmail.com
Thu Sep 25 05:13:27 CEST 2014


On Sat, Sep 20, 2014 at 2:34 PM, Noufal Ibrahim KV
<noufal at nibrahim.net.in> wrote:
> 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 generally try to make the return type consistent. I will do
something like

if consolidated:
    return [object]
    return the_list_of_objects

What I also do is, the object will have a consolidated flag as well.
The the code that invokes this will have to do:

stats = get_stats(consolidated = True)
for stat in stats:
     if stat.consolidated:
        do something.
         do something else

I run into this problem frequently because, there is a tool(framework)
that I write at office that is used by multiple teams. So, to keep
stuff consistent, I always return the same type and let the user
handle the result the way he/she wants. Thanks

Thank you
Balachandran Sivakumar

More information about the BangPypers mailing list