Re: [Tutor] isinstance versus 'is'?

2016-07-06 Thread Alex Hall
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  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@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] isinstance versus 'is'?

2016-07-05 Thread Steven D'Aprano
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] isinstance versus 'is'?

2016-07-05 Thread Alan Gauld via Tutor
On 06/07/16 00:22, Alan Gauld via Tutor wrote:

 type(c) is C
> True
 type(d) is type(C)
> False

The last one should of course be

>>> type(d) is C
False

Apologies.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] isinstance versus 'is'?

2016-07-05 Thread Danny Yoo
On Tue, Jul 5, 2016 at 12:05 PM, 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?


'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


Re: [Tutor] isinstance versus 'is'?

2016-07-05 Thread Alan Gauld via Tutor
On 05/07/16 20:05, Alex Hall wrote:

> I was double checking that I remembered the isinstance order of arguments
> correctly by using the command line interpreter, and found something very
> odd.
> 
 a = 5
 isinstance(a, int)
> True
 a is int
> False
> 
> What happened there? Don't these do the same thing? 

Evidently not!
Think about it. isinstance() is asking if a is an
instance of int. int is a type. 5 is an instance of
that type but it's not the type itself.

a is int

is asking if a and int are the same things
(are they actually the same object).
Clearly 5 is not the type int. We can check
this in the interpreter:

>>> isinstance(5,int)
True
>>> int

>>> 5
5

What you may be getting confused with is:

>>> type(5) is int
True
>>>

But even then this is not identical to isinstance,
because the latter checks for subclass relationships
too:

>>> class C: pass
...
>>> class D(C): pass
...
>>> c = C()
>>> d = D()
>>> isinstance(c, C)
True
>>> isinstance(c, D)
False
>>> isinstance(d, C)
True
>>> isinstance(d, D)
True
>>> type(c) is C
True
>>> type(d) is type(C)
False

And to further complicate matters the above works
differently in v2 from v3(above)

The bottom line is that isinstance() is usually
the best choice.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] isinstance versus 'is'?

2016-07-05 Thread Joel Goldstick
On Tue, Jul 5, 2016 at 3:05 PM, Alex Hall  wrote:
> Hi all,
> Thanks for all the help regarding decorators. It makes more sense now.
>
> I was double checking that I remembered the isinstance order of arguments
> correctly by using the command line interpreter, and found something very
> odd.
>
 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?
>
> --
> Alex Hall
> Automatic Distributors, IT department
> ah...@autodist.com
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

is denotes identity.  5 is not the type int.  It is an int, but it is
not the same -- if it was 5('16') would be 16.

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] isinstance versus 'is'?

2016-07-05 Thread Alex Hall
Hi all,
Thanks for all the help regarding decorators. It makes more sense now.

I was double checking that I remembered the isinstance order of arguments
correctly by using the command line interpreter, and found something very
odd.

>>> 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?

-- 
Alex Hall
Automatic Distributors, IT department
ah...@autodist.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor