
Strings are VERY different from other iterables. Every item in a string is itself an (iterables) string. In many ways, strings are more like scalars, and very often we treat them as such. You could make an argument that e.g. a NumPy array of homogenous scalars is similar. However, that would be a wrong argument. Quite literally the ONLY predicate that can be expressed about a single character is it being a member of a subset of all Unicode characters. Yes, you could express that in convoluted ways like it's ord() being in a certain range, but it boils down to subset membership. In contrast, predicates of unlimited complexity can be expressed of numbers. You can ask if an integer is prime. You can ask is the sine of the square of a float is more than pi/4. Arbitrary predicates make sense of arbitrary iterables. This is not so of the characters making up strings strings. On Fri, Apr 30, 2021, 11:08 PM Valentin Berlier <berlier.v@gmail.com> wrote:
It's kind of weird that people seem to be missing the point about this. Python already has comprehensions for all the iterable builtins except strings. The proposed syntax doesn't introduce any new concept and would simply make strings more consistent with the rest of the builtins. The argument that we can already do this with the "".join() idiom is backwards. It's something we have to do _because_ there's no way to write a string comprehensions directly. Comprehensions express intent. Joining a generator expression with an empty string doesn't convey the intent that you're building a string where each character is derived from another iterable.
Also I haven't seen anyone acknowledge the potential performance benefits of string comprehensions. The "".join() idiom needs to go through the entire generator machinery to assemble the final string, whereas a decent implementation of string comprehensions would enable some pretty significant optimizations. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/CYCM35... Code of Conduct: http://python.org/psf/codeofconduct/