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.

classification
Title: New function proposal: string.from_iterable(iterable [,map_function])
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, josh.r, r.david.murray, rhettinger, youtux
Priority: normal Keywords: patch

Created on 2015-01-06 23:27 by youtux, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
string.from_iterable.patch youtux, 2015-01-06 23:27 The proposed enhancement review
Messages (6)
msg233554 - (view) Author: Alessio Bogon (youtux) * Date: 2015-01-06 23:27
I would like to suggest a new string function/constructor: 
string.from_iterable(iterable [,map_function])

I think that the behaviour is intuitive: given an iterable, it construct a string using its element by apply a `map_function`, if provided, to each one of them. After that the str() constructor will be applied to each element in any way, to ensure that effectively an iterable of strings is used.

Of course I do not expect that you will accept this patch, but I think this really is a missing piece of the python library.

You can argue that I could just use
"".join(iterable)
but in my opinion there are two problems:
1) if any of the elements of `iterable` is not a `str` instance, it will fail miserably;
2) this is not very pythonic.

This issue is meant to be an idea for the python maintainers, so I did not write the corresponding `Doc/libary/string.rst` documentation, but if you are interested I could do it.

Thank you people for your amazing work.
msg233555 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2015-01-06 23:34
You should propose this to the python-ideas mailing list.
FWIW, if this is accepted, I would use str as default map_function.
msg233556 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-01-07 02:09
Thanks for wanting to contribute.  However, I don't see why this function would be beneficial.  As indicated by the patch, it is simply

  ''.join(str(map_function(x)) for x in iterable)

but without the inherent flexibility of the above formulation.  Which looks pretty Pythonic to my eyes :).

We often say "not every one line expression deserves to be a function."

But yes, python-ideas would be the appropriate forum to discuss such a thing.  This issue can be reopened if you get consensus that it is a valuable addition.
msg233557 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2015-01-07 02:11
I'll note: "".join(map(str, iterable)) will solve problem #1. It's fast, uses existing built-ins, and is relatively intuitive. I know map is sometimes considered Pythonic, but "".join(str(x) for x in iterable) is an equally effective, if somewhat slower, alternative that can't be called un-Pythonic.

I have no idea why you think "".join(iterable) is not Pythonic, it's *the* way to join strings into a single string. Fast, direct, simple. string.from_iterable seems like a silly way to do what "".join(map(INSERTFAVORITEFUNCTIONHERE, iterable)) does already, for no gain.
msg233559 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2015-01-07 02:11
Correction:  I know map is sometimes considered *un-Pythonic
msg233567 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-01-07 07:18
I concur with Josh and David.  We already have a way to do it and there doesn't seem to be anything here that would warrant further growth of the already large number of string methods.
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67368
2015-01-07 07:18:17rhettingersetresolution: later -> rejected

messages: + msg233567
nosy: + rhettinger
2015-01-07 02:11:51josh.rsetmessages: + msg233559
2015-01-07 02:11:09josh.rsetnosy: + josh.r
messages: + msg233557
2015-01-07 02:09:30r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg233556

resolution: later
stage: resolved
2015-01-06 23:34:50ezio.melottisetnosy: + ezio.melotti
messages: + msg233555
2015-01-06 23:27:18youtuxcreate