python 析構函數:__del__()


'''
析構函數:__del__() 釋放對象是自動調用
'''

class Person(object):
def run(self):
print("run")
def eat(self, food):
print("eat " + food)
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weight = weight
def __del__(self):
print("這里是析構函數")

per = Person("hanmeimei", 20, 170, 55)
#釋放對象
del per

#對象釋放以后就不能再訪問了
#print(per.age)

#在函數里定義的對象,會在函數結束時自動釋放,這樣可以用來減少內存空間的浪費
def func():
per2 = Person("aa", 1, 1, 1)

func()

while 1:
pass


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM