Is this a bug? Variables are identical references to the same string in this example (Python)

后端 未结 3 1620
说谎
说谎 2021-02-15 18:57

This is for Python 2.6.

I could not figure out why a and b are identical:

>>> a = \"some_string\"
>>> b = \"some_string\"
>>>          


        
3条回答
  •  半阙折子戏
    2021-02-15 19:09

    This should actually be more of a comment to Gleen's answer but I can not do comments yet. I've run some tests directly on the Python interpreter and I saw some interesting behavior. According to Glenn, the interpreter treats entries as separate "files" and they don't share a string table when stored for future reference. Here is what I run:

    >>> a="some_string"
    >>> b="some_string"
    >>> id(a)
    2146597048
    >>> id(b)
    2146597048
    >>> a="some string"
    >>> b="some string"
    >>> id(a)
    2146597128
    >>> id(b)
    2146597088
    >>> c="some string"   <-----(1)
    >>> d="some string"   
    >>> id(c)
    2146597208            <-----(1)
    >>> a="some_string"
    >>> b="some_string"
    >>> id(a)
    2146597248            <---- waited a few minutes
    >>> c="some_string" 
    >>> d="some_string"
    >>> id(d)
    2146597248            <---- still same id after a few min
    >>> b="some string"
    >>> id(b)
    2146597288
    >>> b="some_string"  <---(2)
    >>> id(b)
    2146597248           <---(2)
    >>> a="some"
    >>> b="some"
    >>> c="some"
    >>> d="some"         <---(2) lost all references
    >>> id(a)
    2146601728
    >>> a="some_string"  <---(2)
    >>> id(a)
    2146597248           <---(2) returns same old one after mere seconds
    >>> a="some"
    >>> id(a)
    2146601728           <---(2) Waited a few minutes
    >>> a="some_string"   <---- (1)
    >>> id(a)
    2146597208            <---- (1) Reused a "different" id after a few minutes
    

    It seems that some of the id references might be reused after the initial references are lost and no longer "in use" (1), but it might also be related to the time those id references are not being used, as you can see in what i have marked as number (2), giving different id references depending on how long that id has not been used. I just find it curious and thought of posting it.

提交回复
热议问题