Managing property access using the Python descriptor

Source: Internet
Author: User
Keywords PYTHON
Tags access added aliyun automatically update class create html http

Python 2.2 introduced the Python descriptor and introduced some new style classes, but they were not widely used. The Python descriptor is a way to create managed properties. In addition to other benefits, managed properties are also used to protect properties from modification, or to http://www.aliyun.com/zixun/aggregation/18862.html ">" to automatically update the value of a dependent property.

Descriptors add to the understanding of Python and improve coding skills. This article describes the descriptor Protocol and demonstrates how to create and use descriptors.

Descriptor protocol

The Python descriptor protocol is simply a way to specify the event that will occur when a property is referenced in the model. It allows programmers to manage property access easily and efficiently:

Set get Delete

In other programming languages, descriptors are called setters and getter, while public functions are used to obtain (get) and set (set) a private variable. Python has no concept of private variables, and descriptor protocols can be used as a Python way to implement functions similar to private variables.

In general, a descriptor is a property of an object that has a binding behavior, and its property access is overwritten by the method in the descriptor protocol. These methods are __get__, __set__ and __delete__. If any of these methods are defined for an object, it is considered a descriptor. Learn more about these methods in Listing 1.

Listing 1. Descriptor methods

__get__ (self, instance, owner) __set__ (self, instance, value) __delete__ (self, instance)

which:

__GET__ is used to access properties. It returns the value of the property, or a Attributeerror exception occurs if the requested property does not exist.

__SET__ will be called in the property allocation operation. No content will be returned.

__delete__ controls the deletion operation. Content will not be returned.

Note that the descriptor is assigned to a class, not an instance. Modifying this class overrides or deletes the descriptor itself, rather than the code that triggers it.

Situations where you need to use a descriptor

Consider the email attribute. Before assigning a value to this property, you need to verify the message format. The descriptor allows an e-mail message to be processed through a regular expression, and then the format is checked and assigned to an attribute.

In many other cases, the Python protocol descriptor Controls access to properties, such as protecting the Name property.

Create a descriptor

You can create a descriptor in many ways:

Create a class and overwrite any one of the descriptor methods: __set__, __ get__, and __delete__. Use this method when a descriptor is required to span several different classes and attributes, such as type validation. Using attribute types, this method makes it easier and more flexible to create a descriptor. The property descriptor is used, which combines the property type method with the Python descriptor.

The following examples are similar in terms of their operation. The difference lies in the way it is implemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.