好吧,是一个 feature 还是 bug 已经搞不清了
Ubuntu 16.04.03 Python3.5.2
>>> a = ''
>>> a[1:-1]
''
>>> a[2:-1]
''
>>> a[3:-1]
''
好吧,是一个 feature 还是 bug 已经搞不清了
Ubuntu 16.04.03 Python3.5.2
>>> a = ''
>>> a[1:-1]
''
>>> a[2:-1]
''
>>> a[3:-1]
''
|
clinoSep 1, 2017
1
你觉得应该返回什么?
|
|
013231Sep 1, 2017
3
是 feature。
https://docs.python.org/3.6/library/stdtypes.html#common-sequence-operations s[i:j:k] “ The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty.” |
|
estSep 1, 2017
4
In [457]: a='a'
In [458]: a[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0] Out[458]: 'a' |
|
mark06Sep 1, 2017
5
> However, out of range slice indexes are handled gracefully when used for slicing:
``` >>> word[4:42] 'on' >>> word[42:] '' ``` [document]( https://docs.python.org/3.5/tutorial/introduction.html#strings) |
|
SuperMildSep 1, 2017
6
动态语言一般倾向于默认一种行为以方便使用,而不是抛出异常,抛出异常还要处理异常多麻烦啊。动不动就抛出异常那是静态语言的习惯。
|
|
LeeSeoungSep 1, 2017
7
说实话,要是避免抛异常你是不是还要多写几个判断边界的语句,但是写完判断边界后你觉得是这种特性好用,还是你自己再写一堆代码好用。。
|
|
winglight2016Sep 1, 2017
11
@eloah 这大概是语言背后的哲学不同,我习惯 java 就很难理解这种 feature
|
|
jedihySep 2, 2017
12
一直都是这样啊
|
|
popbonesSep 2, 2017
13
异常不一定带表错误,是否检查以及如何处理异常是程序逻辑,程序员需要根据需要使用相应的语法功能。
这就好比什么时候用 if a 什么时候用 if a is not None 是程序员的责任而不是语言的责任一样。 |