is operator
Carl Banks
pavlovevidence at gmail.com
Mon Mar 10 11:25:16 EDT 2008
On Mar 10, 10:39 am, Gary Herron <gher... at islandtraining.com> wrote:
> Metal Zong wrote:
>
> > The operator is and is not test for object identity: x is y is true if
> > and only if x and y are the same objects.
>
> > >>> x = 1
>
> > >>> y = 1
>
> > >>> x is y
>
> > True
>
> > Is this right? Why? Thanks.
>
> Yes that is true, but it's an implementation defined optimization and
> could be applied to *any* immutable type. For larger ints, such a thing
> is not true.
> >>> x=1000
> >>> y=1000
> >>> x is y
> False
>
> If either is a surprise, then understand that the "is" operator should
> probably *never* be used with immutable types.
Not quite: None is immutable but it's usually recommended to test for
it using is.
Carl Banks
More information about the Python-list
mailing list