X is confusing

Duncan Booth me at privacy.net
Wed Jul 7 04:21:41 EDT 2004


"Peter Mott" <peter at monicol.co.uk> wrote in
news:40eb114e$0$58816$bed64819 at news.gradwell.net: 

> I get this inb the Python interpreter v. 2.3
> 
>>>>
>>>> X=99
>>>> X is 99
> True
>>>> Y=100
>>>> Y is 100
> False
>>>>
> 
> Numbers less that 100 get True, more than 100 get False. It seems.
> What does this mean? If nothing, then why does it happen? I mean how
> do you explain 'is' or 'id(X)' to the first time programmer when it
> does this to you. 
> 

Similar things happen with strings:

>>> x = 'hello'
>>> x is 'hello'
True
>>> y = 'hello world'
>>> y is 'hello world'
False
>>> 

The compiler is free to optimise immutable values if it wishes. First time 
programmers probably don't need to know all of the details of 'is', for 
that matter even experienced programmers rarely need to use it.



More information about the Python-list mailing list