git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

how to let argument be optional falling back to certain integer


Le 20/06/2020 ? 18:23, Stefan Ram a ?crit?:
> Boris Dorestand <bdorestand at example.com> writes:
>> 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.
> 
>    bool is a subtype of int.
> 
>> I'd like 0 to be a valid choice for k.
> 
> k = N-1 if k==None else k

When comparing wih None "is" is preferred to ==

k = N - 1 if k is None else k