[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: is operator

Gary Herron

3/10/2008 2:39:00 PM

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.

Gary Herron

4 Answers

Carl Banks

3/10/2008 3:25:00 PM

0

On Mar 10, 10:39 am, Gary Herron <gher...@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

Steven D'Aprano

3/10/2008 4:37:00 PM

0

On Mon, 10 Mar 2008 07:39:25 -0700, Gary Herron wrote:

> If either is a surprise, then understand that the "is" operator should
> probably *never* be used with immutable types.

Carl Banks has already mentioned testing for None with "is". The standard
idiom for using mutable default arguments goes something like this:

def foo(arg=None):
if arg is None:
arg = []
do_something_with(arg)


Another standard idiom that requires "is":

sentinel = object() # a special value that isn't None
....
if something is sentinel:
blah blah blah


Mutable or immutable, it makes no difference: "is" is for testing
identity, == is for testing equality. If you need to test for identity,
use "is". If you need to test for equality, use ==.



--
Steven


Aaron Brady

3/10/2008 5:10:00 PM

0

On Mar 10, 11:36 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> On Mon, 10 Mar 2008 07:39:25 -0700, Gary Herron wrote:
> > If either is a surprise, then understand that the "is" operator should
> > probably *never* be used with immutable types.
>
> Mutable or immutable, it makes no difference: "is" is for testing
> identity, == is for testing equality. If you need to test for identity,
> use "is". If you need to test for equality, use ==.

What is the notion of equal defined for functions?

Terry Reedy

3/11/2008 4:21:00 AM

0


<castironpi@gmail.com> wrote in message
news:b61e001a-3a41-4a35-ab3a-546e453d8d69@b64g2000hsa.googlegroups.com...
| What is the notion of equal defined for functions?

Isness. The developers did not see sufficient reason to refine it further
by comparing all the attributes, including the code object. But there has
just been a discussion on pydev about the notion of equality for bound
methods.

Isness is also the default notion of equality for user-class instances, but
in this latter case, an explicit and different __eq__ method can be given.

tjr