Comparison with boolean numpy arrays VS PEP8 E712

前端 未结 2 1076
情书的邮戳
情书的邮戳 2020-12-20 18:10

PEP8 E712 requires that \"comparison to True should be if cond is True: or if cond:\".

But if I follow this

2条回答
  •  抹茶落季
    2020-12-20 18:29

    That advice only applies to if statements testing for the "truthiness" of a value. numpy is a different beast.

    >>> a = np.array([True, False]) 
    >>> a == True
    array([ True, False], dtype=bool)
    >>> a is True
    False
    

    Note that a is True is always False because a is an array, not a boolean, and is does a simple reference equality test (so only True is True; None is not True for example).

提交回复
热议问题