How does Python know the values already stored in its memory? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Which Python memory profiler is recommended?How can I safely create a nested directory in Python?How to return multiple values from a function?How do I parse a string to a float or int in Python?Does Python have a ternary conditional operator?Convert bytes to a string?How do I sort a dictionary by value?“Least Astonishment” and the Mutable Default ArgumentDoes Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

Significance of Cersei's obsession with elephants?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?

Why wasn't DOSKEY integrated with COMMAND.COM?

Project Euler #1 in C++

How to write the following sign?

Did Deadpool rescue all of the X-Force?

How do living politicians protect their readily obtainable signatures from misuse?

How can I reduce the gap between left and right of cdot with a macro?

How to react to hostile behavior from a senior developer?

What is a fractional matching?

Morning, Afternoon, Night Kanji

Using audio cues to encourage good posture

How were pictures turned from film to a big picture in a picture frame before digital scanning?

What do you call the main part of a joke?

What does it mean that physics no longer uses mechanical models to describe phenomena?

What would you call this weird metallic apparatus that allows you to lift people?

Time to Settle Down!

Find 108 by using 3,4,6

Trademark violation for app?

Performance gap between vector<bool> and array

Why weren't discrete x86 CPUs ever used in game hardware?

What is the difference between globalisation and imperialism?

What initially awakened the Balrog?



How does Python know the values already stored in its memory?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Which Python memory profiler is recommended?How can I safely create a nested directory in Python?How to return multiple values from a function?How do I parse a string to a float or int in Python?Does Python have a ternary conditional operator?Convert bytes to a string?How do I sort a dictionary by value?“Least Astonishment” and the Mutable Default ArgumentDoes Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








7















I want to know how Python knows (if it knows) if a value-type object is already stored in its memory (and also knows where it is).



For this code, when assigning the value 1 for b, how does it know that the value 1 is already in its memory and stores its reference in b?



>>> a = 1
>>> b = 1
>>> a is b
True









share|improve this question









New contributor




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




















  • Use print(hex(id(b))) to check memory address for b

    – Yusufsn
    21 mins ago












  • >>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'

    – Just A Lone
    19 mins ago











  • the values are the same

    – Just A Lone
    19 mins ago






  • 1





    If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.

    – Yusufsn
    18 mins ago







  • 1





    @Yusufsn No. For bigger integers (>256) it's not true.

    – ajnLJA-0184
    13 mins ago

















7















I want to know how Python knows (if it knows) if a value-type object is already stored in its memory (and also knows where it is).



For this code, when assigning the value 1 for b, how does it know that the value 1 is already in its memory and stores its reference in b?



>>> a = 1
>>> b = 1
>>> a is b
True









share|improve this question









New contributor




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




















  • Use print(hex(id(b))) to check memory address for b

    – Yusufsn
    21 mins ago












  • >>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'

    – Just A Lone
    19 mins ago











  • the values are the same

    – Just A Lone
    19 mins ago






  • 1





    If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.

    – Yusufsn
    18 mins ago







  • 1





    @Yusufsn No. For bigger integers (>256) it's not true.

    – ajnLJA-0184
    13 mins ago













7












7








7


1






I want to know how Python knows (if it knows) if a value-type object is already stored in its memory (and also knows where it is).



For this code, when assigning the value 1 for b, how does it know that the value 1 is already in its memory and stores its reference in b?



>>> a = 1
>>> b = 1
>>> a is b
True









share|improve this question









New contributor




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












I want to know how Python knows (if it knows) if a value-type object is already stored in its memory (and also knows where it is).



For this code, when assigning the value 1 for b, how does it know that the value 1 is already in its memory and stores its reference in b?



>>> a = 1
>>> b = 1
>>> a is b
True






python python-3.x memory






share|improve this question









New contributor




Just A Lone 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




Just A Lone 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 8 mins ago









user11206537

149115




149115






New contributor




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









asked 25 mins ago









Just A LoneJust A Lone

384




384




New contributor




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





