How does the Python's stack work when dealing with tuples/lists?












1















I saw in Python documentation that the BUILD_TUPLE instruction "Creates a tuple consuming count items from the stack, and pushes the resulting tuple onto the stack."



It really pushes the tuple itself? What if the tuple contains a large number of elements? How it is placed on stack?










share|improve this question



























    1















    I saw in Python documentation that the BUILD_TUPLE instruction "Creates a tuple consuming count items from the stack, and pushes the resulting tuple onto the stack."



    It really pushes the tuple itself? What if the tuple contains a large number of elements? How it is placed on stack?










    share|improve this question

























      1












      1








      1








      I saw in Python documentation that the BUILD_TUPLE instruction "Creates a tuple consuming count items from the stack, and pushes the resulting tuple onto the stack."



      It really pushes the tuple itself? What if the tuple contains a large number of elements? How it is placed on stack?










      share|improve this question














      I saw in Python documentation that the BUILD_TUPLE instruction "Creates a tuple consuming count items from the stack, and pushes the resulting tuple onto the stack."



      It really pushes the tuple itself? What if the tuple contains a large number of elements? How it is placed on stack?







      python python-3.x stack python-internals pythoninterpreter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 19:50









      FelipeFelipe

      113




      113
























          2 Answers
          2






          active

          oldest

          votes


















          2














          further to @juanpa.arrivillaga's answer and me playing with the dis module for the first time…



          it might be instructive to disassemble the trivial function:



          def foo(a, b, c):
          return (a, b, c)


          which results in:



            2           0 LOAD_FAST                0 (a)
          2 LOAD_FAST 1 (b)
          4 LOAD_FAST 2 (c)
          6 BUILD_TUPLE 3
          8 RETURN_VALUE


          in other words: we're making sure the stack has the correct parameter values on the top, then pops them all off and replaces them with a (reference to a) single tuple.



          this is how stack machines traditionally operate, which I believe CPython is (at least partially) modelled after, e.g. What does it mean that python is stack based?






          share|improve this answer































            1














            This answer applies to CPython specifically, but all CPython objects live on a private heap.




            Memory management in Python involves a private heap containing all
            Python objects and data structures. The management of this private
            heap is ensured internally by the Python memory manager. The Python
            memory manager has different components which deal with various
            dynamic storage management aspects, like sharing, segmentation,
            preallocation or caching.



            At the lowest level, a raw memory allocator ensures that there is
            enough room in the private heap for storing all Python-related data by
            interacting with the memory manager of the operating system. On top of
            the raw memory allocator, several object-specific allocators operate
            on the same heap and implement distinct memory management policies
            adapted to the peculiarities of every object type. For example,
            integer objects are managed differently within the heap than strings,
            tuples or dictionaries because integers imply different storage
            requirements and speed/space tradeoffs. The Python memory manager thus
            delegates some of the work to the object-specific allocators, but
            ensures that the latter operate within the bounds of the private heap.




            Note, everything in Python is an object. The only thing going onto the interpreter stack is a PyObject pointer. The stack here being an implementation detail of the CPython runtime. Source code is compiled to bytecode, which is executed on this stack-based virtual machine.






            share|improve this answer























              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
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364821%2fhow-does-the-pythons-stack-work-when-dealing-with-tuples-lists%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              further to @juanpa.arrivillaga's answer and me playing with the dis module for the first time…



              it might be instructive to disassemble the trivial function:



              def foo(a, b, c):
              return (a, b, c)


              which results in:



                2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST 1 (b)
              4 LOAD_FAST 2 (c)
              6 BUILD_TUPLE 3
              8 RETURN_VALUE


              in other words: we're making sure the stack has the correct parameter values on the top, then pops them all off and replaces them with a (reference to a) single tuple.



              this is how stack machines traditionally operate, which I believe CPython is (at least partially) modelled after, e.g. What does it mean that python is stack based?






              share|improve this answer




























                2














                further to @juanpa.arrivillaga's answer and me playing with the dis module for the first time…



                it might be instructive to disassemble the trivial function:



                def foo(a, b, c):
                return (a, b, c)


                which results in:



                  2           0 LOAD_FAST                0 (a)
                2 LOAD_FAST 1 (b)
                4 LOAD_FAST 2 (c)
                6 BUILD_TUPLE 3
                8 RETURN_VALUE


                in other words: we're making sure the stack has the correct parameter values on the top, then pops them all off and replaces them with a (reference to a) single tuple.



                this is how stack machines traditionally operate, which I believe CPython is (at least partially) modelled after, e.g. What does it mean that python is stack based?






                share|improve this answer


























                  2












                  2








                  2







                  further to @juanpa.arrivillaga's answer and me playing with the dis module for the first time…



                  it might be instructive to disassemble the trivial function:



                  def foo(a, b, c):
                  return (a, b, c)


                  which results in:



                    2           0 LOAD_FAST                0 (a)
                  2 LOAD_FAST 1 (b)
                  4 LOAD_FAST 2 (c)
                  6 BUILD_TUPLE 3
                  8 RETURN_VALUE


                  in other words: we're making sure the stack has the correct parameter values on the top, then pops them all off and replaces them with a (reference to a) single tuple.



                  this is how stack machines traditionally operate, which I believe CPython is (at least partially) modelled after, e.g. What does it mean that python is stack based?






                  share|improve this answer













                  further to @juanpa.arrivillaga's answer and me playing with the dis module for the first time…



                  it might be instructive to disassemble the trivial function:



                  def foo(a, b, c):
                  return (a, b, c)


                  which results in:



                    2           0 LOAD_FAST                0 (a)
                  2 LOAD_FAST 1 (b)
                  4 LOAD_FAST 2 (c)
                  6 BUILD_TUPLE 3
                  8 RETURN_VALUE


                  in other words: we're making sure the stack has the correct parameter values on the top, then pops them all off and replaces them with a (reference to a) single tuple.



                  this is how stack machines traditionally operate, which I believe CPython is (at least partially) modelled after, e.g. What does it mean that python is stack based?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 '18 at 21:45









                  Sam MasonSam Mason

                  3,30211331




                  3,30211331

























                      1














                      This answer applies to CPython specifically, but all CPython objects live on a private heap.




                      Memory management in Python involves a private heap containing all
                      Python objects and data structures. The management of this private
                      heap is ensured internally by the Python memory manager. The Python
                      memory manager has different components which deal with various
                      dynamic storage management aspects, like sharing, segmentation,
                      preallocation or caching.



                      At the lowest level, a raw memory allocator ensures that there is
                      enough room in the private heap for storing all Python-related data by
                      interacting with the memory manager of the operating system. On top of
                      the raw memory allocator, several object-specific allocators operate
                      on the same heap and implement distinct memory management policies
                      adapted to the peculiarities of every object type. For example,
                      integer objects are managed differently within the heap than strings,
                      tuples or dictionaries because integers imply different storage
                      requirements and speed/space tradeoffs. The Python memory manager thus
                      delegates some of the work to the object-specific allocators, but
                      ensures that the latter operate within the bounds of the private heap.




                      Note, everything in Python is an object. The only thing going onto the interpreter stack is a PyObject pointer. The stack here being an implementation detail of the CPython runtime. Source code is compiled to bytecode, which is executed on this stack-based virtual machine.






                      share|improve this answer




























                        1














                        This answer applies to CPython specifically, but all CPython objects live on a private heap.




                        Memory management in Python involves a private heap containing all
                        Python objects and data structures. The management of this private
                        heap is ensured internally by the Python memory manager. The Python
                        memory manager has different components which deal with various
                        dynamic storage management aspects, like sharing, segmentation,
                        preallocation or caching.



                        At the lowest level, a raw memory allocator ensures that there is
                        enough room in the private heap for storing all Python-related data by
                        interacting with the memory manager of the operating system. On top of
                        the raw memory allocator, several object-specific allocators operate
                        on the same heap and implement distinct memory management policies
                        adapted to the peculiarities of every object type. For example,
                        integer objects are managed differently within the heap than strings,
                        tuples or dictionaries because integers imply different storage
                        requirements and speed/space tradeoffs. The Python memory manager thus
                        delegates some of the work to the object-specific allocators, but
                        ensures that the latter operate within the bounds of the private heap.




                        Note, everything in Python is an object. The only thing going onto the interpreter stack is a PyObject pointer. The stack here being an implementation detail of the CPython runtime. Source code is compiled to bytecode, which is executed on this stack-based virtual machine.






                        share|improve this answer


























                          1












                          1








                          1







                          This answer applies to CPython specifically, but all CPython objects live on a private heap.




                          Memory management in Python involves a private heap containing all
                          Python objects and data structures. The management of this private
                          heap is ensured internally by the Python memory manager. The Python
                          memory manager has different components which deal with various
                          dynamic storage management aspects, like sharing, segmentation,
                          preallocation or caching.



                          At the lowest level, a raw memory allocator ensures that there is
                          enough room in the private heap for storing all Python-related data by
                          interacting with the memory manager of the operating system. On top of
                          the raw memory allocator, several object-specific allocators operate
                          on the same heap and implement distinct memory management policies
                          adapted to the peculiarities of every object type. For example,
                          integer objects are managed differently within the heap than strings,
                          tuples or dictionaries because integers imply different storage
                          requirements and speed/space tradeoffs. The Python memory manager thus
                          delegates some of the work to the object-specific allocators, but
                          ensures that the latter operate within the bounds of the private heap.




                          Note, everything in Python is an object. The only thing going onto the interpreter stack is a PyObject pointer. The stack here being an implementation detail of the CPython runtime. Source code is compiled to bytecode, which is executed on this stack-based virtual machine.






                          share|improve this answer













                          This answer applies to CPython specifically, but all CPython objects live on a private heap.




                          Memory management in Python involves a private heap containing all
                          Python objects and data structures. The management of this private
                          heap is ensured internally by the Python memory manager. The Python
                          memory manager has different components which deal with various
                          dynamic storage management aspects, like sharing, segmentation,
                          preallocation or caching.



                          At the lowest level, a raw memory allocator ensures that there is
                          enough room in the private heap for storing all Python-related data by
                          interacting with the memory manager of the operating system. On top of
                          the raw memory allocator, several object-specific allocators operate
                          on the same heap and implement distinct memory management policies
                          adapted to the peculiarities of every object type. For example,
                          integer objects are managed differently within the heap than strings,
                          tuples or dictionaries because integers imply different storage
                          requirements and speed/space tradeoffs. The Python memory manager thus
                          delegates some of the work to the object-specific allocators, but
                          ensures that the latter operate within the bounds of the private heap.




                          Note, everything in Python is an object. The only thing going onto the interpreter stack is a PyObject pointer. The stack here being an implementation detail of the CPython runtime. Source code is compiled to bytecode, which is executed on this stack-based virtual machine.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 18 '18 at 20:28









                          juanpa.arrivillagajuanpa.arrivillaga

                          38k33774




                          38k33774






























                              draft saved

                              draft discarded




















































                              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%2f53364821%2fhow-does-the-pythons-stack-work-when-dealing-with-tuples-lists%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







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini