This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients pablogsal, rhettinger
Date 2021-03-18.02:29:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1616034546.27.0.0632217994438.issue43535@roundup.psfhosted.org>
In-reply-to
Content
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])
History
Date User Action Args
2021-03-18 02:29:06rhettingersetrecipients: + rhettinger, pablogsal
2021-03-18 02:29:06rhettingersetmessageid: <1616034546.27.0.0632217994438.issue43535@roundup.psfhosted.org>
2021-03-18 02:29:06rhettingerlinkissue43535 messages
2021-03-18 02:29:05rhettingercreate