function operators

Martin von Loewis loewis at informatik.hu-berlin.de
Tue Nov 27 09:50:04 CET 2001


"James A. H. Skillen" <jahs at jahs.org> writes:

> but wouldn't:
> 
>     f = cos + sin
> 
> be *much* nicer?

If a functional notation of operators is acceptable, then this might
help as well.

import operator
class Combiner:
   def __init__(self,f1,f2):
      self.f1=f1
      self.f2=f2

   def __call__(self,*args):
      return self.combine(self.f1(*args),self.f2(*args))

class Plus(Combiner):
   combine=operator.add

import math
sincos = Plus(math.sin,math.cos)

print sincos(2)

HTH,
Martin



More information about the Python-list mailing list