What is the most elegant and pythonic way to turn a test such as:
def is_even(n):
return n % 2 == 0
Into a logically equivalent (but different syntactically) test such as:
if n in even_numbers:
# assert is_even(n)
# ...
I can think of a few ways to create the object even_numbers, but I’m interested to hear if there are nice ways I might have overlooked.
Note: I’m aware this probably sound like an XY problem, so my use case is concerning the choices kwarg to argparse.ArgumentParser.add_argument, and whether it’s going to be nicer to use that or alternatively to munge the callable type kwarg.
I would turn
even_numbersinto a container type:Then,