Python's and and Pythons or

Terry Reedy tjreedy at udel.edu
Thu Oct 10 21:41:04 EDT 2013


On 10/10/2013 9:33 AM, Ethan Furman wrote:

> On 10/10/2013 12:43 AM, Terry Reedy wrote:

>> On 10/10/2013 2:45 AM, Chris Angelico wrote:
>>> first_element = some_list[0]    # Oops, may crash

>> some_list[0:1] always works, and sometimes is usable, but you still
>> cannot index the slice.

> Not if some_list is None, False, or another non-indexable type.

Did you really not understand that some_list is intended to be a list? 
Just like my_string, for instance, would be a string? Chris's statement 
further specifies some_list as a list that is expected to not be empty, 
but might be -- so one has to guard against the possibility.

The trick of slicing instead of indexing in this context is not obvious 
to everyone learning Python. Most other languages only have indexing. I 
learned the trick years ago when someone posted it on this list.

-- 
Terry Jan Reedy




More information about the Python-list mailing list