[wxPython-users] a bug that i just can't figure out how to fix (to
do with dc and DrawText (or so i think))
Christopher Barker
Chris.Barker at noaa.gov
Thu Nov 9 10:50:30 PST 2006
Jason Wang wrote:
> I gotta look into the difference between is and ==, thanks for the heads
> up :D
"==" means: Do these two objects have the same value?
"is" means: Are these the SAME object?
some of your code worked because python does some optimizing by
"interning" small integers and strings, so if you create two that have
the same value, they may very well internally be the same object, but
they may not. Since strings and numbers are immutable, it shouldn't
matter to you
>>> x = 4
>>> y = 4
>>> x is y
True
>>> x = 483736
>>> y = 483736
>>> x is y
False
>>> x == y
True
>>> a = "a"
>>> b = "a"
>>> a is b
True
>>> a = "A longer string"
>>> b = "A longer string"
>>> a is b
False
>>> a == b
True
Since strings and numbers are immutable, it shouldn't matter to you
whether they are the same, unless you use "is", when you mean "=="!
So, use "is" only when you really want to know if the objects are the
same object. This isn't very common, except when you are checking if
something is None:
if x is None:
....
This is a good idea because None is a singleton -- every time you set
something to None, you get the same object, and == doesn't always work
with None and objects that support "rich comparison" -- which means they
override == to do something fancy. numpy arrays are an example of this.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the wxpython-users
mailing list