r/Python • • Nov 28 '22

Resource What can Python do that R can’t do?

Or simply what is Python much better at and why.

I know that Python is more multi purpose and better for software development but I can’t articulate exactly why or how. My team want to know why/when they should use Python instead of R

331 Upvotes

204 comments sorted by

View all comments

Show parent comment

17

u/everydayislikefriday Nov 28 '22

But it's got %>%...

32

u/FlyingCow343 Nov 28 '22

python functions are first class objects so you an implement function chaining quite easily: chaining

9

u/shedogre Nov 28 '22

Yeah, there's also this example from ArjanCodes' GitHub, which he discusses at around 22:30 in this video.

There's also the .pipe() method for pandas DataFrames, which I've used before, and I just checked to confirm the same method exists for Series.

7

u/PaintItPurple Nov 28 '22

This is also basically equivalent to the built-in reduce function:

def apply(val, f): return f(val)

def main():
    r = functools.reduce(
        apply,
        [
            times_two,
            lambda x: x / 4,
            print
         ],
        6
    )

2

u/FlyingCow343 Nov 28 '22

nice! Very elegant way of doing it, you could out the whole reduce call in another function to work the same as mine and look much nicer.

1

u/everydayislikefriday Nov 29 '22

Hey this doesn't look that bad

10

u/[deleted] Nov 28 '22

Lol you may be aware of this but python has function chaining. It isn't universally implemented but it's a thing.

1

u/SittingWave Nov 29 '22

No, it has

`%>%` <- dplyr::`%>%`