[Python-ideas] Inclusive Range

spir denis.spir at gmail.com
Wed Oct 6 22:11:59 CEST 2010

On Wed, 6 Oct 2010 23:58:48 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:

> On the more general topic of *teaching* 0-based indexing, the best explanation I've seen is the one where 1-based indexing is explained as referring directly to the items in the sequence, while 0-based indexing numbers the implicit gaps between items and then returns the item immediately after the identified gap. Slicing for 0-based indexing can then be explained without needing to talk about half-open ranges at all - you just grab everything between the two identified gaps*.

In my experience, the only explanation that makes sense for newcomers is that 1-based indexes are just ordinary ordinals like we use everyday, while 0-based ones are _offsets_ measured from the start. It does not really help in practice (people do errors anyway), but at least they understand the logic so can reason when needed, namely to correct their errors.

> I think the main point here is that these are not independent design
> decisions - the behaviour of range() (or its equivalent), indexing,
> slicing, enumeration and anything else related to sequences all comes
> back to a single fundamental design choice of 1-based vs 0-based
> indexing.

I think there are languages with base 0 & closed range, unless it is base 1 & half-open range. Any convention works, practically. Also, the logics every supporter of the C convention, namely the famous tewt by EWD, reverses your argumentation: he show the advantages of helf-open intervals (according to his opinion), then that 0-based indexes fit better with this kind of intervals (ditto).

-- -- -- -- -- -- --
vit esse estrany ☣


More information about the Python-ideas mailing list