MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Python/comments/iohrl/an_alternative_lambda_syntax_for_python/c25k9c8/?context=3
r/Python • u/vocalbit • Jul 13 '11
41 comments sorted by
View all comments
2
The operator module has some functions that can be used in most places of lambdas:
lst.sort(key=attrgetter('name'))
Or partial from functools:
lst.sort(key=partial(somefunc, kwd=x))
Though that wouldn't work with getattr which only accepts positional arguments and we need to skip one.
getattr
You could also easily make a class that produces functions based on actions on its instances, e.g.:
class _Getter(object): def __getattr__(self, name): return lambda o: getattr(o, name) def __getitem__(self, key): return lambda o: o[key] get = _Getter() del _Getter lst.sort(key=get.name)
2
u/donri Jul 14 '11
The operator module has some functions that can be used in most places of lambdas:
Or partial from functools:
Though that wouldn't work with
getattrwhich only accepts positional arguments and we need to skip one.You could also easily make a class that produces functions based on actions on its instances, e.g.: