---
title: "python学习： python中的descriptor"
author: "rstevens"
date: "2011-08-31T00:39:11Z"
tags: []
categories: ["编程语言"]
slug: "python-python-descriptor"
draft: false
lastmod: "2011-08-30T01:29:47Z"
id: 1160990
url: "https://rstevens.iteye.com/blog/1160990"
---

descriptor 用与类的属性

descriptor 是一个类，需要提供 __set__, __get__, __del__ 三个方法的实现。

如果一个类的属性是 descriptor，那么：

该属性不会出现在该类实例化后的对象的__dict__中。也就是说，它部作为字典的一部分出现。
访问该属性时，调用descriptor的__get__()方法
设置该属性时，调用descriptor的__set__()方法
删除该属性时，调用descriptor的__del__()方法

也就是说，如果属性是descriptor，则劫持了对字典的访问。

descriptor 的好处是什么？ 
增加了一层以后，可以提供更多的灵活性，感觉