Python Forum

Full Version: what i repeatedly keeping wanting in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
many times i encounter uses for this one capability i wish Python had. i guess this is because of encountering other programming languages in the past that could do something like this. of course, our view of things today is influenced by what we have seen in the past.

what i would like to have in Python is the ability to add new methods to builtin classes like int and str. and this would be a change to the class referenced as its class by every existing object even though these objects created before this change to the class. so if i add a new method to str after i have many str instances, the change applies to them all. same for all other builtin classes.

instead, i end up creating functions that need to have the object passed to it, which means checking the type of the passed data in the one function. with this method add-on feature there would be specific methods for each class, elimination a lot of testing.

Python is flexible. this would make it more flexible.
Generic functions apply to builtin types and they are very close to methods.
i created 2 functions i think would be better as methods. one, called begins(), takes a string in arg1 and checks to see if that string begins with any of the strings given in the remaining arguments, or, if arg2 is a list or tuple, begins with any of the strings given in that list or tuple. the other function. called ends(), does the same thing but checks the end of the main string. these 2 functions are certainly not hard to code. but i think code using these functions is easier to read. IMHO, it would be even easier if they could be coded as methods. but, i'd have to create or patch a Python implementation to do it. and it would not be well known in that case. the benefit would only be realized if people knew Python could do this.
Personally, I'm glad python doesn't let you modify builtin types. Javascript has that, and it makes reading through some libraries/frameworks quite painful, since you don't really know if they're using the standard Array.slice, or if Array.slice never existed and they added it, or if they just replaced the builtin Array.slice with their own custom version.

Especially since there's nothing string specific about this particular case. "begins" could just as easily apply to any iterable such as lists.

And also it's just a one-line function, really makes me glad it can't be augmented to all strings.

tl;dr: I'm glad this isn't valid: str.begins = lambda s, args: any(s.startswith(substr) for substr in args)
if the capability really does make code harder to read, then it certainly isn't worth it. so theory is that a given builtin class needs to remain finite and never be infinite because people will learn the class in its original form and would have to effectively relearn it just for one use-case (if that were even clear as very dynamic additions could possibly be done).