新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:python析構(gòu)函數(shù)如何使用
1、說(shuō)明

類(lèi)中可以定義【__del__】方法,稱(chēng)為析構(gòu)函數(shù)
2、作用
銷(xiāo)毀類(lèi)的實(shí)例的時(shí)候調(diào)用,以釋放占用的資源,其中就放些清理資源的代碼,比如釋放連接
注意這個(gè)方法不能引起對(duì)象的真正銷(xiāo)毀,只是對(duì)象的銷(xiāo)毀時(shí)候會(huì)自動(dòng)調(diào)用它
使用del語(yǔ)句刪除實(shí)例,引用計(jì)數(shù)減1,當(dāng)引用技術(shù)為0時(shí),會(huì)自動(dòng)調(diào)用【__del__】方法
3、實(shí)例
class Myclass:
def __init__(self,name,age =18):
self.name = name
self.__age = age
def getage(self):
return self.__age
def set_age(self,age):
self.__age =age
def del_age(self):
#del self.__age
print("del")
def __del__(self):
print("我走了")
age = property(lambda self :self.__age,set_age)
a = Myclass("tom")
print(a.age) # 18
a.age = 90
print(a.age) # 90以上就是python析構(gòu)函數(shù)的使用,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
網(wǎng)頁(yè)標(biāo)題:創(chuàng)新互聯(lián)Python教程:python析構(gòu)函數(shù)如何使用
本文地址:http://www.5511xx.com/article/ccehiss.html


咨詢(xún)
建站咨詢(xún)
