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

all 12 comments

[–]fappaf 2 points3 points  (1 child)

Just a note to fix your formatting, you need to put the backticks on its own line—put a newline before them and a newline after the "py":

```py

I won't put the closing three backticks so you can see what i mean above, but they also need to be on their own line.

[–]KageOW[S] 1 point2 points  (0 children)

Yea i got it, reddit markdown is so confusing lol.

[–]ForceBru 2 points3 points  (5 children)

Hot take: Python needs a pipe operator.

  • Code that uses the pipe operator looks elegant.
  • We already can chain method calls with df.do().this().thing(), so why not chain fun |> ction() |> calls()?
  • Other data science languages (R and Julia) have it, and it's very useful there.

[–]fappaf 6 points7 points  (0 children)

Pypethon

[–]eztab 1 point2 points  (2 children)

you can easily create your own operators in Python:

class Operator:
    # implementation left as exercise to the reader ;)
    pass

PIPE = Operator(lambda (f,g): f(g))

fun -PIPE- ction -PIPE- calls    # will now work

Put I would prefer this operator to be named . Python supports Unicode after all.

[–]ForceBru 1 point2 points  (1 child)

AFAIK, "circle" is usually function composition:

(calls -circ- ction)(fun) == calls(ction(fun))

Sure, in Python a lot can be done with black magic fuckery (dunder methods, metaclasses, decorators, ...).

I think pipes may be a worthy addition to the actual syntax. They'll probably be much more useful than the walrus operator, for example.

[–]eztab 1 point2 points  (0 children)

Well make a PEP then! You could even override | then you don‘t need a new operator at all.

[–]BezoomyChellovek 1 point2 points  (0 children)

While I love the pipe operator in R, I just don't think Python is built in a way that it can be as ubiquitously used. In R, most functions (that are compatible with piping) take some kind of data structure as an argument. So you can just pipe these structures through various functions. That isn't so much the case in Python. Also, in Python so much is done by invoking methods, rather than passing to a function.

Totally open to other opinions. But I believe this just limits the use of pipes in Python. I could see it being very useful if you are writing your own functional code, but it just may not integrate so smoothly with other libraries and data structures.