[Tutor] isinstance versus 'is'?

Alex Hall ahall at autodist.com
Tue Jul 5 21:37:26 EDT 2016

Thanks everyone, that all makes more sense. I think I was indeed thinking of "is None", which is essentially the same as "== None" (I know there's a subtile difference, but they do the same thing). Of course, "is None" fits with this usage, as you're asking if the value is the literal None object. It seems it's the way None is handled, not an exception in the way 'is' works. Anyway, thanks for the explanations.
> On Jul 5, 2016, at 20:54, Steven D'Aprano <steve at pearwood.info> wrote:
> On Tue, Jul 05, 2016 at 03:05:45PM -0400, Alex Hall wrote:
>>>>> a = 5
>>>>> isinstance(a, int)
>> True
>>>>> a is int
>> False
>> What happened there? Don't these do the same thing? I thought I could use
>> them interchangeably?
> You're probably thinking of "is a", as in, "5 is an int", "'Hello 
> World' is a str", "[1, 2, 3] is a list", etc.
> Python doesn't have an operator for testing "is a" relationships, it 
> uses isinstance(obj, type). There's also issubclass(), for testing 
> whether one class is a subclass of another.
> "x is y" checks whether the two operands x and y are the same object. 
> That's *not* the same as checking whether they are equal. You should 
> hardly ever use "is" in Python, with the exception of testing for None: 
> "if obj is None: ..." sort of thing.
> -- 
> Steve
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list