Tim Johnson wrote:
> I've been using python is 1.5* but haven't used `is` that much.
> 
> 1)where do `is` and `==` yield the same results?
> 
> 2)where do they not yield the same results?

is tests for object identity - 'a is b' is true if a is the same object 
as b. There is no special method, no specialization for different data 
types, just 'is it the same thing?'

== tests for equality of value. It's exact meaning depends on the types 
of objects your are comparing. It can be specialized by defining a 
special method.

a is b generally implies a == b because if a is b then a==b is the same 
as a==a which is true for pretty much anything - it would be a very 
strange type for which a == a is not true. Maybe NaN (Not a Number) 
compares false to itself, I'm not sure, I don't know how to create NaN 
on Windows.

But really the meaning of == is defined by the underlying type so in 
your own classes you can make it do anything you want.

a==b does not imply a is b - it is quite reasonable for two different 
things to have the same value:

In [9]: a=[1,2]

In [10]: b=[1,2]

In [11]: a is b
Out[11]: False

In [12]: a==b
Out[12]: True

In [13]: a='abc**def'

In [14]: b='abc' + '**def'

In [15]: a is b
Out[15]: False

In [16]: a==b
Out[16]: True

> 
> 3)is there a special method for `is'.

No.

Kent

> 
> 4)Pointers to docs are welcome.
> 
> thanks
> tim
> 


_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to