5

For the following code, str is variable of unicode type but

str is unicode            # returns false
isinstance(str, unicode)  # returns true

Why is is return false?

Bill Lynch
  • 76,897
  • 15
  • 123
  • 168
william007
  • 15,661
  • 20
  • 90
  • 161

1 Answers1

9

is operator is used to check if both the objects are one and the same, whereas isinstance is used to check if the second parameter appears anywhere in the inheritance chain of the first parameter.

So, when you do something like this

print(u"s" is unicode)

you are actually checking if u"s" is the unicode, but when you do

print(isinstance(u"s", unicode))

you are checking if u"s" is of type unicode and the latter is actually True.

thefourtheye
  • 221,210
  • 51
  • 432
  • 478