msg302418 - (view) |
Author: Ashok Bakthavathsalam (kgashok) * |
Date: 2017-09-18 08:04 |
"abcde".rindex("") returns 5
"a".rindex("") returns 1
This is not documented anywhere in the Python documentation.
|
msg302421 - (view) |
Author: Ashok Bakthavathsalam (kgashok) * |
Date: 2017-09-18 08:19 |
Also, "".rindex("") returns 0
|
msg302422 - (view) |
Author: Ashok Bakthavathsalam (kgashok) * |
Date: 2017-09-18 08:24 |
string.find() also exhibits the same behaviour.
"abcde".find("") -> 5 which also is not documented anywhere.
|
msg302424 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) * |
Date: 2017-09-18 08:31 |
This is documented.
"Return the highest index in the string where substring sub is found,"
|
msg302427 - (view) |
Author: Ashok Bakthavathsalam (kgashok) * |
Date: 2017-09-18 08:57 |
The documentation (https://docs.python.org/3/library/stdtypes.html#str.find) does not describe what will be the behaviour if the substring is "".
And by the way, as per https://docs.python.org/3/reference/expressions.html#membership-test-operations,
Empty strings are always considered to be a substring of any other
string, so "" in "abc" will return True.
Returning an invalid index value, although confusing, should at least be documented in some form.
|
msg302431 - (view) |
Author: Martin Panter (martin.panter) * |
Date: 2017-09-18 09:11 |
The behaviour for searching for empty strings in Python is inconsistent; see Issue 24243. IMO the behaviour for the (r)find/index methods is sensible, but it is worth making the documentation explicit.
The returned indexes you have given (5, 1, and 0) are valid for slicing.
|
msg302435 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) * |
Date: 2017-09-18 09:31 |
The documentation doesn't mention empty string specially because there is nothing special with empty string.
Returned index value is valid. "abcde"[5:5] == "".
Issue24243 is about the case of end < start. Not this case.
I don't see a bug here.
|
msg302436 - (view) |
Author: Ashok Bakthavathsalam (kgashok) * |
Date: 2017-09-18 09:34 |
I am not saying that there is a bug. As Martin points out, "it is worth making the documentation explicit."
|
msg302437 - (view) |
Author: Ashok Bakthavathsalam (kgashok) * |
Date: 2017-09-18 09:36 |
How about
"abcde"[5]
Traceback (most recent call last):
File "python", line 1, in <module>
IndexError: string index out of range
|
msg302438 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) * |
Date: 2017-09-18 09:43 |
5 is not valid index of character, but it is valid index of substring.
IMHO documenting explicitly miscellaneous particular cases which are not exceptions of general rules just adds a noise. This makes the documentation larger and decrease the chance that it will be read.
|
msg302443 - (view) |
Author: Ashok Bakthavathsalam (kgashok) * |
Date: 2017-09-18 09:53 |
Look at my suggested changes. It doesn't add noise, IMHO.
All I am saying is that the explicit behaviour needs to be documented.
I unnecessarily wasted at least 3-4 hours on this "undocumented" behavior.
|
msg302444 - (view) |
Author: Ashok Bakthavathsalam (kgashok) * |
Date: 2017-09-18 10:08 |
@Storchaka,
You say `5` is related to the substring. Pray, explain how 5 is related to a null substring? Also, from https://bugs.python.org/msg243710, as per the great Hettinger:
Though this is closed as not a bug, feel free to add an example or a
mention in the documentation. Keep it short though.
That's all I am asking to be included.
|
msg302455 - (view) |
Author: Vedran Čačić (veky) * |
Date: 2017-09-18 14:14 |
This is nonsense. 'abcde'[7:7] is also ''. So the maximal index in fact doesn't exist. What do you think exactly is the property that differentiates 5 from 7 here?
|
msg302465 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2017-09-18 15:55 |
You have to remember that the most useful way to think about python slice indexes is that they point between characters. Consider, for example, that you have a starting index of something, and you are looking backward in the string for a trailing delimiter:
>>> x = 'this is <weird example> of something'
>>> x.rfind('>', 9)
22
>>> x[9:22]
'weird example'
So the above is why 5 is different from 7: 5 is the index that you would use in a slice if you wanted the string that ended before the match...and the match in the null string case is the end of the string. That is, Python is being consistent in this degenerate case.
|
msg380092 - (view) |
Author: Vedran Čačić (veky) * |
Date: 2020-10-31 19:44 |
Maybe you would use 5, but I would use 7 and get the same result. If the docs say "X.rindex(Y) == i means i is the highest index where Y is found in X", and "Y is found in X at i" is interpreted as "X[i:i+len(Y)] == Y" (as Serhiy said), then there is no such (highest) index.
I understand what _you_'re saying, but please understand that the docs do not say anything like that.
|
|
Date |
User |
Action |
Args |
2022-04-11 14:58:52 | admin | set | github: 75685 |
2020-10-31 19:44:47 | veky | set | messages:
+ msg380092 |
2020-10-31 18:34:20 | iritkatriel | set | status: pending -> closed type: enhancement -> behavior resolution: rejected stage: patch review -> resolved |
2018-03-26 15:02:21 | serhiy.storchaka | set | status: open -> pending |
2017-09-18 15:55:59 | r.david.murray | set | messages:
+ msg302465 |
2017-09-18 14:14:07 | veky | set | nosy:
+ veky messages:
+ msg302455
|
2017-09-18 12:33:30 | kgashok | set | type: behavior -> enhancement |
2017-09-18 10:08:03 | kgashok | set | messages:
+ msg302444 |
2017-09-18 09:53:39 | kgashok | set | messages:
+ msg302443 |
2017-09-18 09:43:06 | serhiy.storchaka | set | nosy:
+ rhettinger, r.david.murray messages:
+ msg302438
|
2017-09-18 09:36:48 | kgashok | set | messages:
+ msg302437 |
2017-09-18 09:34:09 | kgashok | set | messages:
+ msg302436 |
2017-09-18 09:31:14 | serhiy.storchaka | set | messages:
+ msg302435 |
2017-09-18 09:26:10 | kgashok | set | keywords:
+ patch stage: patch review pull_requests:
+ pull_request3637 |
2017-09-18 09:11:26 | martin.panter | set | nosy:
+ martin.panter messages:
+ msg302431
|
2017-09-18 08:57:10 | kgashok | set | messages:
+ msg302427 |
2017-09-18 08:31:57 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages:
+ msg302424
|
2017-09-18 08:24:45 | kgashok | set | messages:
+ msg302422 |
2017-09-18 08:19:14 | kgashok | set | messages:
+ msg302421 |
2017-09-18 08:15:22 | kgashok | set | assignee: docs@python
components:
+ Documentation, - Library (Lib) nosy:
+ docs@python |
2017-09-18 08:04:47 | kgashok | create | |