Home Database Mysql Tutorial python descriptor descriptor(1)

python descriptor descriptor(1)

Dec 23, 2016 pm 05:37 PM

Python descriptors are a way to create managed properties. Whenever an attribute is queried, an action occurs. This action defaults to get, set or delete. However, sometimes an application may have more requirements and require you to design more complex actions. The best solution is to write a function that performs the desired action, and then specify that it runs when the property is accessed. An object

with this functionality is called a descriptor. Descriptors are the basis for the implementation of Python methods, bound methods, super, PRperty, staticmethod and classmethod.

1. Descriptor protocol

A descriptor is an object that represents an attribute value. By implementing one or more __get__, __set__, __delete__ methods, the descriptor can be linked to the attribute access mechanism, and these can also be customized. operate.

__get__(self, instance, own): used to access attributes and return the value of the attribute. Instance is the instance object using the descriptor, and own is the class to which the instance belongs. When accessing a property through a class, instance is None.

__set__(self,instance,value): Set attribute value.

__delete__(self,instance): Delete attribute value.

2. How to implement descriptors

class Descriptor(object):

def __get__(self, instance, owner):

print 'getting:%s'%self._name
return self._name
def __set__(self, Instance, name):
Print' setting:%s'%name
self._name = name
def __delete __ (self, instance):
Print 'deleting:%s'%seld._name
SS Person (object):
name = Descriptor()


A very simple descriptor object is generated. Now you can read, set and delete the attribute name of a Person object:

>>> p =Person()

>>> p.name='john'

setting:john

>>> p.name
getting:john
'john'
>>> del p. name
deleting:john


Note: Descriptors can only be instantiated at the class level, descriptors cannot be created for each instance by creating descriptor objects in __init__() and other methods.

Property names used by classes with descriptors have higher priority than property names stored on instances. In order for a descriptor to store a value on an instance, the descriptor must pick a name that is different from the name it itself uses.

As in the above example, the name cannot be used when the Person class initialization __init__ function sets attributes for the instance.

data descriptor and none-data descriptor:

If __get__ and __set__ are implemented, it is a data descriptor, if only __get__ is implemented, it is a non-data descriptor. The different effect is that the data descriptor always replaces the attribute implementation in an instance,

and the non-data descriptor does not have a set, when assigning values ​​​​to attributes through the instance, such as the above p.name = 'hello', it does not The __set__ method will be called again, and the instance attribute p.name will be directly set to 'hello'.

Of course, if you only raise AttributeError in __set__, you will still get a non-data descriptor.

Descriptor calling mechanism:

When querying the attribute a.attr of an object, if python finds that attr is a descriptor object, how to read the attribute depends on the object a:

Direct call: The simplest call is to use it directly The code calls the descriptor method, attr.__get__(a)

Instance binding: If a is an instance object, call the method: type(a).__dict__['attr'].__get__(a,type(a))

Class binding: If A is a class object, call the method: A.__dict__['attr'].__get__(None,A)

Super binding: If a is a super instance, then super(B,obj).m () Find B’s base class A by querying obj.__class__.__mro__, and then execute A.__dict__['m'].__get__(obj,obj.__class__)

3. Descriptor that performs attribute type checking

class TypedProperty(object):
def __init__(self,name,attr_type,default=None):
self.name='_'+name
self.type=attr_type
self.default=default if default else attr_type()
def __get__(self,instance,own):
return getattr(instance,self.name,self.default)
def __set__(self,instance,value):
if not isinstance(value,self.type):
raise TypeError,'Must be %s'%self.type
setattr(instance,self.name,value) def __delete__(self,instance):
raise AttributeError('Can not delete attribute')
class Foo(object):
name=TypedProperty('name',str)
num=TypedProperty('num',int,37)


The above descriptor can check the type of the attribute. If the name attribute is not set to str type or num is not If set to int type, an error will be reported:

>>> f.name=21

TypeError: Must be

and deletion of attributes is prohibited:

> >> del f.name

AttributeError: Can not delete attribute

f.name Invisible call type(f).__dict__['name'].__get__(f,Foo), that is, Foo.name.__get__( f,Foo).

The above descriptor is actually stored on the instance. Name is stored on f._name through setattr(f,_name,value), and num is stored on f._num. This is also the reason for underlining,

Otherwise the descriptor The name name will conflict with the instance attribute name, and the descriptor attribute f.name will overwrite the instance attribute f.name.

The above is the content of python descriptor descriptor (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

See all articles