operator on functions

Scottie me at nospam.net
Fri Apr 6 16:35:23 EDT 2001


I'll put this in the Cookbook entry I'm preparing, along with curry.

class compose:
    '''Compose two functions.'''
    def __init__(self, f, g):
        self.f = f
        self.g = g

    def __call__(self, *arg, **kwarg ):
        return self.f(self.g(*arg, **kwarg))

test:
    from math import sin, cos
    sincos = compose(sin, cos)
    cossin = compose(cos, sin)
    for x in range(10):
        assert sincos(x) == sin(cos(x))
        assert cossin(x) == cos(sin(x))


-Scott David Daniels






More information about the Python-list mailing list