如何在python中创建和销毁变量?

问题描述 投票:0回答:2

如果我们创建2个变量a,然后是b,那么这些变量如何在python堆中销毁。我们想了解由python解释器完成的内存管理。

python memory-management interpreter
2个回答
1
投票

简单地说,Python内存管理器对对现有变量的引用计数,如果引用计数等于零,则垃圾收集器会自动为该变量分配空间。

我建议使用以下资源以更好地了解流程:

  1. https://realpython.com/python-memory-management/
  2. https://docs.python.org/3/c-api/memory.html

0
投票

订单未定义,因为python具有垃圾回收器。要跟踪顺序,您可以添加__del__方法,该方法在Python中称为析构函数方法。当没有对对象的引用并且对象被垃圾回收时,将调用它。

请参见What is the __del__ method, How to call it?

© www.soinside.com 2019 - 2024. All rights reserved.