Slice confusion : a[n:p] is a list exclude the last element p

Max M maxm at mxm.dk
Tue May 6 13:59:39 EDT 2003


Alex Martelli wrote:
> nguyen khanh wrote:
> 
>>a=range(10)
>>b=a[3:7]
>>b=[3,4,5,6]
>>Why the design exclude the last element ?
> 
> It's an important design principle, which I first saw proposed in print
> by Andrew Koenig in his landmark book "C Traps and Pitfalls": *all bounded
> loops should be first-bound-included, last-bound-excluded* -- and which I
> will therefore call "Koenig's Principle" although I have never seen the
> principle *named* in the literature 


Just a short note on this.

A good way to think of it, is how a video records timed shows.

With the pythonic approach you can easily record 2 shows after on another:

Simpsons: 19:00 - 19:30
X-Files:  19:30 - 20:30

You don't get into trouble because the second show the same time as the 
first show ends.

If the last item was included you would have to remember to set the 
timer to:

Simpsons: 19:00 - 19:29
X-Files:  19:30 - 20:29

Anyhoo I find it a usable metaphor.

-- 

hilsen/regards Max M Rasmussen, Denmark

http://www.futureport.dk/
Fremtiden, videnskab, skeptiscisme og transhumanisme





More information about the Python-list mailing list