Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
testing value type 2
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  testing value type Skaperen 1 1,760 Jan-23-2021, 03:48 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020