Archive - Originally posted on "The Horse's Mouth" - 2008-05-31 22:35:14 - Graham Ellis
Is the number 7.0 the same as the number 7.00000? A trick question, because it depends on how you look at it. It has the same value, but it is not identical. And if eachof them is held in computer memory, there's no chance at all that they're both the same thing - i.e. held in the same memory location - at the same time.
Does it matter which type of identity you use? Sometimes it does!
a) In Python, if you use the == operator on two objects, it uses the __cmp__ method to compare them, and if that returns 0 they are considered to be the same. This could lead to some quite bizarre situations if you code it that way - you could for example decide that all animals are going to be equal simply by having an __cmp__ method in class animal that returns a zero. From version 2.2 onwards, you can also use a method called __eq__
b) The __str__ method creates a string from an object, and those strings could be compared - that's another form of equality in Python. And although identical objects will always return identical strings (unless you're using a time or random element in creating the string!), it's quite possible than non-identical objects will also return identical strings. So this is NOT going to check if two instance variables point to one and the same object.
c) The "trick" in in the __repr__ method, which has a shorthand in the backquote, Using this method, you can return the official string representation of any object, which includes trhe object's class and its address on the stack. And comparing these strings .. if two instance variables both return an identical string, then you can be reassured (stupid programming excepted!) that the are both instances pointing to the same variable.
I've written an example to show a) b) and c) in use - bring up the source in a separate window here. And you can see the results from running that below. Note that "mydesert" is copy of the "toffeepudding" variable (i.e. a reference to the same thingy) but all the other objects are at different addresses - even though "jamrollypolly" has identical settings to "toffeepudding"
toothpaste: A thingy which is sticky and with an edibility no
toffeepudding: A thingy which is sticky and with an edibility yes
jamrollypolly A thingy which is sticky and with an edibility yes
forum_topic: A thingy which is sticky and with an edibility no
water: A thingy which is wet and with an edibility no
mydesert: A thingy which is sticky and with an edibility yes
Use of ==, showing that == does NOT mean identity
Toothpaste and Toffeepudding are equal
Water and Toffeepudding are NOT equal
Comparing the print strings - does not test for same object
toothpaste and forum_topic are the same
toffeepudding and mydesert are the same
Compare two objects via __repr__ - are they the same object
toffeepudding and mydesert are identical refs to same object
toffeepudding: <__main__.thingy object at 0x68a70>
mydesert: <__main__.thingy object at 0x68a70>
jamrollypolly: <__main__.thingy object at 0x68a90>
It's getting late at night and this talk of food is leaving me rather hungry - I think I'll head off for some "Ice Cream" before "I Scream" at how complex a subject equality is. But I would be very happy to go through it - at length if necessary - on our Python Course