python之類的相關知識

來源:互聯網
上載者:User

標籤:物件導向   字典   over   匯入模組   定義   自己   執行個體化   中國人   self   

在Python中聲明函數與聲明類很相似

def functionName(arge):    "函數文檔字串"    #函數體    #聲明類class 類名:    "類的文檔字串"    類體

 經典類:

class Chinese:    ‘這是一個中國人的類‘    pass#用類Chinese執行個體化出一個對象d1d1 = Chinese()print d1

 新式類:

#新式類class Chinese2(object):    pass

 #屬性:類是用來描述一類事物,類的對象指的是這一類事物中的一個個體,是事物就要有屬性,屬性分為
#1,資料屬性,就是變數
#2,函數屬性,就是函數,在物件導向裡通常稱為方法
#注意:類和對象均用點來訪問自己的屬性

 

資料屬性

 

class China:    goverment = "123"print China.goverment

 

 函數屬性

class China:    goverment = "123"    def sui_di(): #函數屬性        print ("共性")    def cha_dui(self):        print "前面"print China.govermentChina.sui_di()

 屬性的調用

__dict__ : 類的屬性(包含一個字典,由類的資料屬性群組成)

__doc__ :類的文檔字串

__name__: 類名

__module__: 類定義所在的模組(類的全名是‘__main__.className‘,如果類位於一個匯入模組mymod中,那麼className.__module__ 等於 mymod)

__bases__ : 類的所有父類構成元素(包含了一個由所有父類組成的元組)

 #類的其他屬性
print China.__name__
print China.__doc__
print China.__bases__
print China.__module__

python之類的相關知識

聯繫我們

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