You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My idea is to create an intermediated class like _MathExpression:
classAttr: # Just ignore the name for nowdef__init__(self, attr: str) ->None:
self._attrgetter=operator.attrgetter(attr)
def__call__(self, *args, **kwargs) ->Any:
iflen(args) ==1:
returnself._attrgetter(args[0])
returnlambdaobj: self._attrgetter(obj)(*args, **kwargs)
This implementation has a limitation, they won't work correctly if we try to call a method with one argument. This is hard because we don't know when the attribute is callable!
One possible concept is to pull the bytecode of the calling frame, then walking through it following the stack changes, to get all calls within the "scope" of the lambdas usage.
# pip install lambdazfromlambdazimporta0# a0 is the first argument with index 0# `_` at the end is used as a termination sign. After accessing this attribute, the expression becomes callablefn=a0.strip().title()[::-1]._fn(" drow lleh ")
Activity
internetimagery commentedon Jul 18, 2021
One possible concept is to pull the bytecode of the calling frame, then walking through it following the stack changes, to get all calls within the "scope" of the lambdas usage.
eg https://gist.github.com/internetimagery/05082fac28bc17860ec23fa0d7172df7
gtors commentedon Oct 3, 2021
Another alternative:
PrVrSs commentedon Jun 15, 2023
Some ideas:
If need support only single call
_.get('some_key')We can use closure with methodcaller.If need support chain of fn call such as
_.get('a').get('b').