Python中type與Object的區別

來源:互聯網
上載者:User

標籤:

Python中type與Object的區別在查看了Python的API後,總算明白了。現在總結如下:

    先來看object的說明:

    Python中關於object的說明很少,甚至只有一句話:

  1. class object   
  2.     The most base type   

 

 

    從介紹上看這也是Python對類型統一做出的努力。所以這裡的object與Java的Object類有著異曲同工之妙,而且可以推測這個object很可能就是一個定義了一個類型的"空類"

    再來看type的說明:

  1. class type(object)   
  2.     type(object) -> the object‘s type  
  3. type(name, bases, dict) -> a new type  
  4.     
  5.   Methods defined here:  
  6.   
  7. __call__(...)  
  8. x.__call__(...) <==> x(...)  
  9. __cmp__(...)  
  10. x.__cmp__(y) <==> cmp(x,y)  
  11. __delattr__(...)  
  12. x.__delattr__(‘name‘) <==> del x.name  
  13. __getattribute__(...)  
  14. x.__getattribute__(‘name‘) <==> x.name  
  15. __hash__(...)  
  16. x.__hash__() <==> hash(x)  
  17. __repr__(...)  
  18. x.__repr__() <==> repr(x)  
  19. __setattr__(...)  
  20. x.__setattr__(‘name‘, value) <==> x.name = value  
  21. __subclasses__(...)  
  22. __subclasses__() -> list of immediate subclasses  
  23. mro(...)  
  24. mro() -> list  
  25. return a type‘s method resolution order  
  26.    

      自從大家應該明白了type和object的區別吧。如果大家平時在定義類的時候,想儘可能的偷懶,可以直接從type繼承,否則還是建議繼承object吧

     另外貼上實際啟動並執行區別吧:

    
  




 

 

Python中type與Object的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.