python creates everything from heap?

前端 未结 3 1413
孤街浪徒
孤街浪徒 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 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.

提交回复
热议问题