I know that Python is used for many more things than just data science, so I'd love to hear if in these other contexts, a pipe would also make sense. Just trying to understand why the pipe hasn't made it into Python already.
I know that Python is used for many more things than just data science, so I'd love to hear if in these other contexts, a pipe would also make sense. Just trying to understand why the pipe hasn't made it into Python already.
I find myself increasingly frustrated at seeing code like 'let foo = many lines of code'. Let me write something like 'many lines of code =: foo'.
Interesting idea! However, I'm not sure I would prefer
"Mix water, flour [...] and finally you'll get a pie"
to
"To make a pie: mix water, flour [...]"
It's use is discourages in most style guides. I do not use it in scripts, but I use it heavily in console/terminal workflows where I'm experimenting.
df |> filter() |> summarise() -> x
x |> mutate() -> y
plot(y)
bake(divide(add(knead(mix(flour, water, sugar, butter)),eggs),12),450,12)
versus
mix(flour, water, sugar, butter) %>% knead() %>% add(eggs) %>% divide(12) %>% bake(temp=450, minutes=12)
So much easier!
dough = mix(flour, water, sugar, butter)
dough.knead()
dough = dough.add(eggs)
cookies = dough.divide(12)
cookies = bake(temp=450, minutes=12)
Might be more verbose, but definitely readable. result = (df
.pipe(fun1, arg1=1)
.pipe(fun2, arg2=2)
)
is much less readable than result <- df |>
fun1(., arg1=1) |>
fun2(., arg2=2)
but I guess the R thing also works beyond dataframes which is pretty cool result <- df
|> fun1(arg1=1)
|> fun2(arg2=2)
Python doesn't have a pipe operator, but if it did it would have similar syntax: result = df
|> fun1(arg1=1)
|> fun2(arg2=2)
In existing Python, this might look something like: result = pipe(df, [
(fun1, 1),
(fun2, 2)
])
(Implementing `pipe` would be fun, but I'll leave it as an exercise for the reader.)Edit: Realized my last example won't work with named arguments like you've given. You'd need a function for that, which start looking awful similar to what you've written:
result = pipe(df, [
step(fun1, arg1=1),
step(fun2, arg2=2)
])The functions with extra arguments could be curried, or done ad-hoc like lambda v: fun1(v, arg1=1)
I like exercise:
https://gist.github.com/stuarteberg/6bcbe3feb7fba4dc2574a989...
result = df
|> fun1(arg1=1)
|> fun2(arg2=2)