描述符應用

2022-09-04 00:06:22 字數 2673 閱讀 4396

class type:

def __init__(self,key):

self.key = key

def __get__(self, instance, owner):

print("get方法")

return instance.__dict__[self.key]

def __set__(self, instance, value):

print("set方法")

if not isinstance(value,str):

return "你傳入的不是字串"

instance.__dict__[self.key] = value

def __delete__(self, instance):

print("正在執行delete")

instance.__dict__.pop(self.key)

class people:

name = type('name') #這樣傳入引數後,就可以讓描述符去**別的屬性

print("get方法")

return instance.__dict__[self.key]

def __set__(self, instance, value):

print("set方法")

if not isinstance(value,self.exp_type):

raise typeerror("你傳入的型別不是",self.exp_type) #高階玩法,主動引出錯誤

instance.__dict__[self.key] = value

def __delete__(self, instance):

print("正在執行delete")

instance.__dict__.pop(self.key)

class people:

name = type('name',str) #這樣傳入引數後,就可以讓描述符去**別的屬性

print("get方法")

return instance.__dict__[self.key]

def __set__(self, instance, value):

print("set方法")

if not isinstance(value,self.exp_type):

raise typeerror("你傳入的型別不是",self.exp_type)

instance.__dict__[self.key] = value

def __delete__(self, instance):

print("正在執行delete")

instance.__dict__.pop(self.key)

class people:

name = type('name',str) #這樣傳入引數後,就可以讓描述符去**別的屬性

mysql 檔案描述符 檔案描述符

toc 首先,linux的世界裡一切皆為檔案,無論是裝置還是乙個socket連線。檔案又可分為 普通檔案 目錄檔案 鏈結檔案和裝置檔案。檔案描述符 file descriptor 是核心為了高效管理已被開啟的檔案所建立的索引,其是乙個非負整數 通常是小整數 用於指代被開啟的檔案,所有執行i o操作的...

python 描述 python描述符

在python中,訪問乙個屬性的優先順序順序按照如下順序 1.類屬性2.資料描述符3.例項屬性4.非資料描述符5.getattr 方法。描述符,用一句話來說,就是將某種特殊型別的類的例項指派給另乙個類的屬性 注意 這裡是類屬性,而不是物件屬性 而這種特殊型別的類就是實現了 get set delet...

檔案描述符

檔案描述符 是個很小的正整數,它是乙個索引值,指向核心為每乙個程序所維護的該程序開啟檔案的記錄表。檔案描述符的優點 相容posix標準,許多 linux 和unix 系統呼叫都依賴於它。檔案描述符的缺點 不能移植到unix以外的系統上去,也不直觀。基於檔案描述符的輸入輸出函式 open 開啟乙個檔案...