Angus Rodgers wrote: > Hello, world! > > This is my first post to the Tutor list (although I've already > posted to comp.lang.python a couple of times). > > I'm currently reading Chapter 4 of Wesley Chun's book, "Core > Python Programming" (2nd ed.). > > I find this, in Python 2.5.4, on my Win98SE system (using IDLE): > >>>> 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?
In this particular case, the reason is because "colourless" can be used as an identifier. Now, before I continue, I need to warn, never to rely on any sort of interning behavior; as they are strictly implementation detail, implementation specific, and is NOT the behavior of Python (but only the behavior of CPython). Identifier are strings that can be used for names (variables). In python most name lookups are actually a dict lookup (except for locals). Strings that can be used as identifier are interned to speed up these lookups. Identifiers cannot contain spaces, that's why green ideas are not interned. And... have I told you not to rely on this behavior? NEVER rely on this implementation details. Once you get bitten by it, you'll regret it. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor