11

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?

2
  • This answer: stackoverflow.com/questions/13650293/… should give you a better perspective. is is checking for object equality. Commented Oct 4, 2014 at 14:03
  • 2
    @karthikr s/equality/identity/ Commented Oct 4, 2014 at 14:06

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.