By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,775 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

curious about slice behaviour

P: n/a
I just found out by accident, that slice indices can be larger than
the length of the object. For example
'test'[:50] 'test' 'test'[40:50]

''

I'd rather expected to be confronted with an IndexError.
(This is actually described in
http://docs.python.org/lib/typesseq.html, so my expectation was wrong :))

Does anybody know, why this is preferred to just raising an error?

Sep 5 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Stephan Diehl" <st***********@gmx.net> wrote in message
news:pa****************************@gmx.net...
I just found out by accident, that slice indices can be larger than
the length of the object. For example
'test'[:50] 'test' 'test'[40:50]
''
I'd rather expected to be confronted with an IndexError.
(This is actually described in
http://docs.python.org/lib/typesseq.html,


in footnote 4
.so my expectation was wrong :))
Does anybody know, why this is preferred to just raising an error?


Slicing was intentially designed to always give an answer (given int
coords) and never say 'can't answer' (whether by exception or a None
return). This avoids having to call len() when you don't care and avoids
having to use try:...except:... or conditionalize the code when it is not
needed. For instance c=s[0:1] is equivalent to

c=s[0:min(1,len(s))] # if slice had to be exact, or

c = s and s[0] or '' # or

if s:
c = s[0]
else:
c = '' # or

try:
c = s[0]
except IndexError:
c = ''

People occasionally post buggy code which simply needs s[0] changed to
s[0:1].

The form s[i:], which I am sure you agree is useful, is effectively
equivalent to eithers[i:len(s)] or s[i:<maxint>]. The latter view
generalizes to iterables without a knowable length.

Terry J. Reedy

Sep 5 '05 #2

P: n/a
On Mon, 05 Sep 2005 14:26:14 -0400, Terry Reedy wrote:

"Stephan Diehl" <st***********@gmx.net> wrote in message
news:pa****************************@gmx.net...
I just found out by accident, that slice indices can be larger than
the length of the object. For example
> 'test'[:50] 'test' [...] Does anybody know, why this is preferred to just raising an error?


Slicing was intentially designed to always give an answer (given int
coords) and never say 'can't answer' (whether by exception or a None
return). This avoids having to call len() when you don't care and avoids
having to use try:...except:... or conditionalize the code when it is not
needed. For instance c=s[0:1] is equivalent to

c=s[0:min(1,len(s))] # if slice had to be exact, or

c = s and s[0] or '' # or

if s:
c = s[0]
else:
c = '' # or

try:
c = s[0]
except IndexError:
c = ''

People occasionally post buggy code which simply needs s[0] changed to
s[0:1].

The form s[i:], which I am sure you agree is useful, is effectively
equivalent to eithers[i:len(s)] or s[i:<maxint>]. The latter view
generalizes to iterables without a knowable length.


I do think that this is useful and can save some lines of code.
Just never expected this.

Terry J. Reedy



Sep 5 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.