Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A function composition operator in Python

In this question I asked about a function composition operator in Python. @Philip Tzou offered the following code, which does the job.

import functools

class Composable:

    def __init__(self, func):
        self.func = func
        functools.update_wrapper(self, func)

    def __matmul__(self, other):
        return lambda *args, **kw: self.func(other.func(*args, **kw))

    def __call__(self, *args, **kw):
        return self.func(*args, **kw)

I added the following functions.

def __mul__(self, other):
    return lambda *args, **kw: self.func(other.func(*args, **kw))

def __gt__(self, other):
    return lambda *args, **kw: self.func(other.func(*args, **kw))

With these additions, one can use @, *, and > as operators to compose functions. For, example, one can write print((add1 @ add2)(5), (add1 * add2)(5), (add1 > add2)(5)) and get # 8 8 8. (PyCharm complains that a boolean isn't callable for (add1 > add2)(5). But it still ran.)

All along, though, I wanted to use . as a function composition operator. So I added

def __getattribute__(self, other):
    return lambda *args, **kw: self.func(other.func(*args, **kw))

(Note that this fouls up update_wrapper, which can be removed for the sake of this question.)

When I run print((add1 . add2)(5)) I get this error at runtime: AttributeError: 'str' object has no attribute 'func'. It turns out (apparently) that arguments to __getattribute__ are converted to strings before being passed to __getattribute__.

Is there a way around that conversion? Or am I misdiagnosing the problem, and some other approach will work?

like image 804
RussAbbott Avatar asked Jan 12 '19 17:01

RussAbbott


People also ask

What is function composition operator?

In mathematics, function composition is an operation ∘ that takes two functions f and g, and produces a function h = g ∘ f such that h(x) = g(f(x)). In this operation, the function g is applied to the result of applying the function f to x.

What is the meaning of function composition?

Function composition is the combination of two function to form a new function. One simply takes the output of the first function and uses it as the input to the second function.

How do you use the composition of a function?

The composition of functions f(x) and g(x) where g(x) is acting first is represented by f(g(x)) or (f ∘ g)(x). It combines two or more functions to result in another function. In the composition of functions, the output of one function that is inside the parenthesis becomes the input of the outside function.

Why do we use composition function?

You use composite functions whenever you buy a sale (discounted) item. When you are standing in the store trying to decide if you can afford the item, the first thing you calculate is the discount.


2 Answers

You can't have what you want. The . notation is not a binary operator, it is a primary, with only the value operand (the left-hand side of the .), and an identifier. Identifiers are strings of characters, not full-blown expressions that produce references to a value.

From the Attribute references section:

An attribute reference is a primary followed by a period and a name:

attributeref ::=  primary "." identifier

The primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is then asked to produce the attribute whose name is the identifier.

So when compiling, Python parses identifier as a string value, not as an expression (which is what you get for operands to operators). The __getattribute__ hook (and any of the other attribute access hooks) only has to deal with strings. There is no way around this; the dynamic attribute access function getattr() strictly enforces that name must be a string:

>>> getattr(object(), 42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: getattr(): attribute name must be string

If you want to use syntax to compose two objects, you are limited to binary operators, so expressions that take two operands, and only those that have hooks (the boolean and and or operators do not have hooks because they evaluate lazily, is and is not do not have hooks because they operate on object identity, not object values).

like image 120
Martijn Pieters Avatar answered Sep 25 '22 23:09

Martijn Pieters


I am actually unwilling to provide this answer. But you should know in certain circumstance you can use a dot "." notation even it is a primary. This solution only works for functions that can be access from globals():

import functools

class Composable:

    def __init__(self, func):
        self.func = func
        functools.update_wrapper(self, func)

    def __getattr__(self, othername):
        other = globals()[othername]
        return lambda *args, **kw: self.func(other.func(*args, **kw))

    def __call__(self, *args, **kw):
        return self.func(*args, **kw)

To test:

@Composable
def add1(x):
    return x + 1

@Composable
def add2(x):
    return x + 2

print((add1.add2)(5))
# 8
like image 30
Philip Tzou Avatar answered Sep 26 '22 23:09

Philip Tzou