python creates everything from heap?

前端 未结 3 1410
孤街浪徒
孤街浪徒 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:20

    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.

提交回复
热议问题