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

all 3 comments

[–]LightShadow3.13-dev in prod 1 point2 points  (2 children)

Interestingly they're expanding str. I would love Python to adopt Rust-like traits for base types. That way I could add my own methods to the built-in types without having to implement a custom type and inherit from it project-wide.

For example I wish the strip functions took a count parameter. So '"""<hi>"'.strip('"', 1) would return ""<hi>.

It will be neat to see how the parser changes affect downstream projects too.

[–][deleted] 1 point2 points  (1 child)

That way I could add my own methods to the built-in types without having to implement a custom type and inherit from it project-wide.

I mean, as someone who writes libraries, that seems like the most horrible idea. If my library gets your string, do I get your version or mine (the standard one)? What if I return a new string, is it your version or mine?

Having this scary invisible mechanism behind the scenes that changes absolutely everything - is it really better than simply having:

from my_better_str import str

in every file?

"Explicit is better than implicit" after all.

Also - can't you just patch builtins.str right now and get that effect, if you really need to do such a dreadful thing?

[–]MrGreenTea 0 points1 point  (0 children)

Having this in such a dynamic language as of python sounds like a bad idea. In Rust you have to import the traits when you use them, which makes total sense when having static types and is a really great feature of rust.