Difference between 'is' and '=='
Felipe Almeida Lessa
felipe.lessa at gmail.com
Mon Mar 27 22:12:15 EST 2006
Em Seg, 2006-03-27 às 21:05 -0500, Dan Sommers escreveu:
> Right off the top of my head, I can't think of a way to make "a = b; a
> is b" return False.
Sorry for being so --quiet. I will try to be more --verbose.
I can think of two types of constants:
1) Those defined in the language, like True, None, 0 and the like.
2) Those defined on your code.
You said type 1 can be used with "is", you're right:
>>> a = 100
>>> a is 100
False
I said type 2 can (maybe "should"?) be used with "is", and AFAICT I'm
right as well:
>>> b = a
>>> b is a
True
That said, you can do thinks like:
>>> import socket
>>> a = socket.AF_UNIX
>>> a is socket.AF_UNIX
True
That kind of constants can be used with "is". But if don't want to be
prone to errors as I do, use "is" only when you really know for sure
that you're dealing with singletons.
HTH,
--
Felipe.
More information about the Python-list
mailing list