7

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
  • 80,138
  • 16
  • 128
  • 173
william007
  • 17,375
  • 25
  • 118
  • 194

1 Answers1

11

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
  • 233,700
  • 52
  • 457
  • 497