[Python-ideas] values in vs. values out

K. Richard Pixley rich at noir.com
Mon Jan 17 18:54:20 CET 2011


If the values involved are sufficiently weakly related, then I question 
whether it's appropriate to calculate them at all.  If the most frequent 
use is to select out a subset of the values, then even calculating the 
other values seems like a wasted effort.

To take "average" and "stdev" as an example...

If you use an object to represent not the range of return values, but 
the domain of input values, then you can use @property accessors for the 
results.

class Statisics(object):
     def __init__(self, list):
         self.list = list

     @property
     def avg(self):
         return ...

     @property
     def stdev(self):
         return ...

     @property
     def inputs(self):
         return self.list

     @property
     def outputs(self):
         return self.avg, self.stdev

Now you have the syntactic appearance of selecting from multiple values 
in either one step or two, your choice.

     x = Statistics([1, 2, 3]).stdev
     y, z = Statistics([1, 2, 3]).outputs

     p = Statistics([4, 5, 6])
     q = p.avg

--rich




More information about the Python-ideas mailing list