The Python and
and or
operators, return values, rather than True
or False
, which is useful for things such as:
x = d.get(1) or d.get(2) or d.get(3)
Which will make x
the value of d[1]
, d[2]
or d[3]
which ever is present. This is a bit like having an additive Maybe monad in functional languages.
I've always wanted that the python any()
function would be more like a repeated or
. I think it would make sense return the object it finds, like:
any([None, None, 1, 2, None]) == 1
any(notnull_iterator) = try: return next(notnull_iterator); except: return None
And likewise for all()
. It seems to me the change would be entirely backwards compatible, and increase consistency across the API.
Does anyone know of a previous discussion of this topic?