[Tutor] is gotchas?
Michael P. Reilly
arcege at gmail.com
Tue Nov 7 03:51:32 CET 2006
On 11/6/06, Tim Johnson <tim at johnsons-web.com> wrote:
>
> * Kent Johnson <kent37 at tds.net> [061106 10:31]:
> >
> > In [9]: a=[1,2]
> >
> > In [10]: b=[1,2]
>
> Hmmm! Hmmm!
> Lookee here:
> ## console session
> >>> a=[1,2]
> >>> b=[1,2]
> >>> a is b
> False
> >>> c='1' ## one byte
> >>> d='1' ## one byte
> >>> c is d
> True
> >>> c='1,2'
> >>> d='1,2'
> >>> c is d
> False
>
> The Hmmm! is emmitted because I'm thinking that if everything is an
> object in python, then why does `c is d` evaluate to True when
> the assigned value is 1 byte and evaluate to False when the assigned
> value is more that 1 byte?
>
> I think I ran into this before and that's why I never used `is'.
>
You might want to try:
>>> a = 'a'
>>> b = 'a'
>>> a is b
True
Why? Interned strings. As Pujo aluded to, various simple or well-used
objects (like the digits between 0 and 100), and strings that have been
interned with the intern() function.
There is a unique item: None. There is only one object of type NoneType.
No matter how many times you reference it, it will always be the same
object. So it is a perfect use of "is":
def f(arg1, arg2, optarg=None):
if optarg is None:
-Arcege
--
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061106/e0af3c1e/attachment.html
More information about the Tutor
mailing list