Does '!=' equivelent to 'is not'

John Salerno johnjsal at gmailNOSPAM.com
Mon Jun 16 23:51:24 EDT 2008


John Salerno wrote:

> == and != test to see if the *value* of 
> two variables are the same.

Let me just clarify this. It might seem like a picky point, but I think 
it's pretty important when learning Python.

I don't really mean the value of the variables themselves, I mean the 
values that the variables refer to. The variables themselves aren't 
actually the objects, nor do they have values, exactly. Instead, they 
*refer* to objects in memory, and it is these objects that we are 
testing the values and identities of. For example, using that previous code:

a ---> 'hello world'

b ---> 'hello world'

c ---\
       ---> 'hello world'
d ---/

a and b were assigned to two separate objects (it doesn't matter that 
they happen to be the same value). As you can see above, a and b refer 
to different things.

c and d, however, were assigned simultaneously to the same object, and 
therefore refer to a single object in memory. This is why "c is d" is 
True, but "a is b" is False.



More information about the Python-list mailing list