Message388983
Rather than just erroring-out, it would be nice if str.join converted inputs to strings when needed.
Currently:
data = [10, 20, 30, 40, 50]
s = ', '.join(map(str, data))
Proposed:
s = ', '.join(data)
That would simplify a common idiom. That is nice win for beginners and it makes code more readable.
The join() method is unfriendly in a number of ways. This would make it a bit nicer.
There is likely to be a performance win as well. The existing idiom with map() roughly runs like this:
* Get iterator over: map(str, data)
* Without length knowledge, build-up a list of strings
periodically resizing and recopying data (1st pass)
* Loop over the list strings to compute the combined size
(2nd pass)
* Allocate a buffer for the target size
* Loop over the list strings (3rd pass), copying each
into the buffer and wrap the result in a string object.
But, it could run like this:
* Use len(data) or a length-hint to presize the list of strings.
* Loop over the data, converting each input to a string if needed,
keeping a running total of the target size, and storing in the
pre-sized list of strings (all this in a single 1st pass)
* Allocate a buffer for the target size
* Loop over the list strings (2nd pass), copying each
into the buffer
* Loop over the list strings (3rd pass), copying each
into the buffer and wrap the result in a string object.
AFAICT, the proposal is mostly backwards compatible, the only change is that code that currently errors-out will succeed.
For bytes.join() and bytearray.join(), the only auto-conversion that makes sense is from ints to bytes so that you could write:
b' '.join(data)
instead of the current:
b' '.join([bytes([x]) for x in data]) |
|
Date |
User |
Action |
Args |
2021-03-18 02:29:06 | rhettinger | set | recipients:
+ rhettinger, pablogsal |
2021-03-18 02:29:06 | rhettinger | set | messageid: <1616034546.27.0.0632217994438.issue43535@roundup.psfhosted.org> |
2021-03-18 02:29:06 | rhettinger | link | issue43535 messages |
2021-03-18 02:29:05 | rhettinger | create | |
|