On Sat, May 9, 2020 at 4:17 AM Alex Hall <alex.moj...@gmail.com> wrote:
>
> On Fri, May 8, 2020 at 7:52 PM David Mertz <me...@gnosis.cx> wrote:
>>>
>>> Me: For non-singleton immutables, identity is not really a meaningful 
>>> thing.  I mean, other than in a debugger or code profiler, or something 
>>> special like that. I honestly do not know whether, e.g. '(1, "a", 3.5) is 
>>> (1, "a", 3.5)'.  I'll go try it, but I won't be sure the answer for every 
>>> implementation, version, and even runtime, whether that answer will be 
>>> consistent.
>>
>>
>> So I did try it.  I did not necessarily expect these particular results.  
>> Moreover, I have a hunch that with PyPy JIT, something similar might 
>> actually give different answers at different points when the same line was 
>> encountered in a running interpreter.  Not this example, but something else 
>> that might cache values only later.
>>
>> I haven't done anything sneaky with the version at those paths.  They are 
>> all what the environment name hints they should be.  PyPy is at 3.6, which 
>> is the latest version on conda-forge.
>>
>> 810-tmp % $HOME/miniconda3/envs/py2.7/bin/python -c 'print((1, "a", 3.5) is 
>> (1, "a", 3.5))'
>> False
>> 811-tmp % $HOME/miniconda3/envs/py3.4/bin/python -c 'print((1, "a", 3.5) is 
>> (1, "a", 3.5))'
>> False
>> 812-tmp % $HOME/miniconda3/envs/py3.8/bin/python -c 'print((1, "a", 3.5) is 
>> (1, "a", 3.5))'
>> <string>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
>> True
>> 813-tmp % $HOME/miniconda3/envs/pypy/bin/python -c 'print((1, "a", 3.5) is 
>> (1, "a", 3.5))'
>> True
>> 814-tmp % $HOME/miniconda3/envs/py1/bin/python -c 'print (1, "a", 3.5) is 
>> (1, "a", 3.5)'
>> 0
>
>
> This is because of the peephole optimiser, right?
>
> ```
> Python 3.8.0 (default, Oct 30 2019, 12:16:01)
> [GCC 7.4.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> (1, "a", 3.5) is (1, "a", 3.5)
> <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
> True
> >>> x = (1, "a", 3.5)
> >>> x == (1, "a", 3.5)
> True
> >>> x is (1, "a", 3.5)
> <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
> False
> >>>
> ```

I think you're more seeing the module compilation optimizations here.
Inside a single compilation unit (usually a module), constants will
often be shared. So, for instance:

>>> exec("""
... x = "Hello, world!"
... y = "Hello, world!"
... print(x is y)
... """)
True

But if you do those lines individually at the REPL, you'll get False.
Of course, a compliant Python interpreter is free to either collapse
them or keep them separate, but this optimization helps to keep .pyc
file sizes down, for instance.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BYN2KYK3CF7X7LQ5NWVFNQPKRUR7CMCQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to