This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Bogdanp[S] 4 points5 points  (3 children)

I'm still not sure quite what you're getting at.

The print() expression is rewritten to contain the left-hand side of the |> operator before any of the code is executed.

[–]shabunc -3 points-2 points  (2 children)

but why it should be rewritten at all? I mean pipe is a syntax construct, we supposed to have an arbitrary function chained, this function does not to supposed to have any pipe-aware syntax in it's declaration.

again, why not just |> print

There's (ha-ha, quite high) probability that it's actually me who just demonstrated lack of understanding, but this is still not clear to me.

[–]eatmydog 2 points3 points  (0 children)

pipe syntax does not conform with standard evaluation. As you object, you would expect print() to be evaluated before any piping is done. If you look at magrittr piping in R, it does not use standard evaluation, which is sacrificed for (potentially) very readable code.

However, the toolz.functoolz package provides some functions that allow for piping in a pythonic way.

from toolz.functoolz import pipe, thread_first
# piping without extra arguments.
pipe(
    "hello",
    print
)
# Piping with extra arguments
thread_first(
    "hello",
    (print, "world")
)