內(nèi)置方法 | 說(shuō)明 |
__init__(self,...) | 初始化對(duì)象,在創(chuàng)建新對(duì)象時(shí)調(diào)用 |
__del__(self) | 釋放對(duì)象,在對(duì)象被刪除之前調(diào)用 |
__new__(cls,*args,**kwd) | 實(shí)例的生成操作 |
__str__(self) | 在使用PRint語(yǔ)句時(shí)被調(diào)用 |
__getitem__(self,key) | 獲取序列的索引key對(duì)應(yīng)的值,等價(jià)于seq[key] |
__len__(self) | 在調(diào)用內(nèi)聯(lián)函數(shù)len()時(shí)被調(diào)用 |
__cmp__(stc,dst) | 比較兩個(gè)對(duì)象src和dst |
__getattr__(s,name) | 獲取屬性的值 |
__setattr__(s,name,value) | 設(shè)置屬性的值 |
__delattr__(s,name) | 刪除name屬性 |
__getattribute__() | __getattribute__()功能與__getattr__()類(lèi)似 |
__gt__(self,other) | 判斷self對(duì)象是否大于other對(duì)象 |
__lt__(slef,other) | 判斷self對(duì)象是否小于other對(duì)象 |
__ge__(slef,other) | 判斷self對(duì)象是否大于或者等于other對(duì)象 |
__le__(slef,other) | 判斷self對(duì)象是否小于或者等于other對(duì)象 |
__eq__(slef,other) | 判斷self對(duì)象是否等于other對(duì)象 |
__call__(self,*args) | 把實(shí)例對(duì)象作為函數(shù)調(diào)用 |
__init__():__init__方法在類(lèi)的一個(gè)對(duì)象被建立時(shí),馬上運(yùn)行。這個(gè)方法可以用來(lái)對(duì)你的對(duì)象做一些你希望的初始化。注意,這個(gè)名稱(chēng)的開(kāi)始和結(jié)尾都是雙下劃線(xiàn)。代碼例子:
#!/usr/bin/python# Filename: class_init.pyclass Person: def __init__(self, name): self.name = name def sayHi(self): print 'Hello, my name is', self.name p = Person('Swaroop')p.sayHi() 輸出:Hello, my name is Swaroop |
__new__():__new__()在__init__()之前被調(diào)用,用于生成實(shí)例對(duì)象.利用這個(gè)方法和類(lèi)屬性的特性可以實(shí)現(xiàn)設(shè)計(jì)模式中的單例模式.單例模式是指創(chuàng)建唯一對(duì)象嗎,單例模式設(shè)計(jì)的類(lèi)只能實(shí)例化一個(gè)對(duì)象.
#!/usr/bin/python# -*- coding: UTF-8 -*- class Singleton(object): __instance = None # 定義實(shí)例 def __init__(self): pass def __new__(cls, *args, **kwd): # 在__init__之前調(diào)用 if Singleton.__instance is None: # 生成唯一實(shí)例 Singleton.__instance = object.__new__(cls, *args, **kwd) return Singleton.__instance |
__getattr__()、__setattr__()和__getattribute__():當(dāng)讀取對(duì)象的某個(gè)屬性時(shí),python會(huì)自動(dòng)調(diào)用__getattr__()方法.例如,fruit.color將轉(zhuǎn)換為fruit.__getattr__(color).當(dāng)使用賦值語(yǔ)句對(duì)屬性進(jìn)行設(shè)置時(shí),python會(huì)自動(dòng)調(diào)用__setattr__()方法.__getattribute__()的功能與__getattr__()類(lèi)似,用于獲取屬性的值.但是__getattribute__()能提供更好的控制,代碼更健壯.注意,python中并不存在__setattribute__()方法.代碼例子:
#!/usr/bin/python# -*- coding: UTF-8 -*- class Fruit(object): def __init__(self, color = "red", price = 0): self.__color = color self.__price = price def __getattribute__(self, name): # 獲取屬性的方法 return object.__getattribute__(self, name) def __setattr__(self, name, value): self.__dict__[name] = value if __name__ == "__main__": fruit = Fruit("blue", 10) print fruit.__dict__.get("_Fruit__color") # 獲取color屬性 fruit.__dict__["_Fruit__price"] = 5 print fruit.__dict__.get("_Fruit__price") # 獲取price屬性 |
__getitem__():如果類(lèi)把某個(gè)屬性定義為序列,可以使用__getitem__()輸出序列屬性中的某個(gè)元素.假設(shè)水果店中銷(xiāo)售多鐘水果,可以通過(guò)__getitem__()方法獲取水果店中的沒(méi)種水果代碼例子:
#!/usr/bin/python# -*- coding: UTF-8 -*- class FruitShop: def __getitem__(self, i): # 獲取水果店的水果 return self.fruits[i] if __name__ == "__main__": shop = FruitShop() shop.fruits = ["apple", "banana"] print shop[1] for item in shop: # 輸出水果店的水果 print item, 輸出為:bananaapple banana |
__str__():__str__()用于表示對(duì)象代表的含義,返回一個(gè)字符串.實(shí)現(xiàn)了__str__()方法后,可以直接使用print語(yǔ)句輸出對(duì)象,也可以通過(guò)函數(shù)str()觸發(fā)__str__()的執(zhí)行.這樣就把對(duì)象和字符串關(guān)聯(lián)起來(lái),便于某些程序的實(shí)現(xiàn),可以用這個(gè)字符串來(lái)表示某個(gè)類(lèi)代碼例子:
#!/usr/bin/python# -*- coding: UTF-8 -*- class Fruit: '''Fruit類(lèi)''' #為Fruit類(lèi)定義了文檔字符串 def __str__(self): # 定義對(duì)象的字符串表示 return self.__doc__ if __name__ == "__main__": fruit = Fruit() print str(fruit) # 調(diào)用內(nèi)置函數(shù)str()出發(fā)__str__()方法,輸出結(jié)果為:Fruit類(lèi) print fruit #直接輸出對(duì)象fruit,返回__str__()方法的值,輸出結(jié)果為:Fruit類(lèi) |
__call__():在類(lèi)中實(shí)現(xiàn)__call__()方法,可以在對(duì)象創(chuàng)建時(shí)直接返回__call__()的內(nèi)容.使用該方法可以模擬靜態(tài)方法代碼例子:
#!/usr/bin/python# -*- coding: UTF-8 -*- class Fruit: class Growth: # 內(nèi)部類(lèi) def __call__(self): print "grow ..." grow = Growth() # 調(diào)用Growth(),此時(shí)將類(lèi)Growth作為函數(shù)返回,即為外部類(lèi)Fruit定義方法grow(),grow()將執(zhí)行__call__()內(nèi)的代碼if __name__ == '__main__': fruit = Fruit() fruit.grow() # 輸出結(jié)果:grow ... Fruit.grow() # 輸出結(jié)果:grow ... |
轉(zhuǎn)自 http://xukaizijian.blog.163.com/blog/static/170433119201111894228877/
https://docs.python.org/3/library/functions.html#abs
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注