[Tutor] Python and memory allocation

Dave Angel davea at davea.name
Thu Oct 24 19:21:56 CEST 2013


On 24/10/2013 12:38, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote:

> Hello All,
>
>     Before starting to learn python, I first learnt C and C++ for a couple of years. In C/C++, the choice to assign memory during compile time or during execution time, i.e. assigning memory from the stack or the heap lay with the programmer right? But in Python, I have only seen examples of using heap memory in programs. Like list.append(x) and so on. So, is there a concept of heap and stack in python or is it abstracted? And does python being an interpreted language have to do anything with this?
>
>
>
> Thanks and sorry for the newbie doubt :)
>

That's what this group is for, welcome.

In C and C++, there are 3 kinds of variable lifetime, rather than 2.  If
a variable is static, it is created and initialized before your program
really starts, and lasts till the program goes away.

If a variable is automatic (stack-based), it will be created when the
function (or even the scope within a function) begins, and destroyed
when the function or scope ends.  And this is especially important in
C/C++ because when a function is recursive, there may be many named
variables on the stack with the same name, and independent values.

If a data item is heap-based, it is not really a variable at all, but is
a block created by malloc() or equivalent, and *pointed to* by a
variable.  And it's freed when the corresponding free() or equivalent is
called.

In Python it's all different.  There aren't really any static variables
that the user defines, though there are plenty of variables that are
defined and initialized long before your code starts running.  If you
want a surprise, try the following simple program some time.

import sys
print(sys.modules)

when I tried that interactively on 2.7, it printed some 240+ names.

As for auto versus heap-based, all values you define are both.  What's
stored in the 'stack' is references to objects, where the objects are
allocated on the heap.  And those in turn may have references to other
objects, and so on.

So if you have a variable x which is a list of size 10, then you have a
refernce to a list object, which in turn has references to ten other
objects.

All these objects are reference counted, and when the count reaches
zero, they vanish.  And for circular links, there's a garbage collector
which operates periodically.

These references are referred to as bindings in the python literature.

The real point is that when you use a value, you don't have to worry
about where it'll be stored, but just on how you'll find it again when
you need it.  You may have a name bound to it, and also a list item. 
Even if you reuse the name, the list item will still assure you can find
it again.  Similarly, if you call sorted() on a list of large strings,
you get a new list, but the strings are not duplicated, so it's not
nearly the duplication it might look like.

--
DaveA




More information about the Tutor mailing list