Pragmatics of the standard is() function
Roy Smith
roy at panix.com
Sat Nov 26 16:32:17 EST 2011
In article <4ed15825$0$21841$426a34cc at news.free.fr>,
candide <candide at free.invalid> wrote:
> In which cases should we use the is() function ? The is() function
> compares identity of objects rather than values so I was wondering in
> which circumstances comparing identities of objects is really vital.
>
> Examining well reputated Python source code, I realize that is()
> function is mainly used in the following set form :
>
> spam is None
>
> But how much "spam is None" is different from "spam == None" ?
It's the difference between *being* None, and being equal to None. For
example:
class Spam:
def __eq__(self, other):
return not other
spam = Spam()
print spam is None
print spam == None
When I run that, it prints:
False
True
In practice, when you compare something to None, you usually want the
"is" form. In cases where either would work (i.e. 99% of the time),
it's convention (and/or good practice) to use "is" because it more more
clearly expresses what it is that you're trying to do.
More information about the Python-list
mailing list