how to let argument be optional falling back to certain integer

Python python at python.invalid
Sat Jun 20 13:30:13 EDT 2020


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


More information about the Python-list mailing list