Usually, this kind of operation is performed using a pipe operator in other languages (R, Elixir, Unix shell, etc.), As a language polyglot, this is the one killer feature that I like the most that’s absent in Python, it makes the code look so much more clean. Compare:
result <- filter(data, Age >= 25)
result <- select(result, Name, Score)
result <- arrange(result, desc(Score))
to
result <- data |>
filter(Age >= 25) |>
select(Name, Score) |>
arrange(desc(Score))
In Python, to some extent this can be achieved through a fluent interface, though you’re not always working within the limits of one object. I am not sure whether pipe operator syntax would fit Python well. I would, however, appreciate it if functools
had a functional composition function. That would make this kind of code much readable without introducing much sacrifice in terms of operator inflation.
from functools import compose
pipeline = compose(map(fun=lambda x:x*2), list)
pipeline(lst)
An inspiration can be taken from source code of toolz/cytoolz.