Python Forum

Full Version: testing value type 2
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
what about
if foo in (Sometype,):
    ...
compared to
if foo is Sometype:
    ...
is in more like is or more like ==? in this usage i want to determine if foo's value is the type itself ... or not. it's about how in works.
The Documentation Wrote:For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y).
See here. Thus, if is fails to be true, == is attempted.

That said, objects' equality == defaults to is. As types are usually instances of the type type, I would guess that equality for types is the same as is by default, unless you define metaclasses that override __eq__ (which I don't think I have ever done), so it should generally work in this case.

Now if by any chance foo happens not to be a type at run time, there could be a call to foo.__eq__(Sometype) which consequences we do not know. With the is operator, nothing unexpected can happen.
foo could be a non-type in which case i want false even if it is an object of that type or any other. it could be None.