[BangPypers] Return values

Noufal Ibrahim KV noufal at nibrahim.net.in
Sat Sep 20 13:45:40 CEST 2014


On Sat, Sep 20 2014, Sriram Karra wrote:


[...]

> But your problem can be addressed by naming the functions after what they
> do - after all your consolidate function does something tangible other than
> just return a different type of data.

[...]

Let me give you a specific example. 

The psutils library has a function to get cpu times (idle, busy
etc.). It can either do this per CPU (in which case, it will return a
list of CPUTime objects, one per CPU) or give you the total (in which
case, you'll get back a single CPUTime object). This is controlled by a
per_cpu boolean flag.

You can write a separate method to sum the individual times and then
keep the "conslidation" outside get_cpu_times.

The library chooses to sometimes return a list and sometimes return a
single item. I don't like that. 

Now, I can write two functions like get_cpu_times and
get_cpu_times_per_cpu. I don't particularly like that either.

>From the thread so far, Anand's solution is the one I like
best. Although it's something tailored for my problem rather than a
general pattern.


-- 
Cordially,
Noufal
http://nibrahim.net.in


More information about the BangPypers mailing list