Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 42 |
Nodes: | 6 (0 / 6) |
Uptime: | 00:45:48 |
Calls: | 220 |
Calls today: | 1 |
Files: | 824 |
Messages: | 121,521 |
Posted today: | 6 |
'r's = 'qwertyuiop'
s[s.find('r')]
'p's[s.find('p')]
'p's[s.find('a')]
Python seem to suffer from a few poor design decisions regarding
strings and lists that affect the elegance of the language.
(a) An error-prone "feature" is returning -1 if a substring is not
found by "find", since -1 currently refers to the last item.
If "find" is unsuccessful, an error message is the only clean option.
Moreover, using index -1 for the last item is a bad choice: it should
be len(s) - 1 (no laziness!).
Negative indices should be reserved for elements preceding the element
with index 0 (currently not implemented, but a must for orthogonal
design supporting general sequences).
(b) When using assignment for slices, only lists with the same length
as the slice should be acceptable, otherwise an error should be
given.
L.S.,
Python seem to suffer from a few poor design decisions regarding strings
and lists that affect the elegance of the language.
(a) An error-prone "feature" is returning -1 if a substring is not found
by "find", since -1 currently refers to the last item. An example:
'r's = 'qwertyuiop'
s[s.find('r')]
'p's[s.find('p')]
'p's[s.find('a')]
If "find" is unsuccessful, an error message is the only clean option. Moreover, using index -1 for the last item is a bad choice: it should be len(s) - 1 (no laziness!).
Negative indices should be reserved for elements preceding the element
with index 0 (currently not implemented, but a must for orthogonal
design supporting general sequences).
(b) When using assignment for slices, only lists with the same length as
the slice should be acceptable, otherwise an error should be given.
Anything that re-indexes items not covered by the slice is against the essential idea of assignment. For changes that imply re-indexing (e.g., inserting a list longer than the slice), Python offers cleaner solutions.
Comments are welcome.
(a) An error-prone "feature" is returning -1 if a substring is not found
by "find", since -1 currently refers to the last item. An example:
>>> s = 'qwertyuiop'
>>> s[s.find('r')]
'r'
>>> s[s.find('p')]
'p'
>>> s[s.find('a')]
'p'
>>>
If "find" is unsuccessful, an error message is the only clean option.
Moreover, using index -1 for the last item is a bad choice: it should be
len(s) - 1 (no laziness!).
I'm not sure if this answers your objection but the note in the
documentation (https://docs.python.org/3/library/stdtypes.html#str.find) says:
The find() method should be used only if you need to know the position of sub.
I think the use case above is a little bit different.
To write the nested expression, s[s.find(...)] it means you're 200% sure
of what happens in case of not found.
'p's[s.find('a')]
Moreover, using index -1 for the last item is a bad choice: it should be >len(s) - 1 (no laziness!).
(b) When using assignment for slices, only lists with the same length as
the slice should be acceptable, otherwise an error should be given.
L.S.,This is IMO indeed not the best design decision. Fortunately there's an alternative: the "index" method on strings, which raises exception
Python seem to suffer from a few poor design decisions regarding
strings and lists that affect the elegance of the language.
(a) An error-prone "feature" is returning -1 if a substring is not
found by "find", since -1 currently refers to the last item. An example:
'p's = 'qwertyuiop'
s[s.index('p')]
Traceback (most recent call last):s[s.index('a')]
Moreover, using index -1 for the last item is a bad choice: it shouldI don't agree, I think this is a case of "practicality beats purity".
be len(s) - 1 (no laziness!).
Negative indices should be reserved for elements preceding the element
with index 0 (currently not implemented, but a must for orthogonal
design supporting general sequences).
(b) When using assignment for slices, only lists with the same lengthAgain I don't agree. I don't see anything wrong with replacing a part of
as the slice should be acceptable, otherwise an error should be
given. Anything that re-indexes items not covered by the slice is
against the essential idea of assignment. For changes that imply
re-indexing (e.g., inserting a list longer than the slice), Python
offers cleaner solutions.