本文目錄一覽:
python類中屬性方法的事件
1普通方法:直接用self調用的方法。
2私有方法:__函數名,只能在類中被調用的方法。
3屬性方法:@property,將方法偽裝成為屬性,讓代碼看起來更合理。
4特殊方法(雙下劃線方法):以__init__為例,是用來封裝實例化對象的屬性,只要是實例化對象就一定會執行__init方法,如果對象子類中沒有則會尋找父類(超類),如果父類(超類)也沒有,則直接繼承object(python 3.x)類,執行類中的__init__方法。
5類方法:通過類名的調用去操作公共模板中的屬性和方法。
6靜態方法:不用傳入類空間、對象的方法, 作用是保證代碼的一致性,規範性,可以完全獨立類外的一個方法,但是為了代碼的一致性統一的放到某個模塊(py文件)中。
python類中的 方法 屬性分別什麼意思
很抽象 沒辦法回答
屬性:就是類對象的屬性,存儲某個值 這個值的代號可以稱為類的屬性
方法:把每一項類中所有的功能封裝起來稱為方法,一般方法里的內容就是方法的執行過程。
舉例: 比如類表示的是People 也就是人
人的類中會有一些屬性 這些屬性大概是 身高、體重、姓名等等
那麼方法比如是 跑、吃、等等
Class People():
def __init__(self, name):
self.name = name
self.height = 0
# 比如說跑了一次之後 身高就增長了1個單位
def run():
print “開始奔跑”
self.height += 1
print “奔跑結束”
上面的例子 run就是方法 ;name 和 height就是屬性
run的方法執行過程就是跑完之後 height就加1
不知道這樣講你能明白嗎
python類的屬性有哪幾種?如何訪問它們?
屬性的訪問機制
一般情況下,屬性訪問的默認行為是從對象的字典中獲取,併當獲取不到時會沿著一定的查找鏈進行查找。例如 a.x 的查找鏈就是,從 a.__dict__[‘x’] ,然後是 type(a).__dict__[‘x’] ,再通過 type(a) 的基類開始查找。
若查找鏈都獲取不到屬性,則拋出 AttributeError 異常。
一、__getattr__ 方法
這個方法是當對象的屬性不存在是調用。如果通過正常的機制能找到對象屬性的話,不會調用 __getattr__ 方法。
class A:
a = 1
def __getattr__(self, item):
print(‘__getattr__ call’)
return item
t = A()
print(t.a)
print(t.b)
# output
1
__getattr__ call
b
二、__getattribute__ 方法
這個方法會被無條件調用。不管屬性存不存在。如果類中還定義了 __getattr__ ,則不會調用 __getattr__()方法,除非在 __getattribute__ 方法中顯示調用__getattr__() 或者拋出了 AttributeError 。
class A:
a = 1
def __getattribute__(self, item):
print(‘__getattribute__ call’)
raise AttributeError
def __getattr__(self, item):
print(‘__getattr__ call’)
return item
t = A()
print(t.a)
print(t.b)
所以一般情況下,為了保留 __getattr__ 的作用,__getattribute__() 方法中一般返回父類的同名方法:
def __getattribute__(self, item):
return object.__getattribute__(self, item)
使用基類的方法來獲取屬性能避免在方法中出現無限遞歸的情況。
三、__get__ 方法
這個方法比較簡單說明,它與前面的關係不大。
如果一個類中定義了 __get__(), __set__() 或 __delete__() 中的任何方法。則這個類的對象稱為描述符。
class Descri(object):
def __get__(self, obj, type=None):
print(“call get”)
def __set__(self, obj, value):
print(“call set”)
class A(object):
x = Descri()
a = A()
a.__dict__[‘x’] = 1 # 不會調用 __get__
a.x # 調用 __get__
如果查找的屬性是在描述符對象中,則這個描述符會覆蓋上文說的屬性訪問機制,體現在查找鏈的不同,而這個行文也會因為調用的不同而稍有不一樣:
如果調用是對象實例(題目中的調用方式),a.x 則轉換為調用:。type(a).__dict__[‘x’].__get__(a, type(a))
如果調用的是類屬性, A.x 則轉換為:A.__dict__[‘x’].__get__(None, A)
其他情況見文末參考資料的文檔
四、__getitem__ 方法
這個調用也屬於無條件調用,這點與 __getattribute__ 一致。區別在於 __getitem__ 讓類實例允許 [] 運算,可以這樣理解:
__getattribute__適用於所有.運算符;
__getitem__適用於所有 [] 運算符。
class A(object):
a = 1
def __getitem__(self, item):
print(‘__getitem__ call’)
return item
t = A()
print(t[‘a’])
print(t[‘b’])
如果僅僅想要對象能夠通過 [] 獲取對象屬性可以簡單的:
def __getitem(self, item):
return object.__getattribute__(self, item)
總結
當這幾個方法同時出現可能就會擾亂你了。我在網上看到一份示例還不錯,稍微改了下:
class C(object):
a = ‘abc’
def __getattribute__(self, *args, **kwargs):
print(“__getattribute__() is called”)
return object.__getattribute__(self, *args, **kwargs)
# return “haha”
def __getattr__(self, name):
print(“__getattr__() is called “)
return name + ” from getattr”
def __get__(self, instance, owner):
print(“__get__() is called”, instance, owner)
return self
def __getitem__(self, item):
print(‘__getitem__ call’)
return object.__getattribute__(self, item)
def foo(self, x):
print(x)
class C2(object):
d = C()
if __name__ == ‘__main__’:
c = C()
c2 = C2()
print(c.a)
print(c.zzzzzzzz)
c2.d
print(c2.d.a)
print(c[‘a’])
可以結合輸出慢慢理解,這裡還沒涉及繼承關係呢。總之,每個以 __get 為前綴的方法都是獲取對象內部數據的鉤子,名稱不一樣,用途也存在較大的差異,只有在實踐中理解它們,才能真正掌握它們的用法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/247042.html