0
>>> x = (1, 2)
>>> y = (1, 2)
>>> x is y

when I run this code in IDE it returns True but in cmd it returns False. so why is that so?

  • Surely, you want `==`, not `is`. You could print `id(x)` and `id(y)` to double-check – OneCricketeer Nov 17 '20 at 19:35
  • That's not what's being asked, OP is basically asking why `id(x) != id(y)` in "cmd" but equal in their IDE. – Random Davis Nov 17 '20 at 19:36
  • Which IDE are you using? Is that Pycharm? – Har Nov 17 '20 at 19:37
  • Whether or not `(1,2)` produces a distinct object each time it is used, or simply returns the same reference to a single object, is implementation-defined. – chepner Nov 17 '20 at 19:37
  • 2
    You're doing something that *has no guaranteed result*. This is like asking why you flipped a coin twice, but it came up heads one time, and tails the other. – jasonharper Nov 17 '20 at 19:37
  • its pycharm yes – PythonicG Nov 17 '20 at 19:37
  • 2
    Does this answer your question? [What's with the integer cache maintained by the interpreter?](https://stackoverflow.com/questions/15171695/whats-with-the-integer-cache-maintained-by-the-interpreter) I'm aware that this claims to be about ints, but the accepted answer answers this regardless of type: *This happens whenever the parser has a chance to analyze where the literals are used, for example when defining a function in the interactive interpreter* – Tomerikoo Nov 17 '20 at 19:38
  • @jasonharper Not really, as the result is (probably) going to be reproducible within a given implementation. – chepner Nov 17 '20 at 19:38
  • For example, try in the CMD to do it as `x = (1, 2) ; y = (1, 2)` (same line) and now it will produce `True` – Tomerikoo Nov 17 '20 at 19:43
  • https://github.com/satwikkansal/wtfpython#-how-not-to-use-is-operator – mportes Nov 17 '20 at 19:43

0 Answers0