2 ms·
Well, considering that this is an indirection on top of the `in` operator which will slow the whole thing down, of course they're not typically implemented in i
by falcolas 12y ago
Well, considering that this is an indirection on top of the `in` operator which will slow the whole thing down, of course they're not typically implemented in idiomatic solutions in Python.
The obvious being said, you can also use functions and lambdas to get around having to create custom classes or abuse lists.
def value(x):
return lambda: x
def values_in(keys, d):
return [x() for x in (value(d[key]) if key in d else None for key in keys) if x]
def get_maybe(d, k):
return value(d[k]) if k in d else None
def values_maybe(keys, d):
""" If you want the caller to be responsible for handling the maybe case """
return [get_maybe(d, k) for k in keys]