Why does semicolon in python make a different result

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
12
down vote

favorite












I found a strange question about semicolon ";" in python.



>>> x=20000;y=20000
>>> x is y
True
>>> x=20000
>>> y=20000
>>> x is y
False
>>> x=20000;
>>> y=20000
>>> x is y
False


Why is it that the first test returns True and the other two return False?



Python Version: 3.6.5










share|improve this question









New contributor




longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    12
    down vote

    favorite












    I found a strange question about semicolon ";" in python.



    >>> x=20000;y=20000
    >>> x is y
    True
    >>> x=20000
    >>> y=20000
    >>> x is y
    False
    >>> x=20000;
    >>> y=20000
    >>> x is y
    False


    Why is it that the first test returns True and the other two return False?



    Python Version: 3.6.5










    share|improve this question









    New contributor




    longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      12
      down vote

      favorite









      up vote
      12
      down vote

      favorite











      I found a strange question about semicolon ";" in python.



      >>> x=20000;y=20000
      >>> x is y
      True
      >>> x=20000
      >>> y=20000
      >>> x is y
      False
      >>> x=20000;
      >>> y=20000
      >>> x is y
      False


      Why is it that the first test returns True and the other two return False?



      Python Version: 3.6.5










      share|improve this question









      New contributor




      longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I found a strange question about semicolon ";" in python.



      >>> x=20000;y=20000
      >>> x is y
      True
      >>> x=20000
      >>> y=20000
      >>> x is y
      False
      >>> x=20000;
      >>> y=20000
      >>> x is y
      False


      Why is it that the first test returns True and the other two return False?



      Python Version: 3.6.5







      python






      share|improve this question









      New contributor




      longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 38 mins ago









      NaruS

      6812




      6812






      New contributor




      longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      longshuai ma

      614




      614




      New contributor




      longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      longshuai ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          15
          down vote













          In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int value in each assignment, and so can (it doesn't have to, but does) make x and y references to the same object.



          The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ; that joins two statements into one.



          In the following two examples, by the time y=20000 is read and evaluated, x=20000 (with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y.






          share|improve this answer




















          • Thanks, i understand. then i also try x,y=20000,20000, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
            – longshuai ma
            43 mins ago










          • Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
            – chepner
            23 mins ago


















          up vote
          1
          down vote













          The is operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True and sometimes `False+ just to be a matter of luck.



          For example, the results are different in an interactive session and in a standalone program:



          $ cat test.py
          x = 200000; y = 200000
          print(x is y)

          xx = 200000
          yy = 200000
          print(xx is yy)

          $ python test.py
          True
          True


          Or you have this other example:



          >>> x = 50 + 50; y = 50 + 50
          >>> x is y
          True
          >>> x = 5000 + 5000; y = 5000 + 5000
          >>> x is y
          False


          This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000 object. It has nothing to do with the semicolon.






          share|improve this answer






















          • How come can one consider it to be a matter of luck while it isn't at all?
            – Işık Kaplan
            47 mins ago










          • and when it will return False?
            – mad_
            43 mins ago










          • @IşıkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
            – Roberto Bonvallet
            34 mins ago










          • @mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the is operator return False.
            – Roberto Bonvallet
            25 mins ago










          • There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
            – Işık Kaplan
            22 mins ago










          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52319436%2fwhy-does-semicolon-in-python-make-a-different-result%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          15
          down vote













          In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int value in each assignment, and so can (it doesn't have to, but does) make x and y references to the same object.



          The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ; that joins two statements into one.



          In the following two examples, by the time y=20000 is read and evaluated, x=20000 (with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y.






          share|improve this answer




















          • Thanks, i understand. then i also try x,y=20000,20000, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
            – longshuai ma
            43 mins ago










          • Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
            – chepner
            23 mins ago















          up vote
          15
          down vote













          In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int value in each assignment, and so can (it doesn't have to, but does) make x and y references to the same object.



          The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ; that joins two statements into one.



          In the following two examples, by the time y=20000 is read and evaluated, x=20000 (with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y.






          share|improve this answer




















          • Thanks, i understand. then i also try x,y=20000,20000, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
            – longshuai ma
            43 mins ago










          • Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
            – chepner
            23 mins ago













          up vote
          15
          down vote










          up vote
          15
          down vote









          In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int value in each assignment, and so can (it doesn't have to, but does) make x and y references to the same object.



          The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ; that joins two statements into one.



          In the following two examples, by the time y=20000 is read and evaluated, x=20000 (with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y.






          share|improve this answer












          In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int value in each assignment, and so can (it doesn't have to, but does) make x and y references to the same object.



          The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ; that joins two statements into one.



          In the following two examples, by the time y=20000 is read and evaluated, x=20000 (with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          chepner

          229k27214311




          229k27214311











          • Thanks, i understand. then i also try x,y=20000,20000, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
            – longshuai ma
            43 mins ago










          • Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
            – chepner
            23 mins ago

















          • Thanks, i understand. then i also try x,y=20000,20000, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
            – longshuai ma
            43 mins ago










          • Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
            – chepner
            23 mins ago
















          Thanks, i understand. then i also try x,y=20000,20000, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
          – longshuai ma
          43 mins ago




          Thanks, i understand. then i also try x,y=20000,20000, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
          – longshuai ma
          43 mins ago












          Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
          – chepner
          23 mins ago





          Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
          – chepner
          23 mins ago













          up vote
          1
          down vote













          The is operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True and sometimes `False+ just to be a matter of luck.



          For example, the results are different in an interactive session and in a standalone program:



          $ cat test.py
          x = 200000; y = 200000
          print(x is y)

          xx = 200000
          yy = 200000
          print(xx is yy)

          $ python test.py
          True
          True


          Or you have this other example:



          >>> x = 50 + 50; y = 50 + 50
          >>> x is y
          True
          >>> x = 5000 + 5000; y = 5000 + 5000
          >>> x is y
          False


          This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000 object. It has nothing to do with the semicolon.






          share|improve this answer






















          • How come can one consider it to be a matter of luck while it isn't at all?
            – Işık Kaplan
            47 mins ago










          • and when it will return False?
            – mad_
            43 mins ago










          • @IşıkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
            – Roberto Bonvallet
            34 mins ago










          • @mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the is operator return False.
            – Roberto Bonvallet
            25 mins ago










          • There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
            – Işık Kaplan
            22 mins ago














          up vote
          1
          down vote













          The is operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True and sometimes `False+ just to be a matter of luck.



          For example, the results are different in an interactive session and in a standalone program:



          $ cat test.py
          x = 200000; y = 200000
          print(x is y)

          xx = 200000
          yy = 200000
          print(xx is yy)

          $ python test.py
          True
          True


          Or you have this other example:



          >>> x = 50 + 50; y = 50 + 50
          >>> x is y
          True
          >>> x = 5000 + 5000; y = 5000 + 5000
          >>> x is y
          False


          This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000 object. It has nothing to do with the semicolon.






          share|improve this answer






















          • How come can one consider it to be a matter of luck while it isn't at all?
            – Işık Kaplan
            47 mins ago










          • and when it will return False?
            – mad_
            43 mins ago










          • @IşıkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
            – Roberto Bonvallet
            34 mins ago










          • @mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the is operator return False.
            – Roberto Bonvallet
            25 mins ago










          • There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
            – Işık Kaplan
            22 mins ago












          up vote
          1
          down vote










          up vote
          1
          down vote









          The is operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True and sometimes `False+ just to be a matter of luck.



          For example, the results are different in an interactive session and in a standalone program:



          $ cat test.py
          x = 200000; y = 200000
          print(x is y)

          xx = 200000
          yy = 200000
          print(xx is yy)

          $ python test.py
          True
          True


          Or you have this other example:



          >>> x = 50 + 50; y = 50 + 50
          >>> x is y
          True
          >>> x = 5000 + 5000; y = 5000 + 5000
          >>> x is y
          False


          This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000 object. It has nothing to do with the semicolon.






          share|improve this answer














          The is operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True and sometimes `False+ just to be a matter of luck.



          For example, the results are different in an interactive session and in a standalone program:



          $ cat test.py
          x = 200000; y = 200000
          print(x is y)

          xx = 200000
          yy = 200000
          print(xx is yy)

          $ python test.py
          True
          True


          Or you have this other example:



          >>> x = 50 + 50; y = 50 + 50
          >>> x is y
          True
          >>> x = 5000 + 5000; y = 5000 + 5000
          >>> x is y
          False


          This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000 object. It has nothing to do with the semicolon.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 31 mins ago

























          answered 59 mins ago









          Roberto Bonvallet

          19.9k53349




          19.9k53349











          • How come can one consider it to be a matter of luck while it isn't at all?
            – Işık Kaplan
            47 mins ago










          • and when it will return False?
            – mad_
            43 mins ago










          • @IşıkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
            – Roberto Bonvallet
            34 mins ago










          • @mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the is operator return False.
            – Roberto Bonvallet
            25 mins ago










          • There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
            – Işık Kaplan
            22 mins ago
















          • How come can one consider it to be a matter of luck while it isn't at all?
            – Işık Kaplan
            47 mins ago










          • and when it will return False?
            – mad_
            43 mins ago










          • @IşıkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
            – Roberto Bonvallet
            34 mins ago










          • @mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the is operator return False.
            – Roberto Bonvallet
            25 mins ago










          • There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
            – Işık Kaplan
            22 mins ago















          How come can one consider it to be a matter of luck while it isn't at all?
          – Işık Kaplan
          47 mins ago




          How come can one consider it to be a matter of luck while it isn't at all?
          – Işık Kaplan
          47 mins ago












          and when it will return False?
          – mad_
          43 mins ago




          and when it will return False?
          – mad_
          43 mins ago












          @IşıkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
          – Roberto Bonvallet
          34 mins ago




          @IşıkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
          – Roberto Bonvallet
          34 mins ago












          @mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the is operator return False.
          – Roberto Bonvallet
          25 mins ago




          @mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the is operator return False.
          – Roberto Bonvallet
          25 mins ago












          There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
          – Işık Kaplan
          22 mins ago




          There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
          – Işık Kaplan
          22 mins ago










          longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.












          longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.











          longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52319436%2fwhy-does-semicolon-in-python-make-a-different-result%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery