Python中的類學(xué)習(xí)筆記
Python使用中面向?qū)ο蟮恼Z言,支持繼承、多態(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ì)象,直覺上,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
可見,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
訪問成員都要通過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);
訪問控制
Python并不直接支持私有方訪問,而是要靠程序員自己把握。
不過,可以在屬性名稱前加上雙下劃線而給其私有訪問能力(對(duì)外不可見);
>>> 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ì)象的屬性上,這說明類屬性和對(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)文章
Python編程快速上手——Excel表格創(chuàng)建乘法表案例分析
這篇文章主要介紹了Python Excel表格創(chuàng)建乘法表,結(jié)合具體實(shí)例形式分析了Python接受cmd命令操作Excel文件創(chuàng)建乘法表相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-02-02
Python生成requirements.txt的兩種最新方法
在Python項(xiàng)目開發(fā)中requirements.txt文件扮演著至關(guān)重要的角色,它記錄了項(xiàng)目所需的所有依賴包及其精確版本號(hào),這篇文章主要介紹了Python生成requirements.txt的兩種最新方法,需要的朋友可以參考下2025-04-04
利用python將xml文件解析成html文件的實(shí)現(xiàn)方法
下面小編就為大家分享一篇利用python將xml文件解析成html文件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
Python中enumerate()函數(shù)詳細(xì)分析(附多個(gè)Demo)
Python的enumerate()函數(shù)是一個(gè)內(nèi)置函數(shù),主要用于在遍歷循環(huán)中獲取每個(gè)元素的索引以及對(duì)應(yīng)的值,這篇文章主要介紹了Python中enumerate()函數(shù)的相關(guān)資料,需要的朋友可以參考下2024-10-10
python錯(cuò)誤調(diào)試及單元文檔測(cè)試過程解析
這篇文章主要介紹了python錯(cuò)誤調(diào)試及單元文檔測(cè)試過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
使用Python實(shí)現(xiàn)計(jì)算DICOM圖像兩點(diǎn)真實(shí)距離
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)計(jì)算DICOM圖像兩點(diǎn)真實(shí)距離,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
python編程webpy框架模板之def with學(xué)習(xí)
這篇文章主要為大家介紹了python編程web.py框架模板之def with的學(xué)習(xí)有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11

