-2

i create two identical tuples and use is operator on them the answer that should come is false but when i use it in vscode/atom/notepadd++ it comes true but when i use the same code in pthon run through cmd it comes false

a = (1,2,3)
b = (1,2,3)

print(a is b)

actual result should be false and it is false when i use python through cmd or some online python compiler but when i used vscode to write the above code and create a .py file it comes true. The following picture shows what i am trying to say.

My code in vscode and executed through terminal and directly in terminal:

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • CPython does not promise to intern immutatble objects (strings/tuple) by default, but in practice, a lot of places in the Python codebase do reuse already-created objects. This is the reason if you check id for such small objects is same. – Mahesh Karia Jan 07 '19 at 09:07
  • Which Python is being used by the IDE? Are you using the same one yourself? Since a tuple of integers is a completely immutable, I could see other versions of Python interning the object, resulting in them having the same id. I don't expect this out of CPython though--it would have been helpful to see the rest of the version string in the picture you took. – John Szakmeister Jan 07 '19 at 09:10
  • As an example, PyPy will intern the tuple, at least if run against the script rather than interactively. – John Szakmeister Jan 07 '19 at 09:12
  • i am using python 3.7. and when code is written in ide but executed using a terminal – Shehryar khan Jan 07 '19 at 11:00
  • It looks like this is a new feature of Python 3.7 as a result of [some work](https://github.com/python/cpython/commit/7ea143ae795a9fd57eaccf490d316bdc13ee9065) on the AST-level constant folding (because a tuple of numbers is really just a constant) and [some work](https://github.com/python/cpython/commit/87010e85cb37192d63b1a30e5fabba307ad5a3f5) on the peephole optimizer. – John Szakmeister Jan 07 '19 at 12:46
  • so if anyone can tell me whats happening and what should i do. because i am learning python and now i am stuck at this – Shehryar khan Jan 07 '19 at 14:51
  • What is happening is that Python 3.7 now treats `(1, 2, 3)` as a constant and is, in effect, interning the result when running as a script (it's sharing the same value in the compiled version of the file). It is safe for Python to do this because tuples and integers are both immutable, which means tuples of integers are also immutable and safe to share--just like what Python does for strings. Why is this a problem for you? If the material for the class requires the ids to not be the same, then you'll need to fall back to the latest in the Python 3.6. 3.6 will produce different ids still. – John Szakmeister Jan 07 '19 at 16:02

2 Answers2

0

I checked it in linux terminal result is False. It is because a is b will become True when a and b are pointing to same object. is does not compare values of the tuples. If you define b=a then you will get True. You can check this discussion Python "is" statement: what is happening?

user10340258
  • 339
  • 1
  • 5
  • 15
0

This a change introduced in Python 3.7.0 alpha 4 by change bpo-29469: Move constant folding from bytecode layer to AST layer.

We see different behavior between the command line (REPL) and a script. Each line entered at the command line is compiled and executed when entered. All of a script is compiled at once.

In general, for ordinary comparisons, you should use the equality operator (==) instead of the identity operator (is). For all objects the equality operator has a variety of strategies for completing the comparison (actually each class can provide its own __eq__ operator). The identity operator will only compare the objects' ids.

Doug Henderson
  • 785
  • 8
  • 17
  • To confirm this behavior for any python version, at the python interactive prompt, enter `a=(1,2,3); b=(1,2,3); print(a is b)` on one line. – Doug Henderson Jan 07 '19 at 22:35
  • wow i mean ur amazing thank u so much for your help i was just stuck at another thing just like this and you saved my time. Just one thing normally the whole script is compiled at once and command line is used for teaching purpose. which one is correct thing to do command line or the entire script?? – Shehryar khan Jan 13 '19 at 17:58
  • so @Doug Henderson each line in idle this >>> mean this is gonna be 1 script and the next >>> will be another script – Shehryar khan Jan 15 '19 at 00:56