Python's and and Pythons or

Ethan Furman ethan at stoneleaf.us
Thu Oct 10 22:47:02 EDT 2013


On 10/10/2013 06:41 PM, Terry Reedy wrote:
> 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.

I understood it just fine.  I'm also aware that at some point, in some program, it will be None (and it won't be a bug ;).


> 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.

It's a good trick, I use it myself.

--
~Ethan~



More information about the Python-list mailing list