455,548 Members | 1,496 Online
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

# Equality and identity

 P: n/a In python, it seems like immutable objects are equal under both equality and identity: 5 == 5 True 5 is 5 True "hei" == "hei" True "hei" is "hei" True But that isn't true for tuples: (5,2) == (5,2) True (5,2) is (5,2) False Why not? Are tuples mutable in some way I've missed, or are there other reasons for this behaviour? To me, it seems natural to concider (5,2) and (5,2) the same object, just like "5,2" and "5,2". Marius -- "Who needs crypto WYCWLT?" -- William Newman Jul 18 '05 #1
6 Replies

 P: n/a In article <3c*************@nelja.ifi.uio.no>, Marius Bernklev wrote: In python, it seems like immutable objects are equal under both equality and identity: 5 == 5 True 5 is 5 True "hei" == "hei" True "hei" is "hei" True But that isn't true for tuples: (5,2) == (5,2) True (5,2) is (5,2) False Why not? Are tuples mutable in some way I've missed, or are there other reasons for this behaviour? To me, it seems natural to concider (5,2) and (5,2) the same object, just like "5,2" and "5,2". Note that the Python interpreter is a program, with finite limits on its implementation. It is not an algebraic system. If you find that (5,2) and (5,2) are not the same object, rather than trying to infer properties of types from this, first you might ask `what would it take, as implementor of Python, to always use the same object for these two quantities?' and, `how important is it to do so?' This may lead you to make a few more experiments with strings and find some holes in your identity proposition there, too. Don't make the mistake that people seem recently so prone to make, of an unhealthy preoccupation with "is" and identity. When you need to test for equality, always use "==". Donn Cave, do**@u.washington.edu Jul 18 '05 #2