Python: max/min builtin functions depend on parameter order

后端 未结 3 2168
半阙折子戏
半阙折子戏 2020-11-29 07:51

max(float(\'nan\'), 1) evaluates to nan

max(1, float(\'nan\')) evaluates to 1

Is it the intended behavior?


Thanks for

3条回答
  •  遥遥无期
    2020-11-29 08:33

    Max works the following way:

    The first item is set as maxval and then the next is compared to this value. The comparation will always return False:

    >>> float('nan') < 1
    False
    >>> float('nan') > 1
    False
    

    So if the first value is nan, then (since the comparation returns false) it will not be replaced upon the next step.

    OTOH if 1 is the first, the same happens: but in this case, since 1 was set, it will be the maximum.

    You can verify this in the python code, just look up the function min_max in Python/bltinmodule.c

提交回复
热议问题