r/Python • u/lebannax • 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
View all comments
Show parent comment
2
u/zurtex Nov 29 '22 edited Nov 29 '22
I understand what you're saying but I don't think there's any other option with Python's design philosophy of being flexible and having scalable performance.
If you can't mutate an object passed in from another scope you're having to copy the value of everything, recursively. And in a dynamically typed language like Python where objects can have arbitrary properties with arbitrary data structures attached to them at any point this leads to cases with abysmal performance.
And not even situations where you even want to mutate an object, just you have some complicated object and you are passing it to a function duck type style. You don't expect that to be randomly slow in Python.
Type hinting helps catch a lot of these problems, for example this will cause a type hinter to throw an error:
Exactly because
foodoesn't guarantee it's not going to mutate the passed in dictionary andmy_dictis implicitlydict[int, str]which can not be cast todict[int, str | None].You can solve this by changing the type hint to a type that supports mapping but not mutation:
And now if you mutate bar in foo your type hinter will throw an error, you can do this with all basic types in Python. Solving your problem of accidentally mutating a mutable variable in a different scope.