Python 類的特殊成員解析
類的成員有兩種形式
公有成員,在任何地方都能訪問
私有成員,只有在類的內部才能方法,私有成員命名時,前兩個字符是下劃線。
class Foo: def __init__(self, name, age): self.name = name self.__age = age def show(self): # 間接方法私有字段 return self.__age obj = Foo('klvchen', 25) print(obj.name) res = obj.show() print(res)
運行結果:
klvchen
25
公有靜態(tài)字段:類可以訪問;類內部可以訪問;派生類中可以訪問
私有靜態(tài)字段:僅類內部可以訪問;
class Foo: __v = '666' # 私有靜態(tài)字段 def __init__(self): pass def show(self): return Foo.__v obj = Foo() res = obj.show() print(res)
運行結果:
666
class Foo: __v = '666' def __init__(self): pass def show(self): return Foo.__v @staticmethod def stat(): return Foo.__v res = Foo.stat() print(res)
運行結果:
666
無法從父類繼承私有字段
class F: def __init__(self): self.ge = 123 self.__gene = 456 #私有字段 class S(F): def __init__(self, name): self.name = name self.__age = 18 super(S, self).__init__() def show(self): print(self.name) print(self.__age) print(self.ge) print(self.__gene) s = S('klvchen') s.show()
運行結果:
klvchen
18
123
AttributeError: 'S' object has no attribute '_S__gene'
類的特殊成員
int(對象),會自動執(zhí)行對象中的__int__方法,并將返回賦值給 int 對象,同理 str(對象),會自動執(zhí)行__str__方法,并返回賦值給 str 對象。
class Foo: def __init__(self): pass def __int__(self): return 666 def __str__(self): return 'hello world' obj = Foo() print(obj, type(obj)) res = int(obj) print(res) res1 = str(obj) print(res1)
運行結果:
<__main__.Foo object at 0x0000022BBE9DA978> <class '__main__.Foo'>
666
hello world
print(對象),str(對象),都會自動執(zhí)行對象中的__str__方法,并返回
class Foo: def __init__(self, n, a): self.name = n self.age = a def __str__(self): return '%s-%d' %(self.name, self.age) obj = Foo('klvchen', 28) print(obj)
運行結果:
klvchen-28
兩個對象相加時,自動執(zhí)行第一對象的__add__方法,并且將第二個對象當參數傳遞進去
class Foo: def __init__(self, name, age): self.name = name self.age = age def __add__(self, other): return self.age + other.age obj1 = Foo('klv1', 23) obj2 = Foo('klv2', 24) res = obj1 + obj2 print(res, type(res))
運行結果:
47 <class 'int'>
class Foo:
def __init__(self, name, age):
self.name = name
self.age = age
def __add__(self, other):
return Foo(obj1.name, obj2.age)
def __del__(self):
print('析構方法')
obj1 = Foo('klv1', 23)
obj2 = Foo('klv2', 24)
res = obj1 + obj2
print(res, type(res))
運行結果:
<__main__.Foo object at 0x0000016DFCE125C0> <class '__main__.Foo'>
析構方法
析構方法
析構方法
li[對象] 會自動執(zhí)行 li 對象的類中的__getitem__方法,8當作參數傳遞給item
class Foo: def __init__(self, name, age): self.name = name self.age = age def __getitem__(self, item): return item li = Foo('klvchen', 28) r = li[8] print(r)
運行結果:
8
類中的__setitem__,__delitem__方法
class Foo: def __init__(self, name, age): self.name = name self.age = age def __getitem__(self, item): return item def __setitem__(self, key, value): print(key, value) def __delitem__(self, key): print(key) li = Foo('klvchen', 28) r = li[8] print(r) li[100] = 'hello' del li[999]
運行結果:
8
100 hello
999
執(zhí)行類中的__iter__()方法,并獲取其返回值,循環(huán)上一步中返回對象,用于迭代器,之所以列表、字典、元組可以進行for循環(huán),是因為類型內部定義了iter
class Foo: def __init__(self, name, age): self.name = name self.age = age def __iter__(self): return iter([11, 22, 33]) li = Foo('klvchen', 26) for i in li: print(i)
運行結果:
11
22
33
for 循環(huán)的內部操作
obj = iter([11, 22, 33]) while True: val = obj.next() print val
總結
以上所述是小編給大家介紹的Python 類的特殊成員解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
Python urlencode和unquote函數使用實例解析
這篇文章主要介紹了Python urlencode和unquote函數使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03Python實現遍歷windows所有窗口并輸出窗口標題的方法
這篇文章主要介紹了Python實現遍歷windows所有窗口并輸出窗口標題的方法,涉及Python調用及遍歷windows窗口句柄的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03詳解Python中的函數參數傳遞方法*args與**kwargs
本文將討論Python的函數參數。我們將了解args和kwargs,/和的都是什么,雖然這個問題是一個基本的python問題,但是在我們寫代碼時會經常遇到,比如timm中就大量使用了這樣的參數傳遞方式2023-03-03