how to let argument be optional falling back to certain integer
On Sun, Jun 21, 2020 at 2:02 AM Boris Dorestand <bdorestand at example.com> wrote:
> I just wrote
> def f(y, N, k = None):
> k = k or (N - 1)
> return k
> I was surprised to find out that 0 == False, so f(7, 31, 0) produces 31.
> I'd like 0 to be a valid choice for k.
> How do you guys let k be an optional argument such that it defaults to
> N - 1?
The easiest way is to explicitly check for None.
if k is None: k = N - 1
Zero being false shouldn't be a surprise. If None can count as false,
then so should other "emptiness" values. (Remember, the canonical
falseness value is False, not None.)