"/a" is not "/a" ?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Mar 7 00:49:50 CET 2009


En Fri, 06 Mar 2009 19:31:02 -0200, Emanuele D'Arrigo <manu3d at gmail.com>  
escribió:

>>>> a = "a"
>>>> b = "a"
>>>> a is b
> True
>
>>>> a = "/a"  <- same as above, except the forward slashes!
>>>> b = "/a"  <- same as above, except the forward slashes!
>>>> a is b
> False
>
> So, it appears that in the first case a and b are names to the same
> string object, while in the second case they are to two separate
> objects. Why? What's so special about the forward slash that cause the
> two "/a" strings to create two separate objects? Is this an
> implementation-specific issue?

With all the answers you got, I hope you now understand that you put the  
question backwards: it's not "why aren't a and b the very same object in  
the second case?" but "why are they the same object in the first case?".

Two separate expressions, involving two separate literals, don't *have* to  
evaluate as the same object. Only because strings are immutable the  
interpreter *may* choose to re-use the same string. But Python would still  
be Python even if all those strings were separate objects (although it  
would perform a lot slower!)

-- 
Gabriel Genellina




More information about the Python-list mailing list