Difference between 'is' and '=='

Joel Hedlund joel.hedlund at gmail.com
Tue Mar 28 04:40:34 EST 2006


>>Not those kind of constants, but this one:
> 
> 
>>Python 2.4.2 (#2, Nov 20 2005, 17:04:48)
>>[GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
>>Type "help", "copyright", "credits" or "license" for more information.
>>
>>>>>CONST = 123456789
>>>>>a = CONST
>>>>>a == CONST
>>
>>True
>>
>>>>>a is CONST
>>
>>True
>>
> 
> That's a little misleading, and goes back to the questions of "what is
> assignment in Python?" and "What does it mean for an object to be
> mutable?"
> 
> The line "a = CONST" simply gives CONST a new name.  After that, "a is
> CONST" will be True no matter what CONST was.  Under some circumstances,
> I can even change CONST, and "a is CONST" will *still* be True.

Anyone who thinks it's a good idea to change a CONST that's not in a module 
that they have full control over must really know what they're doing or suffer 
the consequences. Most often, the consequences will be nasty bugs.

Cheers!
/Joel Hedlund



More information about the Python-list mailing list