8 is int
The above line of code statement shows false.
Even though 8 is an int.
How does this work?
I would like to know how is operator works in this case.
Thank you.
Asked
Active
Viewed 40 times
0

Pavan
- 11
- 1
-
https://stackoverflow.com/questions/13650293/understanding-pythons-is-operator – Abhinav Mathur Oct 17 '20 at 06:34
-
Use `isinstance()`. – Olvin Roght Oct 17 '20 at 06:35
-
1As you said 8 is **an** int, but 8 is not int, because int is a type, and 8 is an instance of this type. – Yuval.R Oct 17 '20 at 06:48
-
Are you identical to the concept of being human? No; you *are a* human - i.e., you are a member of the set of humans, or you are an instance of humanity. `is` does not do the kind of test you want. In programming, we need to be precise about language, in a way that is difficult or impossible in English. – Karl Knechtel Oct 17 '20 at 06:52
1 Answers
0
just a small mistake in your syntax. You should have written type(8) is int
in your condition statement which is the correct syntax. type(object)
returns the class of the object so you can compare it with the is operator. So for example:-
Bonjour
is an object of class Greeting
So the following code:-
if type(Bonjour) is Greeting:
print("Bonjour is a Greeting!")
would print out:-
Bonjour is a Greeting

tofuthefirst
- 65
- 1
- 7