Python中的類學(xué)習(xí)筆記
Python使用中面向?qū)ο蟮恼Z(yǔ)言,支持繼承、多態(tài);
定義一個(gè)Person類:
>>> class Person:
... def sayHello(self):
... print('hello')
...
>>> Person.sayHello(None)
hello
>>> Person().sayHello()
hello
可以修改Person的類方法
>>> def hack_sayHello(obj):
... print('...hello')
...
>>>
>>> Person.sayHello = hack_sayHello
>>> Person.sayHello(None)
...hello
>>> Person().sayHello()
...hello
>>> sayHello = Person().sayHello
>>> sayHello()
...hello
Person().sayHello也是一個(gè)函數(shù),可以賦值給變量,并可以直接調(diào)用;
>>> Person.sayHello is Person().sayHello
False
>>> Person.sayHello == Person().sayHello
False
Person.sayHello與Person().sayhello并不是同一個(gè)對(duì)象,直覺(jué)上,Person().sayHello關(guān)聯(lián)(綁定)了一個(gè)Person實(shí)例,而Person.sayHello是一個(gè)類方法;
self參數(shù)事實(shí)上正是方法和函數(shù)的區(qū)別:方法將它們的第一個(gè)參數(shù)綁定到所屬的實(shí)例上,因此這個(gè)參數(shù)可以不必提供;
>>> class Person:
... name = 'unkown'
... def sayHello(self):
... print('i\'m ' + name)
...
>>>
>>> Person.sayHello(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
NameError: name 'name' is not defined
>>> p = Person()
>>> p.name = 'wyj'
>>> p.sayHello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
NameError: name 'name' is not defined
可見(jiàn),Python在解析變量時(shí),默認(rèn)從local scope/global scope中查找;
>>> class Person:
... name = 'unkown'
... def sayHello(self):
... print('i\'m ' + self.name)
...
>>>
>>> Person.sayHello(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
AttributeError: 'NoneType' object has no attribute 'name'
>>> p = Person()
>>> p.name = 'wyj'
>>> p.sayHello()
i'm wyj
訪問(wèn)成員都要通過(guò)self,假如以包含name屬性的對(duì)象調(diào)用Person.sayHello(obj),是否可以呢?
>>> class Cat:
... name = 'huanhuan'
...
>>> Person.sayHello(Cat())
i'm huanhuan
可以,Python并不限制必須用相同類的實(shí)例對(duì)象作為參數(shù)調(diào)用類方法(貌似Python的類機(jī)制類似Javascript);
訪問(wèn)控制
Python并不直接支持私有方訪問(wèn),而是要靠程序員自己把握。
不過(guò),可以在屬性名稱前加上雙下劃線而給其私有訪問(wèn)能力(對(duì)外不可見(jiàn));
>>> class Person:
... def __private_method(self):
... print('private')
... def test(self):
... self.__private_method()
...
>>> Person().test()
private
>>> Person().__private_method()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute '__private_method'
實(shí)際上,以上下劃線打頭的方法都有一個(gè)_ClassName__methodName的方法
>>> Person._Person__private_method
<function Person.__private_method at 0x7fed431a2d90>
調(diào)用
>>> Person._Person__private_method(None)
private
總之,Python并不能阻止從類外進(jìn)行方法調(diào)用;
類屬性以及對(duì)象屬性
首先,可以為類添加屬性,新對(duì)象將得到屬性的一份拷貝
>>> Person.age = 3
>>> Person().age
3
>>> Person.age = 4
>>> Person().age
4
>>> p = Person()
>>> Person.age = 31
>>> p.age
31
對(duì)類屬性的修改,反映到了先前生成的對(duì)象的屬性上,這說(shuō)明類屬性和對(duì)象的屬性共享一個(gè)值;
>>> p.age = 34
>>> p.age
34
>>> Person.age
31
>>> Person.age = 99
>>> p.age
34
而一旦對(duì)對(duì)象的屬性的修改,對(duì)象屬性就擁有了自己的值,并不會(huì)反映到類屬性上,而對(duì)類屬性的修改,也不再反映到該對(duì)象的屬性上;
這種行為與Javascript類似
相關(guān)文章
畫(huà)pytorch模型圖,以及參數(shù)計(jì)算的方法
今天小編就為大家分享一篇畫(huà)pytorch模型圖,以及參數(shù)計(jì)算的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08通過(guò)實(shí)例解析Python RPC實(shí)現(xiàn)原理及方法
這篇文章主要介紹了通過(guò)實(shí)例解析Python RPC實(shí)現(xiàn)原理及方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Python3中的map函數(shù)調(diào)用后內(nèi)存釋放問(wèn)題
這篇文章主要介紹了Python3中的map函數(shù)調(diào)用后內(nèi)存釋放問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Python中Enum使用的幾點(diǎn)注意事項(xiàng)
Python中的枚舉是作為一個(gè)類存在的,這是與其他語(yǔ)言的一個(gè)較為鮮明的特征,下面這篇文章主要給大家介紹了關(guān)于Python中Enum使用的幾點(diǎn)注意事項(xiàng),需要的朋友可以參考下2022-02-02TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例
今天小編就為大家分享一篇TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01python批量實(shí)現(xiàn)Word文件轉(zhuǎn)換為PDF文件
這篇文章主要為大家詳細(xì)介紹了python批量實(shí)現(xiàn)Word文件轉(zhuǎn)換為PDF文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03