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?
I guess you're looking for
first = lambda s: next((x for x in s if x), None)
e.g.
first([None, None,1, 2,None]) # 1
The "why" question is answered over here.
>>> from functools import partial
>>> my_any = partial(reduce, lambda x, y:x or y)
>>> my_any([None, None, 1, 2, None])
1
>>> my_all = partial(reduce, lambda x, y:x and y)
>>> my_all([0, 0, 1, 2, 0])
0
In the above example, my_all([]) raises an exception. However, you can easily provide a default value instead
>>> my_all([], "Foo")
'Foo'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With