_del_()方法

來源:互聯網
上載者:User

標籤:time   解釋   調用   計數   python   對象   cat   變數   構造方法   

  建立對象後,python解譯器預設調用_init_()方法;

  當刪除一個對象後,python解譯器也會預設調用一個方法,這個方法為 _del_()方法

  

import timeclass Animal(object):    #構造方法      def _init_(self,name):        print("_init_方法被調用")        self._name = name         #方法     def _del_(self):         print("_del_方法被調用")         print("%s對象馬上要被幹掉了。。。"%self._name)#建立對象dog = Animal(“哈皮狗”)#刪除對象del dogcat = Animal(“波斯貓”)cat2 =  catcat3 =  catprint("----馬上  刪除cat對象")del catprint("----馬上  刪除cat2對象")del cat2print("---馬上 刪除cat3對象")del cat3print(“程式2秒後結束”)time.sleep(2)

總結:

  *當有一個班裡儲存了對象的引用時,此對象的引用計數就會增加1

  *當使用del刪除變數指向的對象時,如果對象的引用計數不會1,比如3,那麼此時只會讓這個使用者引用計數減1,即變為2,當再次調用del時,變為1,如果再調用1次del,此時會真的把對象刪除。

_del_()方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.