New contributor





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






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












  • Use print(hex(id(b))) to check memory address for b

    – Yusufsn
    21 mins ago












  • >>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'

    – Just A Lone
    19 mins ago











  • the values are the same

    – Just A Lone
    19 mins ago






  • 1





    If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.

    – Yusufsn
    18 mins ago







  • 1





    @Yusufsn No. For bigger integers (>256) it's not true.

    – ajnLJA-0184
    13 mins ago

















  • Use print(hex(id(b))) to check memory address for b

    – Yusufsn
    21 mins ago












  • >>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'

    – Just A Lone
    19 mins ago











  • the values are the same

    – Just A Lone
    19 mins ago






  • 1





    If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.

    – Yusufsn
    18 mins ago







  • 1





    @Yusufsn No. For bigger integers (>256) it's not true.

    – ajnLJA-0184
    13 mins ago
















Use print(hex(id(b))) to check memory address for b

– Yusufsn
21 mins ago






Use print(hex(id(b))) to check memory address for b

– Yusufsn
21 mins ago














>>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'

– Just A Lone
19 mins ago





>>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'

– Just A Lone
19 mins ago













the values are the same

– Just A Lone
19 mins ago





the values are the same

– Just A Lone
19 mins ago




1




1





If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.

– Yusufsn
18 mins ago






If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.

– Yusufsn
18 mins ago





1




1





@Yusufsn No. For bigger integers (>256) it's not true.

– ajnLJA-0184
13 mins ago





@Yusufsn No. For bigger integers (>256) it's not true.

– ajnLJA-0184
13 mins ago












4 Answers
4






active

oldest

votes


















7














Python uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.



a = 10e5
b = 10e5
a is b # False


Wait, what? If you check the address of the numbers, you'll find something interesting:



a = 1
b = 1
id(a) # 4463034512
id(b) # 4463034512

a = 257
b = 257
id(a) # 4642585200
id(b) # 4642585712


It's called integer cache. You can read more about the integer cache here: https://wsvincent.com/python-wat-integer-cache/




“The current implementation keeps an array of integer objects for all
integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object.”




Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.






share|improve this answer










