python creates everything from heap?

前端 未结 3 1412
孤街浪徒
孤街浪徒 2021-02-08 10:59

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 ex

3条回答
  •  别跟我提以往
    2021-02-08 11:05

    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.

提交回复
热议问题