changing the List's behaviour?

Terry Reedy tjreedy at udel.edu
Sun Jul 27 20:44:11 EDT 2003


> > i want to change the List so that it returns None if the index for
> > accesssing list elements is out of bound.

Slicing always 'works' (does not raise exception), so seq[i:i+1]
returns empty slice rather than length 1 slice if i too large.  This
is a standard idiom for getting, for instance, first element of list
if there is one when there might not be.

So slice and condition off length and if 1, extract element. This has
advantage over idea above that [] is different from [None] (which you
would get is seq[i]==None).

Terry J. Reedy






More information about the Python-list mailing list