What is python object destruction? What is the object destruction (garbage collection) mechanism in python?

乌拉乌拉~
Release: 2018-08-20 14:38:40
Original
1900 people have browsed it

In this article, let’s take a look at python object destruction (garbage collection). For those who have just come into contact with the programming language python, they should know less about python object destruction (garbage collection), and I don’t know much about python garbage collection. But it doesn’t matter. In the next article, we will learn about the knowledge of python object destruction (garbage collection) and the garbage collection mechanism in python.

Python Object Destruction (Garbage Collection)

Python uses a simple technique called reference counting to track and recycle garbage.

Internally, Python records how many references each object in use has.

An internal tracking variable, called a reference counter.

When an object is created, a reference count is created. When the object is no longer needed, that is, when the object's reference count becomes 0, it is garbage collected. However, recycling is not "immediate". The interpreter will recycle the memory space occupied by garbage objects at the appropriate time.

a = 40      # 创建对象  <40>
b = a       # 增加引用, <40> 的计数
c = [b]     # 增加引用.  <40> 的计数

del a       # 减少引用 <40> 的计数
b = 100     # 减少引用 <40> 的计数
c[0] = -1   # 减少引用 <40> 的计数
Copy after login

Garbage collection mechanism not only targets objects with a reference count of 0, but can also handle circular references. A circular reference occurs when two objects refer to each other, but no other variable refers to them.

In this case, reference counting alone is not enough. Python's garbage collector is actually a reference counter and a cyclic garbage collector. In addition to reference counting, the garbage collector also looks at objects that have been allocated a large amount (and those that have not been destroyed by reference counting). In this case, the interpreter will pause and try to clean up any unreferenced loops.

Instance analysis

Destructor __del__, __del__ is called when the object is destroyed. When the object is no longer used, the __del__ method runs:

# !/usr/bin/python
# -*- coding: UTF-8 -*-
class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
    def __del__(self):
        class_name = self.__class__.__name__
        print class_name, "销毁"
        
pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3)  # 打印对象的id
del pt1
del pt2
del pt3
Copy after login

The results of the above example are as follows:

3083401324 3083401324 3083401324Point 销毁
Copy after login

(Note: Usually you need to define a class in a separate file,)

The above is all described in this article Content, this article mainly introduces the knowledge of python object destruction (garbage collection). I hope you can use the information to understand what is said above and the examples given. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What is python object destruction? What is the object destruction (garbage collection) mechanism in python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!