New contributor




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



























    2














    If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.






    share|improve this answer






























      0














      Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.






      share|improve this answer








      New contributor




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




















      • I think you're missing the point of the question. a == b is obviously true. OP is asking why a is b is true.

        – Mad Physicist
        19 mins ago


















      0














      Why?



      is is kinda confusing:



      @ajnLJA-0184 is correct, but additionally, strings work well too, but when you do some operations to it, but still the same strings, nope:



      >>> a = 'python'
      >>> b = 'python'
      >>> a is b
      True
      >>> a = 'pytho' + 'n'
      >>> b = 'pythonn'[:-1]
      >>> a is b
      False
      >>>


      But strange enough, it is different for integers:



      >>> a = 1
      >>> b = 1
      >>> a is b
      True
      >>> a = 3*8
      >>> b = 4*6
      >>> a is b
      True
      >>>


      But as the link he gave, there you can see that:



      >>> a = 257
      >>> b = 257
      >>> a is b
      False
      >>>


      How to know if it is gonna be True or not?



      Well, here is when id comes in handy:



      Here you go, just type in id and two parens and say your varaible...:



      >>> a = 1
      >>> b = 1
      >>> id(a)
      1935522256
      >>> id(b)
      1935522256
      >>> a = 257 # or -6
      >>> b = 257 # or -6
      >>> id(a)
      935705330960
      >>> id(b)
      935705331216
      >>>





      share|improve this answer




















      • 1





        Why d is a equals False in the first example? That's new.

        – ajnLJA-0184
        2 mins ago











      • @ajnLJA-0 Because the strings aren't direct like 'python', they do an operation to get 'python', that's why.

        – U9-Forward
        1 min 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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      Just A Lone 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%2f55756029%2fhow-does-python-know-the-values-already-stored-in-its-memory%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      Python uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.



      a = 10e5
      b = 10e5
      a is b # False


      Wait, what? If you check the address of the numbers, you'll find something interesting:



      a = 1
      b = 1
      id(a) # 4463034512
      id(b) # 4463034512

      a = 257
      b = 257
      id(a) # 4642585200
      id(b) # 4642585712


      It's called integer cache. You can read more about the integer cache here: https://wsvincent.com/python-wat-integer-cache/




      “The current implementation keeps an array of integer objects for all
      integers between -5 and 256, when you create an int in that range you
      actually just get back a reference to the existing object.”




      Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.






      share|improve this answer










      New contributor




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
























        7














        Python uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.



        a = 10e5
        b = 10e5
        a is b # False


        Wait, what? If you check the address of the numbers, you'll find something interesting:



        a = 1
        b = 1
        id(a) # 4463034512
        id(b) # 4463034512

        a = 257
        b = 257
        id(a) # 4642585200
        id(b) # 4642585712


        It's called integer cache. You can read more about the integer cache here: https://wsvincent.com/python-wat-integer-cache/




        “The current implementation keeps an array of integer objects for all
        integers between -5 and 256, when you create an int in that range you
        actually just get back a reference to the existing object.”




        Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.






        share|improve this answer










        New contributor




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






















          7












          7








          7







          Python uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.



          a = 10e5
          b = 10e5
          a is b # False


          Wait, what? If you check the address of the numbers, you'll find something interesting:



          a = 1
          b = 1
          id(a) # 4463034512
          id(b) # 4463034512

          a = 257
          b = 257
          id(a) # 4642585200
          id(b) # 4642585712


          It's called integer cache. You can read more about the integer cache here: https://wsvincent.com/python-wat-integer-cache/




          “The current implementation keeps an array of integer objects for all
          integers between -5 and 256, when you create an int in that range you
          actually just get back a reference to the existing object.”




          Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.






          share|improve this answer










          New contributor




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










          Python uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.



          a = 10e5
          b = 10e5
          a is b # False


          Wait, what? If you check the address of the numbers, you'll find something interesting:



          a = 1
          b = 1
          id(a) # 4463034512
          id(b) # 4463034512

          a = 257
          b = 257
          id(a) # 4642585200
          id(b) # 4642585712


          It's called integer cache. You can read more about the integer cache here: https://wsvincent.com/python-wat-integer-cache/




          “The current implementation keeps an array of integer objects for all
          integers between -5 and 256, when you create an int in that range you
          actually just get back a reference to the existing object.”




          Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.







          share|improve this answer










          New contributor




          ajnLJA-0184 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 answer



          share|improve this answer








          edited 14 mins ago





















          New contributor




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









          answered 20 mins ago









          ajnLJA-0184ajnLJA-0184

          1884




          1884




          New contributor




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





          New contributor





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






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























              2














              If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.






              share|improve this answer



























                2














                If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.






                share|improve this answer

























                  2












                  2








                  2







                  If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.






                  share|improve this answer













                  If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 14 mins ago









                  Mad PhysicistMad Physicist

                  39k1682113




                  39k1682113





















                      0














                      Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.






                      share|improve this answer








                      New contributor




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




















                      • I think you're missing the point of the question. a == b is obviously true. OP is asking why a is b is true.

                        – Mad Physicist
                        19 mins ago















                      0














                      Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.






                      share|improve this answer








                      New contributor




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




















                      • I think you're missing the point of the question. a == b is obviously true. OP is asking why a is b is true.

                        – Mad Physicist
                        19 mins ago













                      0












                      0








                      0







                      Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.






                      share|improve this answer








                      New contributor




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










                      Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.







                      share|improve this answer








                      New contributor




                      Monster AR44 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 answer



                      share|improve this answer






                      New contributor




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









                      answered 21 mins ago









                      Monster AR44Monster AR44

                      111




                      111




                      New contributor




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





                      New contributor





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






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












                      • I think you're missing the point of the question. a == b is obviously true. OP is asking why a is b is true.

                        – Mad Physicist
                        19 mins ago

















                      • I think you're missing the point of the question. a == b is obviously true. OP is asking why a is b is true.

                        – Mad Physicist
                        19 mins ago
















                      I think you're missing the point of the question. a == b is obviously true. OP is asking why a is b is true.

                      – Mad Physicist
                      19 mins ago





                      I think you're missing the point of the question. a == b is obviously true. OP is asking why a is b is true.

                      – Mad Physicist
                      19 mins ago











                      0














                      Why?



                      is is kinda confusing:



                      @ajnLJA-0184 is correct, but additionally, strings work well too, but when you do some operations to it, but still the same strings, nope:



                      >>> a = 'python'
                      >>> b = 'python'
                      >>> a is b
                      True
                      >>> a = 'pytho' + 'n'
                      >>> b = 'pythonn'[:-1]
                      >>> a is b
                      False
                      >>>


                      But strange enough, it is different for integers:



                      >>> a = 1
                      >>> b = 1
                      >>> a is b
                      True
                      >>> a = 3*8
                      >>> b = 4*6
                      >>> a is b
                      True
                      >>>


                      But as the link he gave, there you can see that:



                      >>> a = 257
                      >>> b = 257
                      >>> a is b
                      False
                      >>>


                      How to know if it is gonna be True or not?



                      Well, here is when id comes in handy:



                      Here you go, just type in id and two parens and say your varaible...:



                      >>> a = 1
                      >>> b = 1
                      >>> id(a)
                      1935522256
                      >>> id(b)
                      1935522256
                      >>> a = 257 # or -6
                      >>> b = 257 # or -6
                      >>> id(a)
                      935705330960
                      >>> id(b)
                      935705331216
                      >>>





                      share|improve this answer




















                      • 1





                        Why d is a equals False in the first example? That's new.

                        – ajnLJA-0184
                        2 mins ago











                      • @ajnLJA-0 Because the strings aren't direct like 'python', they do an operation to get 'python', that's why.

                        – U9-Forward
                        1 min ago
















                      0














                      Why?



                      is is kinda confusing:



                      @ajnLJA-0184 is correct, but additionally, strings work well too, but when you do some operations to it, but still the same strings, nope:



                      >>> a = 'python'
                      >>> b = 'python'
                      >>> a is b
                      True
                      >>> a = 'pytho' + 'n'
                      >>> b = 'pythonn'[:-1]
                      >>> a is b
                      False
                      >>>


                      But strange enough, it is different for integers:



                      >>> a = 1
                      >>> b = 1
                      >>> a is b
                      True
                      >>> a = 3*8
                      >>> b = 4*6
                      >>> a is b
                      True
                      >>>


                      But as the link he gave, there you can see that:



                      >>> a = 257
                      >>> b = 257
                      >>> a is b
                      False
                      >>>


                      How to know if it is gonna be True or not?



                      Well, here is when id comes in handy:



                      Here you go, just type in id and two parens and say your varaible...:



                      >>> a = 1
                      >>> b = 1
                      >>> id(a)
                      1935522256
                      >>> id(b)
                      1935522256
                      >>> a = 257 # or -6
                      >>> b = 257 # or -6
                      >>> id(a)
                      935705330960
                      >>> id(b)
                      935705331216
                      >>>





                      share|improve this answer




















                      • 1





                        Why d is a equals False in the first example? That's new.

                        – ajnLJA-0184
                        2 mins ago











                      • @ajnLJA-0 Because the strings aren't direct like 'python', they do an operation to get 'python', that's why.

                        – U9-Forward
                        1 min ago














                      0












                      0








                      0







                      Why?



                      is is kinda confusing:



                      @ajnLJA-0184 is correct, but additionally, strings work well too, but when you do some operations to it, but still the same strings, nope:



                      >>> a = 'python'
                      >>> b = 'python'
                      >>> a is b
                      True
                      >>> a = 'pytho' + 'n'
                      >>> b = 'pythonn'[:-1]
                      >>> a is b
                      False
                      >>>


                      But strange enough, it is different for integers:



                      >>> a = 1
                      >>> b = 1
                      >>> a is b
                      True
                      >>> a = 3*8
                      >>> b = 4*6
                      >>> a is b
                      True
                      >>>


                      But as the link he gave, there you can see that:



                      >>> a = 257
                      >>> b = 257
                      >>> a is b
                      False
                      >>>


                      How to know if it is gonna be True or not?



                      Well, here is when id comes in handy:



                      Here you go, just type in id and two parens and say your varaible...:



                      >>> a = 1
                      >>> b = 1
                      >>> id(a)
                      1935522256
                      >>> id(b)
                      1935522256
                      >>> a = 257 # or -6
                      >>> b = 257 # or -6
                      >>> id(a)
                      935705330960
                      >>> id(b)
                      935705331216
                      >>>





                      share|improve this answer















                      Why?



                      is is kinda confusing:



                      @ajnLJA-0184 is correct, but additionally, strings work well too, but when you do some operations to it, but still the same strings, nope:



                      >>> a = 'python'
                      >>> b = 'python'
                      >>> a is b
                      True
                      >>> a = 'pytho' + 'n'
                      >>> b = 'pythonn'[:-1]
                      >>> a is b
                      False
                      >>>


                      But strange enough, it is different for integers:



                      >>> a = 1
                      >>> b = 1
                      >>> a is b
                      True
                      >>> a = 3*8
                      >>> b = 4*6
                      >>> a is b
                      True
                      >>>


                      But as the link he gave, there you can see that:



                      >>> a = 257
                      >>> b = 257
                      >>> a is b
                      False
                      >>>


                      How to know if it is gonna be True or not?



                      Well, here is when id comes in handy:



                      Here you go, just type in id and two parens and say your varaible...:



                      >>> a = 1
                      >>> b = 1
                      >>> id(a)
                      1935522256
                      >>> id(b)
                      1935522256
                      >>> a = 257 # or -6
                      >>> b = 257 # or -6
                      >>> id(a)
                      935705330960
                      >>> id(b)
                      935705331216
                      >>>






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 31 secs ago

























                      answered 10 mins ago









                      U9-ForwardU9-Forward

                      18.5k51744




                      18.5k51744







                      • 1





                        Why d is a equals False in the first example? That's new.

                        – ajnLJA-0184
                        2 mins ago











                      • @ajnLJA-0 Because the strings aren't direct like 'python', they do an operation to get 'python', that's why.

                        – U9-Forward
                        1 min ago













                      • 1





                        Why d is a equals False in the first example? That's new.

                        – ajnLJA-0184
                        2 mins ago











                      • @ajnLJA-0 Because the strings aren't direct like 'python', they do an operation to get 'python', that's why.

                        – U9-Forward
                        1 min ago








                      1




                      1





                      Why d is a equals False in the first example? That's new.

                      – ajnLJA-0184
                      2 mins ago





                      Why d is a equals False in the first example? That's new.

                      – ajnLJA-0184
                      2 mins ago













                      @ajnLJA-0 Because the strings aren't direct like 'python', they do an operation to get 'python', that's why.

                      – U9-Forward
                      1 min ago






                      @ajnLJA-0 Because the strings aren't direct like 'python', they do an operation to get 'python', that's why.

                      – U9-Forward
                      1 min ago











                      Just A Lone is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      Just A Lone is a new contributor. Be nice, and check out our Code of Conduct.












                      Just A Lone is a new contributor. Be nice, and check out our Code of Conduct.











                      Just A Lone is a new contributor. Be nice, and check out our Code of Conduct.














                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55756029%2fhow-does-python-know-the-values-already-stored-in-its-memory%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Magento 2 duplicate PHPSESSID cookie when using session_start() in custom php scriptMagento 2: User cant logged in into to account page, no error showing!Magento duplicate on subdomainGrabbing storeview from cookie (after using language selector)How do I run php custom script on magento2Magento 2: Include PHP script in headerSession lock after using Cm_RedisSessionscript php to update stockMagento set cookie popupMagento 2 session id cookie - where to find it?How to import Configurable product from csv with custom attributes using php scriptMagento 2 run custom PHP script

                      Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

                      How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2