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:

我们可以通过多种方式创建描述符:

  1. Create a class and override any of the __get__, __set__ and __delete__ methods and use them.

    创建一个类并重写__get__ , __set__和__delete__方法中的任何一个并使用它们。

  2. We can use the property type to create descriptor.

    我们可以使用属性类型来创建描述符。

  3. 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=0
		
	def __get__(self,ob, ty):
		print ("get method")		
		return self.value
		
	def __set__(self, ob, ty):
		self.value = ty
		print("set method",self.value)
	
	def __delete__(self, ob):	
		print("delete method")
		del self.value

class inc:
	r=rk()
	
a=inc()
print(a.r)
a.r=3
del a.r

Output

输出量

get method
0
set method 3
delete method

In 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=0

	def fget(self):
		print("Get method")
		return self.value

	def fset(self, ty):
		print ("Set method")
		self.value = ty

	def 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.name

Output

输出量

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

	@property
	def inc(self):
		print("get method")
		return self.i

	@inc.setter
	def inc(self, ty):
		self.i=ty
		print ("Set method",self.i)

	@inc.deleter
	def inc(self):
		print ("delete method")
		del self.i

r=rk()

print(r.inc)
r.inc=3
del r.inc

Output

输出量

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中文件描述符

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值