の返却:空の文字列は、私は今、本当に困惑している代わりにはIndexError
>>> string = "some string"
>>> string[100:105]
''
>>> string[100]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
なぜ(予想通り)string[100]
一方、空の文字列はIndexErrorを返したstring[100:105]
復帰? string[100:105]
がIndexErrorを発生させないのはなぜですか?
それが定義されているので。スライスはインデックスエラーを発生させません。 –
これは彼らが作った奇妙なデザインの決定です。 – user2357112
http://stackoverflow.com/questions/9490058/why-substring-slicing-index-out-of-range-works-in-python重複としてフラグが立てられている – nir0s