13

in c/c++, you have variables in stack when you create a local variable inside a function.

http://effbot.org/zone/call-by-object.htm

CLU objects exist independently of procedure activations. Space for objects is allocated from a dynamic storage area /.../ In theory, all objects continue to exist forever. In practice, the space used by an object may be reclaimed when the object isno longer accessible to any CLU program.

Does this mean objects in python is created from heap(as in malloc in c/c++)? and the objects are deallocated when there 's no name associated with them?(like smart pointers)?

Example:

def foo(a): 
  result = []
  result.append(a)
  return result

foo("hello")

myList = foo("bye")

So the first result([]) was created in the heap and got deallocated because there's no name associated with it?

eugene
  • 39,839
  • 68
  • 255
  • 489
  • you can also delete things manually using `del` as in `del result` if you think you need to. – Ryan Haining Jul 27 '12 at 13:48
  • 2
    If your motivation for asking this is that you want your function to always use the same list, note that you can define it as `def foo(a, result = [])`, and the same list will be used every time you call the function, as it's created when the function is defined, not when it's run. – Wooble Jul 27 '12 at 14:26

3 Answers3

18

Yes, all Python objects live on the heap (at least on CPython.) They are reference-counted: they are de-allocated when the last reference to the object disappear. (CPython also has a garbage collector to break cycles.)

In CPython your first list disappears as soon as the function returns since you did not bind the return value to a name and the reference count dropped to zero. In other implementation the object may live longer until the garbage-collector kicks in.

Some objects (like open files) have resources attached that are automatically freed when the object is deallocated, but because of the above it is not recommended to rely on this. Resources should be closed explicitly when you are done with them.

Simon Sapin
  • 9,790
  • 3
  • 35
  • 44
7

As a supplement to the other answers, here's one way to track when garbage-collection happens, using the special method __del__:

class Test(object):
    def __init__(self, name):
        self.name = name

    def __del__(self):
        print "deleting {0}".format(self.name)

print "discarded instance creation"
Test("hello")

print "saved instance creation"
myList = Test("bye")

print "program done"

Output:

discarded instance creation
deleting hello
saved instance creation
program done
deleting bye

For more in-depth data, see the gc module.

Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
6

Yes, all values in CPython are allocated on the heap and reference-counted to know when to deallocate them. Unlike in C, there is no way to know in most cases if a value will outlive its function, so the only safe thing to do is to heap-allocate everything.

Certainly you could do some analysis and determine that certain values are never passed to functions and thus couldn't escape, but that's of limited use in Python and the extra overhead probably wouldn't be worth it.

Gabe
  • 84,912
  • 12
  • 139
  • 238