A Function Composition Operator In Python
Solution 1:
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).
Solution 2:
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
classComposable:def__init__(self, func):
self.func = func
functools.update_wrapper(self, func)
def__getattr__(self, othername):
other = globals()[othername]
returnlambda *args, **kw:self.func(other.func(*args, **kw))
def__call__(self, *args, **kw):
returnself.func(*args, **kw)
To test:
@Composable
def add1(x):
return x + 1@Composable
def add2(x):
return x + 2
print((add1.add2)(5))
# 8
Solution 3:
You can step around the limitation of defining composable functions exclusively in the global scope by using the inspect
module. Note that this is about as far from Pythonic as you can get, and that using inspect
will make your code that much harder to trace and debug. The idea is to use inspect.stack()
to get the namespace from the calling context, and lookup the variable name there.
import functools
import inspect
classComposable:def__init__(self, func):
self._func = func
functools.update_wrapper(self, func)
def__getattr__(self, othername):
stack = inspect.stack()[1][0]
other = stack.f_locals[othername]
return Composable(lambda *args, **kw:self._func(other._func(*args, **kw)))
def__call__(self, *args, **kw):
returnself._func(*args, **kw)
Note that I changed func
to _func
to half-prevent collisions should you compose with a function actually named func
. Additionally, I wrap your lambda in a Composable(...)
, so that it itself may be composed.
Demonstration that it works outside of the global scope:
defmain():
@Composabledefadd1(x):
return x + 1 @Composabledefadd2(x):
return x + 2print((add1.add2)(5))
main()
# 8
This gives you the implicit benefit of being able to pass functions as arguments, without worrying about resolving the variable name to the actual function's name in the global scope. Example:
@Composabledefinc(x):
return x + 1defrepeat(func, count):
result = func
for i inrange(count-1):
result = result.func
return result
print(repeat(inc, 6)(5))
# 11
Post a Comment for "A Function Composition Operator In Python"