程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python magic method (3):__ del __ (self) method

編輯:Python

Python There are some magical ways to do it , They are always surrounded by double underscores , They are object-oriented Of Python Everything . They are special ways to add magic to your classes , If your object implements ( heavy load ) A magic method , Then this method will be automatically used in special cases Python The call .

function

Destructor , Defines the behavior of an object when it is freed in memory .

Parameters

self Represents the object itself , There can also be other parameters .

Application scenarios

Python Bring your own garbage collection mechanism , But it can only recycle user level resources , The system resources cannot be recycled automatically . So when an object has a user level at the same time (Python Program ) And kernel level ( System ) Two resources , You must recycle system resources while clearing objects ( Such as closing files ), And that's where it comes in __del__ Method .

__del__ Method is a magic method called when an object is recycled by the system , Call the method at the end of the object life cycle call .

Python Use automatic reference counting (ARC) Method to reclaim the space occupied by the object , When a variable in a program references the Python Object time ,Python The object reference count is automatically guaranteed to be 1; When two variables in the program refer to the Python Object time ,Python The object reference count is automatically guaranteed to be 2, And so on , If the reference count of an object becomes 0, The object is no longer referenced by variables in the program , Indicates that the object is no longer needed by the program , therefore Python Will recycle the object . So most of the time , We don't need to manually delete objects that are no longer used ,python Our recycling mechanism will automatically help us do this .

Sample code :

class A(object):
def __init__(self, a):
print('this is A init')
self.a = a
def __del__(self):
print('this is magic method del')
m = A(1)

Running results :

this is A init
this is magic method del
class MyText(object):
def __init__(self, file_name):
self.file_name = file_name
def __del__(self):
self.file_name.close()
del self.file_name
print(' I was recycled ')
sample = MyText(open('b.txt', 'a'))
print('1234', file=sample.file_name)
sample1 = sample
del sample
print('----------')

Execution results :

----------
I was recycled 

1、 Instead of executing on a variable del operation , The object referenced by this variable will be recycled , Only when the reference count of an object becomes 0 when , The object will be recycled .
 2、 If the parent class provides __del__ Method , Subclass override __del__ Method must explicitly call the __del__ Method , In this way, we can ensure that some properties of the parent instance can be recycled reasonably .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved