How to multiply functions in python?

You can use your hack class as a decorator pretty much as it’s written, though you’d likely want to choose a more appropriate name for the class.

Like this:

class Composable(object):
    def __init__(self, function):
        self.function = function
    def __call__(self, *args, **kwargs):
        return self.function(*args, **kwargs)
    def __mul__(self, other):
        @Composable
        def composed(*args, **kwargs):
            return self.function(other(*args, **kwargs))
        return composed
    def __rmul__(self, other):
        @Composable
        def composed(*args, **kwargs):
            return other(self.function(*args, **kwargs))
        return composed

You can then decorate your functions like so:

@Composable
def sub3(n):
    return n - 3

@Composable
def square(n):
    return n * n

And compose them like so:

(square * sub3)(n)

Basically it’s the same thing you’ve accomplished using your hack class, but using it as a decorator.

Leave a Comment

techhipbettruvabetnorabahisbahis forumu