r/Python • • Jan 29 '20

I Made This I wrote a small library to enable flexible piping in Python, and finally happy with my live rendering setup in vim. Figured I'd make a small demo.

1.1k Upvotes

74 comments sorted by

View all comments

Show parent comments

0

u/bythenumbers10 Jan 29 '20

But that's all? Just syntax? No performance improvement, and a tradeoff in readability paid for in debug-ability? I thought I was missing something, like streaming large data/input sources. Cutesy syntactical sugar like this in Python is all kinds of un-Pythonic, given "import this".

31

u/Dogeek Expert - 3.9.1 Jan 29 '20

Readability counts.

print(numpy.tan(numpy.array([1, 2, 3])))

is way harder to read than

[1, 2, 3] >> Array >> Tan >> Print

Obviously, it's not for every workflow. It's pretty useful when handling lots of data - you see the above syntax a lot when dealing with lots of vectorized data. If you're used to bash piping, it's actually more readable.

1

u/Aedan91 Jan 30 '20

Millennials discover piping. Signed: Erlang.

1

u/FruityWelsh Jan 29 '20

I very much prefer >> for readabilty.

Couldn't it still be [1, 2, 3] >> numpy.array >> numpy.tan >> print or do you have to create and use the new object?

2

u/Dogeek Expert - 3.9.1 Jan 29 '20

well, there is no reason why you could use piping like that as is. You have to create new objects.

The only way I can see this feature be implemented for every object would be to change the base class (object) at the root, to force that new behavior in. That requires you to change the core python code, and recompile it. It also wouldn't work on classes that overload the rshift operator.

2

u/FruityWelsh Jan 29 '20

yeah :/ honestly I think I would prefer just changing the lshift and rshift operators, so I could use this instead.

But I'm just lazy and opinionated, have there been any discussions about adding a piping operator before to base language?

1

u/p10_user Jan 30 '20

| But that's all? Just syntax?

It's a fair point. I'm guessing that you aren't primarily using Python for data science, and therefore might not have a strong need for this. Maybe I'm wrong and you just don't care about this, and are (legitimately) put off with how un-Pythonic this is - and that's fine.

For me, having a piping capability is something I've wanted to have and work well in Python. As someone who also uses R I quickly gravitate to the dplyr way of doing things with pipes. I'm always making use of chaining methods in pandas when possible. This looks like the most promising way to achieve something similar in all of Python to date.

0

u/[deleted] Jan 29 '20

[deleted]

1

u/bythenumbers10 Jan 30 '20

I know about Julialang, but their approach is markedly different under the hood for piping and execution in general, where this "piping syntax" implementation for Python doesn't add anything, and arguably costs a fair bit in performance and debugging, on top of not being Pythonic in terms of one (hopefully obvious) way to do something.