[Tutor] why is this so?
Steven D'Aprano
steve at pearwood.info
Sun Sep 18 14:46:51 CEST 2011
Khalid Al-Ghamdi wrote:
> Hi All,
>
> why is this so?
>
>>>> type('love')
> <class 'str'>
>>>> "love" is str
> False
The "is" operator tests for object identity. The line
"love" is str
tests whether the instance "love" is the same object as the class str.
Obviously that is not the case.
You might be thinking of an "is-a" test: "love" is a string? For that,
you want:
isinstance("love", str)
which returns True. (That's better than type("love") is str, since it
will work for subclasses as well.)
But before getting too used to isinstance, you should read this article:
http://www.canonical.org/~kragen/isinstance/
--
Steven
More information about the Tutor
mailing list