on-internet
← Community

Why does the behaviour of the `in` / `__contains__` operator differ from manual equality checks?

Stack Overflow ·

More or less by accident, I found out that it is possible to check whether certain Numpy result are of a specific Numpy dtype, by checking whether they occur in a list of dtype-class objects: Python 3.11.14 (main, Oct 21 2025, 18:31:21) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> np.__version__ '2.3.3' >>> np.equal(1,1) in [np.dtypes.BoolDType()] True It seems to indeed perform some check that it is actually the specific dtype: >>> np.equal(1,1) in [np.dtypes.Float32DType()] False I always assumed that the in operator,

Comments

Sign in or sign up to comment and upvote.

No comments yet.