Does '!=' equivelent to 'is not'
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Jun 17 03:33:03 EDT 2008
En Tue, 17 Jun 2008 02:25:42 -0300, Lie <Lie.1296 at gmail.com> escribió:
> On Jun 17, 11:07 am, "Leo Jay" <python.leo... at gmail.com> wrote:
>> On Tue, Jun 17, 2008 at 11:29 AM, pirata <pir... at mars.invalid> wrote:
>> > What's the difference between "is not" and "!=" or they are the same thing?
>>
>> The 'is' is used to test do they point to the exactly same object.
>> The '==' is used to test are their values equal.
>>
>> and be very careful to the dirty corner of python:
[example with "5 is 5" but "100000 is not 100000"]
> No you don't have to be careful, you should never rely on it in the
> first place.
>
> Basically 'a is b' and 'not(a is b)' is similar to 'id(a) == id(b)'
> and 'not(id(a) == id(b))'
No.
> You use 'is' when you want to test whether two variable/names are
> actually the same thing (whether they actually refers to the same spot
> on memory). The '==' equality comparison just test whether two
> objects' values can be considered equal.
Yes, *that* is true. The above statement is not. A counterexample:
py> [] is []
False
py> id([])==id([])
True
Even dissimilar objects may have the same id:
py> class A: pass
...
py> class B: pass
...
py> A() is B()
False
py> A() == B()
False
py> id(A())==id(B())
True
Comparing id(a) with id(b) is only meaningful when a and b are both alive at the same time. If their lifetimes don't overlap, id(a) and id(b) are not related in any way. So I think that trying to explain object identity in terms of the id function is a mistake.
--
Gabriel Genellina
More information about the Python-list
mailing list