Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

additional argument for str.join() #87446

Closed
f-brinkmann mannequin opened this issue Feb 20, 2021 · 4 comments
Closed

additional argument for str.join() #87446

f-brinkmann mannequin opened this issue Feb 20, 2021 · 4 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@f-brinkmann
Copy link
Mannequin

f-brinkmann mannequin commented Feb 20, 2021

BPO 43280
Nosy @stevendaprano, @sweeneyde, @f-brinkmann

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-02-21.01:46:52.863>
created_at = <Date 2021-02-20.20:05:58.618>
labels = ['type-feature', 'library', '3.10']
title = 'additional argument for str.join()'
updated_at = <Date 2021-02-21.01:46:52.862>
user = 'https://github.com/f-brinkmann'

bugs.python.org fields:

activity = <Date 2021-02-21.01:46:52.862>
actor = 'steven.daprano'
assignee = 'none'
closed = True
closed_date = <Date 2021-02-21.01:46:52.863>
closer = 'steven.daprano'
components = ['Library (Lib)']
creation = <Date 2021-02-20.20:05:58.618>
creator = 'f-brinkmann'
dependencies = []
files = []
hgrepos = []
issue_num = 43280
keywords = []
message_count = 4.0
messages = ['387426', '387427', '387430', '387434']
nosy_count = 3.0
nosy_names = ['steven.daprano', 'Dennis Sweeney', 'f-brinkmann']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue43280'
versions = ['Python 3.10']

@f-brinkmann
Copy link
Mannequin Author

f-brinkmann mannequin commented Feb 20, 2021

It would be nice to have an additional argument for str.join() so this

", ".join(["a", "b", "c"], ", and ")

would output

"a, b, and, c"

@f-brinkmann f-brinkmann mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 20, 2021
@f-brinkmann f-brinkmann mannequin changed the title addition argument for str.join() additional argument for str.join() Feb 20, 2021
@f-brinkmann f-brinkmann mannequin changed the title addition argument for str.join() additional argument for str.join() Feb 20, 2021
@sweeneyde
Copy link
Member

This seems like a very specific use case. Too specific IMO for a method on all string objects for anyone using Python anywhere in the world. Why not just write a function like this?

    def my_join(strings, sep=", ", last_sep=", and "):
        strings = list(strings)
        return sep.join(strings[:-1]) + last_sep + strings[-1]
    >>> my_join(["one", "two", "three"])
    'one, two, and three'

@stevendaprano
Copy link
Member

Python 3.9 is in feature freeze, this couldn't be added before 3.10 now.

You have:

", ".join(["a", "b", "c"], ", and ")

outputing this:

"a, b, and, c"

So what are the semantics of this additional argument? To insert it before the last item?

@stevendaprano stevendaprano added 3.10 only security fixes and removed 3.9 only security fixes labels Feb 20, 2021
@stevendaprano
Copy link
Member

Looking more closely, I think that the semantics are to concatenate the extra argument to the second-last item:

", ".join(["a", "b", "c"])
# -> "a, b, c"

", ".join(["a", "b", "c"], ", and")
# -> "a, b, and, c"

which would be the same as:

", ".join(["a", "b, and", "c"])
# -> "a, b, and, c"

I'm not sure how this is useful. In English, there should never be a comma after the "and", and there possibly shouldn't be a comma after the "b" either, depending on context.

# Should be: "a, b and c" or "a, b, and c"

See the Oxford or serial comma:

https://thegrammargirls.wordpress.com/tag/oxford-comma/

I'm going to close this feature request. It's too specific and the semantics don't seem to be very useful. But if you would still like to propose this, or a similar change, please discuss it first either on the Python-Ideas mailing list:

https://mail.python.org/archives/list/python-ideas@python.org/

or at the Ideas topic on

https://discuss.python.org

so that we can determine the required semantics and get a sense for how useful it would be in general.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants