On Tue, Jul 5, 2016 at 12:05 PM, Alex Hall <ah...@autodist.com> 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? 'isinstance' is something else from 'is'. isinstance will tell us if something is considered to be an "instance" of something: it knows how to categorize. This might be important if, when we're running our program, we have some data and want to do some case analysis based on what classification that data falls into. For example, True and False can be identified as instances of the booleans. ################################### >>> isinstance(True, bool) True >>> isinstance(False, bool) True ################################### If we're object oriented programming based on different classes of data, then 'isinstance' can be used to distinguish things based on their class. For example: ############################# class C1(object): pass class C2(object): pass barry = C1() white = C2() print(isinstance(barry, C1)) print(isinstance(barry, C2)) ############################## if we have two classes, we can create instances of them, and use 'isinstance' to find out if they match a particular class-ification. I rarely use this operator in practice because usually, *how* a value operates and behaves is much more important than *what* strata that value belongs to. I suppose that's an ideal of meritocracy. :) In contrast, 'is' is a more low-level notion having to do with *equality*, not classification. These are both operations that compare two things, but their conceptual types are not the same: * 'isinstance' deals with a thing and a category. * 'is' works on two things that, presumably, might be the same type of thing. (Why? Because if we knew in advance that the the two things were of different types, then we already know that 'is' will return False, so we know our answer in advance.) _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor