New submission from Raymond Hettinger <[email protected]>:
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])
----------
components: Interpreter Core
messages: 388983
nosy: pablogsal, rhettinger
priority: normal
severity: normal
status: open
title: Make str.join auto-convert inputs to strings.
type: enhancement
versions: Python 3.10
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43535>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com