Python's and and Pythons or
Chris Angelico
rosuav at gmail.com
Thu Oct 10 02:45:49 EDT 2013
On Thu, Oct 10, 2013 at 5:12 PM, Peter Cacioppi
<peter.cacioppi at gmail.com> 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.
first_element = some_list[0] # Oops, may crash
try:
first_element = some_list[0]
except IndexError:
firstelement = None # A bit verbose
first_element = some_list and some_list[0]
# or if you want a zero instead of an empty list:
first_element = len(some_list) and some_list[0]
Also, consider the case where you have a function, or None:
result = func(*args,**kwargs) # NoneType is not callable
result = func and func(*args,**kwargs)
ChrisA
More information about the Python-list
mailing list