is this a bug?

David Lees deblnonospammy at theworld.com
Wed Aug 15 23:09:48 EDT 2001


Hmmm, I am sure there is a simple explanation.  From the quickee
variation of you example below, I infer that the Python compiler uses a
dictionary for storing literals, so that only 1 copy is of each unique
literal is kept at compile time.  When the same string is built at
run-time in my example, the compiler has already done its job so it has
no way to know the symbol just built is already stored somewhere.  This
is pure guesswork, so could one of the gurus post the right answer? 
Thanks.


def f():
    a='+='
    b='+='
    print id(a),id(b),id('+=')
    b='+'+'='
    print id(b)

f()

result of a run is:
27432416 27432416 27432416
27367504



Skip Montanaro wrote:
<snip>
> Try this:
> 
>     >>> def f():
>     ...   a = "+="
>     ...   print a is a
>     ...   print a is "+="
>     ...
>     >>> f()
>     1
>     1
> 
> It is left as an exercise for the reader why the result of executing f()
> produces different results than the previous interactive session.  ;-)
> 
> --
> Skip Montanaro (skip at pobox.com)
> http://www.mojam.com/
> http://www.musi-cal.com/



More information about the Python-list mailing list