>>>> n = "colourless" >>>> o = "colourless" >>>> n == o > True >>>> n is o > True >>>> p = "green ideas" >>>> q = "green ideas" >>>> p == q > True >>>> p is q > False > > Why the difference?
angus, welcome to Python! you're definitely doing your homework when it comes to trying to understand how Python manages its memory. with regards to your query, you came up with a very good example. fortunately, it's not that important for beginners to fully understand this behavior because for the most part when dealing with strings, you will rarely *care* about whether two variables reference the same string object (or not). you will just use "==" as you did above, and both cases result in the same consistent answer. checking object identity (using 'is') occurs most commonly when comparing a variable against a well-known constant such as True, False, and None. the remaining use cases are where you really do care that a pair of variables refers to the exact same object. regardless, with respect to you original question (which is asked every now and then), there is no hardcoded rule or algorithm that is published because it may change in subsequent versions of Python. the main idea is that short and often-used strings (such as those which can be attribute names or dictionary keys) are interned while the rest are not. here is another reply that someone posted several years ago that may also help: http://groups.google.com/group/comp.lang.python/msg/e4c9c360e19d9c78 hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Python Web Development with Django", Addison Wesley, (c) 2009 http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor