Does '!=' equivelent to 'is not'

Lie Lie.1296 at gmail.com
Tue Jun 17 01:25:42 EDT 2008


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:
> > I'm a bit confusing about whether "is not" equivelent to "!="
>
> > if a != b:
> >  ...
>
> > if a is not b:
> >  ...
>
> > 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.
>
> same objects are equal, but equal don't have to be the same object.
>
> and be very careful to the dirty corner of python:

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))'

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.



More information about the Python-list mailing list