[BangPypers] Return values

Sriram Karra karra.etc at gmail.com
Sat Sep 20 18:27:20 CEST 2014

On Sat, Sep 20, 2014 at 5:15 PM, Noufal Ibrahim KV <noufal at nibrahim.net.in>

> 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.

Anand's solution is good. But with the added context you have given - why
are you not creating classes for CPU and Machine, with get_stats() and
set_stats() methods for them? Something like:

class CPU:
    def set_stats (self, cpu_t):
        self.cpu_stats = cpu_t

    def get_stats (self):
        return self.cpu_stats

class Machine:
    def __init__ (self, n_cpus):
        self.cpus = [CPU() for i in range(0, n_cpus)]

    def fetch_stats (self):
        stats_list = psutils.get_cpu_stats()
        for i, stat in enumerate(stats_list):

        self.cpu_stats_summary = psutils.get_cpu_stats(summary=True)

    def get_cpu_stats_summary (self):
        return self.cpu_stats_summary

More information about the BangPypers mailing list