| |||||||||||
| |||||||||||
Equality, sameness and identity - Python
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 noIt'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 (written 2008-05-31 22:35:14) Associated topics are indexed under Y103 - Python - Conditionals and LoopsY112 - Python - Objects - Intermediate
Some other Articles
Factory method example - PerlExample of OO in Perl Python in an afternoon - a lecture for experienced programmers Westonbirt Arboretum Postcode Equality, sameness and identity - Python Korn shell - some nuggets String, Integer, Array, Associative Array - ksh variables Some useful variables and settings in the Korn Shell Farewell, Newcastle to Stavanger, Haugsund and Bergen Be careful of misreading server statistics 1770 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 at 50 posts per pageThis is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price. Link to Ezine home page (for reading). Link to Blogging home page (to add comments). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||