[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
is != == (Re: [pygame] Sticky Variables: Terrain Loading)
On Mar 12, 2008, at 5:43 AM, Ian Mallett wrote:
Weird. I get:
>>> 2 is 1+1
True
it's not too weird, it's just that 'is' is what you use to check if two
references are to the same instance (object), and there's a pretty well
known optimization in (c)Python that smallish numbers are kind of
singletons so that they are not instanciated all the time
so use the == operator when interested in the values, and not whether
the reference is the same.
'is' works well for "if x is None" because None is a singleton, i.e.
all references to None point to the same object (that's why you better
never do "None = 'x'" :p)
otherwise is useful for checking if a reference is exactly to the same
object, e.g.:
a = SceneNode()
b = SceneNode(parent=a)
c = SceneNode(parent=a)
assert b.parent is c.parent
~Toni