Python中的類學(xué)習(xí)筆記
Python使用中面向?qū)ο蟮恼Z言,支持繼承、多態(tài);
定義一個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也是一個函數(shù),可以賦值給變量,并可以直接調(diào)用;
>>> Person.sayHello is Person().sayHello
False
>>> Person.sayHello == Person().sayHello
False
Person.sayHello與Person().sayhello并不是同一個對象,直覺上,Person().sayHello關(guān)聯(lián)(綁定)了一個Person實例,而Person.sayHello是一個類方法;
self參數(shù)事實上正是方法和函數(shù)的區(qū)別:方法將它們的第一個參數(shù)綁定到所屬的實例上,因此這個參數(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在解析變量時,默認(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屬性的對象調(diào)用Person.sayHello(obj),是否可以呢?
>>> class Cat:
... name = 'huanhuan'
...
>>> Person.sayHello(Cat())
i'm huanhuan
可以,Python并不限制必須用相同類的實例對象作為參數(shù)調(diào)用類方法(貌似Python的類機制類似Javascript);
訪問控制
Python并不直接支持私有方訪問,而是要靠程序員自己把握。
不過,可以在屬性名稱前加上雙下劃線而給其私有訪問能力(對外不可見);
>>> 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'
實際上,以上下劃線打頭的方法都有一個_ClassName__methodName的方法
>>> Person._Person__private_method
<function Person.__private_method at 0x7fed431a2d90>
調(diào)用
>>> Person._Person__private_method(None)
private
總之,Python并不能阻止從類外進行方法調(diào)用;
類屬性以及對象屬性
首先,可以為類添加屬性,新對象將得到屬性的一份拷貝
>>> Person.age = 3
>>> Person().age
3
>>> Person.age = 4
>>> Person().age
4
>>> p = Person()
>>> Person.age = 31
>>> p.age
31
對類屬性的修改,反映到了先前生成的對象的屬性上,這說明類屬性和對象的屬性共享一個值;
>>> p.age = 34
>>> p.age
34
>>> Person.age
31
>>> Person.age = 99
>>> p.age
34
而一旦對對象的屬性的修改,對象屬性就擁有了自己的值,并不會反映到類屬性上,而對類屬性的修改,也不再反映到該對象的屬性上;
這種行為與Javascript類似
相關(guān)文章
Python3中的map函數(shù)調(diào)用后內(nèi)存釋放問題
這篇文章主要介紹了Python3中的map函數(shù)調(diào)用后內(nèi)存釋放問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02TFRecord格式存儲數(shù)據(jù)與隊列讀取實例
今天小編就為大家分享一篇TFRecord格式存儲數(shù)據(jù)與隊列讀取實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python批量實現(xiàn)Word文件轉(zhuǎn)換為PDF文件
這篇文章主要為大家詳細(xì)介紹了python批量實現(xiàn)Word文件轉(zhuǎn)換為PDF文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03