Python 中左值绑定时刻的内存分配类型
Memory allocation types by moment of lvalue binding in Python
我在布置家庭作业时遇到了一些麻烦。我进行了大量研究,但陷入困境。赋值如下:
Develop and implement an example in which you show the different
variable types the assigned language implements, according to their
classification by the moment of binding with the lvalue.
我研究了不同的类型并得出以下结论:
Variables can be divided in 4 types based on the moment of binding with the lvalue.
- Static: Memory is allocated before runtime and the variables last in memory during the entire execution of the program
- Automatic (or semi-static): Memory is allocated when a variable's scope is loaded, and deallocated when leaving the scope.
- Dynamic: Memory is only allocated when needed during execution. (I take this as meaning that instead of allocatin at for example the
function call, it allocates when the actual variable declaration is
run). Memory is deallocated when needed. Pointers are an example I've
found of this.
- Semi-dynamic: I havent found a clear definition of this, but an example I've seen is arrays which size is unknown until runtime.
我在 Pascal 中都有每个示例,但我为该作业指定的语言是 Python。我找不到 Python 使用的分配类型。我已经看到 here Python 使用动态分配,但我不确定这是否真的是动态的(由垃圾收集器管理的手动释放)还是自动的(因为程序员不必手动执行此操作) .我也不确定 class 属性、全局变量和常量之类的东西是否是静态的,或者它们是否与其他变量(automatic/dynamic)的工作方式相同。
任何关于 python 使用的内存类型的见解,甚至回顾我对每种类型的定义都将不胜感激。如前所述,我已经对此进行了研究,但我发现的不是(对我而言)不清楚就是编程特定问题(即我如何在 Python 中手动 allocate/deallocate 内存)而不是哪种类型上面提到 Python 使用。
我做了一些研究,但在 Python 中找不到任何对静态内存分配的引用,或者更准确地说 CPython :它似乎最Python 语言没有指定内存管理的各个方面,但它的实现可以灵活决定。
我相信 CPython 的所有内存管理都是 "dynamic" 因为它的运行时管理私有堆(在需要时)为对象分配内存(正如我在之前的评论,几乎 Python 中的一切都是对象!)
虽然与您的问题没有直接关系,但可以在 CPython 中进行 "typed" 分配,例如使用 typed array type.
现在,PyPy 是另一种野兽。它使用 RPython as a runtime interpreter which seems to do fancier things with regards to memory allocation, like analyzing how user defined classes are used.
现在我不确定这是否归类为自动分配,但这可能是您报告中要讨论的内容!
我在布置家庭作业时遇到了一些麻烦。我进行了大量研究,但陷入困境。赋值如下:
Develop and implement an example in which you show the different variable types the assigned language implements, according to their classification by the moment of binding with the lvalue.
我研究了不同的类型并得出以下结论:
Variables can be divided in 4 types based on the moment of binding with the lvalue.
- Static: Memory is allocated before runtime and the variables last in memory during the entire execution of the program
- Automatic (or semi-static): Memory is allocated when a variable's scope is loaded, and deallocated when leaving the scope.
- Dynamic: Memory is only allocated when needed during execution. (I take this as meaning that instead of allocatin at for example the function call, it allocates when the actual variable declaration is run). Memory is deallocated when needed. Pointers are an example I've found of this.
- Semi-dynamic: I havent found a clear definition of this, but an example I've seen is arrays which size is unknown until runtime.
我在 Pascal 中都有每个示例,但我为该作业指定的语言是 Python。我找不到 Python 使用的分配类型。我已经看到 here Python 使用动态分配,但我不确定这是否真的是动态的(由垃圾收集器管理的手动释放)还是自动的(因为程序员不必手动执行此操作) .我也不确定 class 属性、全局变量和常量之类的东西是否是静态的,或者它们是否与其他变量(automatic/dynamic)的工作方式相同。
任何关于 python 使用的内存类型的见解,甚至回顾我对每种类型的定义都将不胜感激。如前所述,我已经对此进行了研究,但我发现的不是(对我而言)不清楚就是编程特定问题(即我如何在 Python 中手动 allocate/deallocate 内存)而不是哪种类型上面提到 Python 使用。
我做了一些研究,但在 Python 中找不到任何对静态内存分配的引用,或者更准确地说 CPython :它似乎最Python 语言没有指定内存管理的各个方面,但它的实现可以灵活决定。
我相信 CPython 的所有内存管理都是 "dynamic" 因为它的运行时管理私有堆(在需要时)为对象分配内存(正如我在之前的评论,几乎 Python 中的一切都是对象!)
虽然与您的问题没有直接关系,但可以在 CPython 中进行 "typed" 分配,例如使用 typed array type.
现在,PyPy 是另一种野兽。它使用 RPython as a runtime interpreter which seems to do fancier things with regards to memory allocation, like analyzing how user defined classes are used.
现在我不确定这是否归类为自动分配,但这可能是您报告中要讨论的内容!