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

string str.split() behaviour inconsistency #40009

Closed
peteshinners mannequin opened this issue Mar 6, 2004 · 2 comments
Closed

string str.split() behaviour inconsistency #40009

peteshinners mannequin opened this issue Mar 6, 2004 · 2 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@peteshinners
Copy link
Mannequin

peteshinners mannequin commented Mar 6, 2004

BPO 911080
Nosy @sjoerdmullender, @rhettinger

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 = 'https://github.com/rhettinger'
closed_at = <Date 2004-03-07.11:00:03.000>
created_at = <Date 2004-03-06.20:41:54.000>
labels = ['interpreter-core']
title = 'string str.split() behaviour inconsistency'
updated_at = <Date 2004-03-07.11:00:03.000>
user = 'https://bugs.python.org/peteshinners'

bugs.python.org fields:

activity = <Date 2004-03-07.11:00:03.000>
actor = 'sjoerd'
assignee = 'rhettinger'
closed = True
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2004-03-06.20:41:54.000>
creator = 'peteshinners'
dependencies = []
files = []
hgrepos = []
issue_num = 911080
keywords = []
message_count = 2.0
messages = ['20179', '20180']
nosy_count = 3.0
nosy_names = ['sjoerd', 'rhettinger', 'peteshinners']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue911080'
versions = []

@peteshinners
Copy link
Mannequin Author

peteshinners mannequin commented Mar 6, 2004

The str.split() method behaves differently depending on
if it uses the default (no arguments) separator, or if
you provide your own. There is no way to reproduce the
functionality of the default separator if you supply
your own.

>>> s = "a b  c"
>>> s.split()
['a', 'b', 'c']
>>> s.split(" ")
['a', 'b', '', 'c']

The default split uses a different algorithm, where it
combines multiple separators into a single separator.
Providing a custom separator makes split separate each
individual separator.

Obviously there are good reasons for forcing a separate
entry between each separator. With simple comma or
colon separated records, you want to know if an entry
is blank.

The problem is there is not a way to reproduce the
functionality of the default behavior. This alternate
behavior is also not documented, so it becomes
confusing why split behaves different once you want
your own separators.

Fixing could be a problem. Changing the actual split()
method would break many programs. But adding an
different split is a potentially nice solution.

The other option would be to "re-use" the current
splitfields() function and have it work like the
current split. And change the split() to behave like it
does with no default. This would unfortunately still
"break stuff".

The easiest fix may just be documentation and letting
people know of this difference.

I've been helping some newbies through Python. When
this came up I was a little surprised and we were
forced to learn it was just a little "magic and scary".

@peteshinners peteshinners mannequin closed this as completed Mar 6, 2004
@peteshinners peteshinners mannequin assigned rhettinger Mar 6, 2004
@peteshinners peteshinners mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 6, 2004
@sjoerdmullender
Copy link
Member

Logged In: YES
user_id=43607

Use s.split(None) to get the same behavior as without arguments.
This is documented behavior, as far as I know. If the
documentation isn't good enough, submit a bug report
specifically for that.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

2 participants