[Python-ideas] values in vs. values out

K. Richard Pixley rich at noir.com
Mon Jan 17 19:09:34 CET 2011


You could shorten this...

    def __call__(self):
         return self.avg, self.stdev

Now it's even more dense and allows for indexing the results:

     p = Statistics([4, 5, 6])()[0]

--rich

On 1/17/11 09:54 , K. Richard Pixley wrote:
> 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



More information about the Python-ideas mailing list