python中文件描述符_Python中的描述符
python中文件描述符
In Python, a class that implements a get, set or delete methods for an object is called descriptors. Descriptors are the way to create attributes and add managed attributes to objects. These are used to protect the attributes from changes and any modifications. Descriptors can able to increase the readability of a program and coding skills. They can help to validate the data.
在Python中,實現對象的get,set或delete方法的類稱為描述符 。 描述符是創建屬性并將托管屬性添加到對象的方法。 這些用于保護屬性免受更改和任何修改。 描述符可以提高程序的可讀性和編碼技巧。 他們可以幫助驗證數據。
For example, we need only positive integer values for attribute age and string values for attribute string then descriptors provides an efficient solution.
例如 ,對于屬性年齡,我們只需要正整數值,對于屬性字符串,我們只需要字符串值即可,然后描述符提供了有效的解決方案。
To create a descriptor, we need __get__, __set__ and __delete__ methods.
要創建描述符 ,我們需要__get__ , __set__和__delete__方法。
描述符協議 (Descriptor Protocol)
To create a descriptor, we need a descriptor protocol. By defining the following methods we can efficiently manage the attributes. They are:
要創建描述符 ,我們需要一個描述符協議 。 通過定義以下方法,我們可以有效地管理屬性。 他們是:
__get__(self, obj, type=None)__set__(self, obj, value)__delete__(self, obj)Here,
這里,
__get__ : It gets the value from an object and returns it.
__get__ :它從一個對象獲取值并返回它。
__set__ : It sets a value to the object and returns none.
__set__ :它為對象設置一個值,但不返回任何值。
__delete__ : It deletes the value in the object and return none.
__delete__ :刪除對象中的值,不返回任何值。
These methods are normally referred to as getter and setter methods. Python doesn't provide private variables and by using descriptors we can achieve them. The descriptor with the only __get__ method is called non-data descriptors and these are created only for a class, not for an instance. A class can have other than these methods if necessary.
這些方法通常稱為getter和setter方法 。 Python不提供私有變量,通過使用描述符我們可以實現它們。 具有唯一__get__方法的描述符稱為非數據描述符,它們僅針對類而不是針對實例創建。 如果需要,一個類可以具有這些方法以外的其他方法。
創建和調用描述符 (Creating and calling Descriptors)
We can create a descriptor with many ways:
我們可以通過多種方式創建描述符:
Create a class and override any of the __get__, __set__ and __delete__ methods and use them.
創建一個類并重寫__get__ , __set__和__delete__方法中的任何一個并使用它們。
We can use the property type to create descriptor.
我們可以使用屬性類型來創建描述符。
We can create descriptors by combining both property type and python decorators.
我們可以通過組合屬性類型和python裝飾器來創建描述符。
Let look over each way of creating descriptor.
讓我們來看看創建描述符的每種方式。
使用類創建描述符 (Creating descriptors using class)
class rk:def __init__(self):self.value=0def __get__(self,ob, ty):print ("get method") return self.valuedef __set__(self, ob, ty):self.value = typrint("set method",self.value)def __delete__(self, ob): print("delete method")del self.valueclass inc:r=rk()a=inc() print(a.r) a.r=3 del a.rOutput
輸出量
get method 0 set method 3 delete methodIn the above program, the get method will __get__ the value, the __set__ method will set the value to attribute and __delete__ method will delete the attribute.
在上面的程序中,get方法將__get__值, __set__方法將值設置為attribute, __delete__方法將刪除屬性。
使用屬性類型創建描述符 (Creating descriptor using property type)
By using property() it is easy to create descriptors.
通過使用property() ,很容易創建描述符。
Syntax for creating a property method is:
創建屬性方法的語法為:
property(fget=None, fset=None, fdel=None, doc=None)Here,
這里,
fget : Function to be used for getting an attribute value
fget :用于獲取屬性值的函數
fset : Function to be used for setting an attribute value
fset :用于設置屬性值的函數
fdel : Function to be used for deleting an attribute
fdel :用于刪除屬性的函數
doc : docstring
doc :文檔字符串
Now, the same program can be written using property type,
現在,可以使用屬性類型編寫相同的程序,
class rk:def __init__(self):self.value=0def fget(self):print("Get method")return self.valuedef fset(self, ty):print ("Set method")self.value = tydef fdel(self):print("delete method")del self.value name = property(fget, fset, fdel, "I'm the property.")r=rk() r.name=1 print(r.name) del r.nameOutput
輸出量
Set method Get method 1 delete method使用屬性類型和裝飾器創建描述符 (Creating descriptors using property type and decorators)
Python decorators are callable objects used to modify functions or classes. By using decorators we can modify the attribute methods.
Python裝飾器是用于修改函數或類的可調用對象。 通過使用裝飾器,我們可以修改屬性方法。
Example to create descriptors using property type and decorators:
使用屬性類型和裝飾器創建描述符的示例:
class rk(object):def __init__(self):self.i=0@propertydef inc(self):print("get method")return self.i@inc.setterdef inc(self, ty):self.i=typrint ("Set method",self.i)@inc.deleterdef inc(self):print ("delete method")del self.ir=rk()print(r.inc) r.inc=3 del r.incOutput
輸出量
get method 0 Set method 3 delete method描述符的優點 (Advantages of descriptors)
Descriptors can increase the readability of a program and it can validate the data based on our requirements. These are low-level features and by using them we can reuse the code.
描述符可以提高程序的可讀性,并且可以根據我們的要求驗證數據。 這些是低級功能,通過使用它們,我們可以重用代碼。
Descriptors create a way to implement private variables and manage the data. We can protect the attributes from any modifications and any updations.
描述符創建了一種實現私有變量和管理數據的方法。 我們可以保護屬性免受任何修改和更新。
翻譯自: https://www.includehelp.com/python/descriptor.aspx
python中文件描述符
總結
以上是生活随笔為你收集整理的python中文件描述符_Python中的描述符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 地下城与勇士几点开始双倍经验?
- 下一篇: python中二进制整数_Python程