Friday, 4 January 2019

Why does `if None.__eq__("a")` evaluate to True?

If you execute the following statement in Python 3.7, it will (from my testing) print b:

if None.__eq__("a"):
    print("b")

However, None.__eq__("a") evaluates to NotImplemented.

Naturally, "a".__eq__("a") evaluates to True, and "b".__eq__("a") evaluates to False.

I initially discovered this when testing the return value of a function, but didn't return anything in the second case -- so, the function returned None.

What's going on here?



from Why does `if None.__eq__("a")` evaluate to True?

No comments:

Post a Comment