Python's and and Pythons or

Ethan Furman ethan at stoneleaf.us
Thu Oct 10 02:46:55 EDT 2013


On 10/09/2013 11:12 PM, Peter Cacioppi wrote:
>
> I'm trying to think of a good example usage of echo-argument and. Maybe something like
>
> possible = foo and foo.allowsit()
> if (possible is None) :
>     print "foo not provided"
> if (possible is False) :
>     print "foo doesn't allow it"
>
> A bit awkward, echo-argument or is more naturally useful to me then echo-argument and.

It's used as a guard:

     if some_list and some_list[0] == something_or_other:
          do some_work()

Without the 'some_list and' portion when some_list was either empty or, say, None, the some_list[0] would fail with an 
error (IndexError, TypeError, etc.).

--
~Ethan~



More information about the Python-list mailing list