Python中属性和描述符的正确使用


Posted in Python onAugust 23, 2016

关于@property装饰器

在Python中我们使用@property装饰器来把对函数的调用伪装成对属性的访问。

那么为什么要这样做呢?因为@property让我们将自定义的代码同变量的访问/设定联系在了一起,同时为你的类保持一个简单的访问属性的接口。

举个栗子,假如我们有一个需要表示电影的类:

class Movie(object):
 def __init__(self, title, description, score, ticket):
 self.title = title
 self.description = description
 self.score = scroe
 self.ticket = ticket

你开始在项目的其他地方使用这个类,但是之后你意识到:如果不小心给电影打了负分怎么办?你觉得这是错误的行为,希望Movie类可以阻止这个错误。 你首先想到的办法是将Movie类修改为这样:

class Movie(object):
 def __init__(self, title, description, score, ticket):
 self.title = title
 self.description = description

 self.ticket = ticket
 if score < 0:
  raise ValueError("Negative value not allowed:{}".format(score))
 self.score = scroe

但这行不通。因为其他部分的代码都是直接通过Movie.score来赋值的。这个新修改的类只会在__init__方法中捕获错误的数据,但对于已经存在的类实例就无能为力了。如果有人试着运行m.scrore= -100,那么谁也没法阻止。那该怎么办?

Python的property解决了这个问题。

我们可以这样做

class Movie(object):
 def __init__(self, title, description, score):
 self.title = title
 self.description = description
 self.score = score

 self.ticket = ticket
 
 @property
 def score(self):
 return self.__score
 
 
 @score.setter
 def score(self, score):
 if score < 0:
  raise ValueError("Negative value not allowed:{}".format(score))
 self.__score = score
 
 @score.deleter
 def score(self):
 raise AttributeError("Can not delete score")

这样在任何地方修改score都会检测它是否小于0。

property的不足

对property来说,最大的缺点就是它们不能重复使用。举个例子,假设你想为ticket字段也添加非负检查。

下面是修改过的新类:

class Movie(object):
 def __init__(self, title, description, score, ticket):
 self.title = title
 self.description = description
 self.score = score
 self.ticket = ticket
 
 @property
 def score(self):
 return self.__score
 
 
 @score.setter
 def score(self, score):
 if score < 0:
  raise ValueError("Negative value not allowed:{}".format(score))
 self.__score = score
 
 @score.deleter
 def score(self):
 raise AttributeError("Can not delete score")
 
 
 @property
 def ticket(self):
 return self.__ticket
 
 @ticket.setter
 def ticket(self, ticket):
 if ticket < 0:
  raise ValueError("Negative value not allowed:{}".format(ticket))
 self.__ticket = ticket
 
 
 @ticket.deleter
 def ticket(self):
 raise AttributeError("Can not delete ticket")

可以看到代码增加了不少,但重复的逻辑也出现了不少。虽然property可以让类从外部看起来接口整洁漂亮,但是却做不到内部同样整洁漂亮。

描述符登场

什么是描述符?

一般来说,描述符是一个具有绑定行为的对象属性,其属性的访问被描述符协议方法覆写。这些方法是__get__() __set__()__delete__() ,一个对象中只要包含了这三个方法中的至少一个就称它为描述符。

描述符有什么作用?

The default behavior for attribute access is to get, set, or delete the attribute from an object's dictionary. For instance, a.x has a lookup chain starting witha.__dict__[‘x'], then type(a).__dict__[‘x'], and continuing through the base classes of type(a) excluding metaclasses. If the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined.—?摘自官方文档

简单的说描述符会改变一个属性的基本的获取、设置和删除方式

先看如何用描述符来解决上面 property逻辑重复的问题。

class Integer(object):
 def __init__(self, name):
 self.name = name
 
 def __get__(self, instance, owner):
 return instance.__dict__[self.name]
 
 def __set__(self, instance, value):
 if value < 0:
  raise ValueError("Negative value not allowed")
 instance.__dict__[self.name] = value
 
class Movie(object):
 score = Integer('score')
 ticket = Integer('ticket')

因为描述符优先级高并且会改变默认的getset行为,这样一来,当我们访问或者设置Movie().score的时候都会受到描述符Integer的限制。

不过我们也总不能用下面这样的方式来创建实例。

a = Movie()
a.score = 1
a.ticket = 2
a.title = ‘test'
a.descript = ‘…'

这样太生硬了,所以我们还缺一个构造函数。

class Integer(object):
 def __init__(self, name):
 self.name = name
 
 def __get__(self, instance, owner):
 if instance is None:
  return self
 return instance.__dict__[self.name]
 
 def __set__(self, instance, value):
 if value < 0:
  raise ValueError('Negative value not allowed')
 instance.__dict__[self.name] = value
 
 
class Movie(object):
 score = Integer('score')
 ticket = Integer('ticket')
 
 def __init__(self, title, description, score, ticket):
 self.title = title
 self.description = description
 self.score = score
 self.ticket = ticket

这样在获取、设置和删除scoreticket的时候都会进入Integer__get__ __set__ ,从而减少了重复的逻辑。

现在虽然问题得到了解决,但是你可能会好奇这个描述符到底是如何工作的。具体来说,在__init__函数里访问的是自己的self.scoreself.ticket,怎么和类属性scoreticket关联起来的?

描述符如何工作

看官方的说明

If an object defines both __get__() and __set__(), it is considered a data descriptor. Descriptors that only define __get__() are called non-data descriptors (they are typically used for methods but other uses are possible).

Data and non-data descriptors differ in how overrides are calculated with respect to entries in an instance's dictionary. If an instance's dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance's dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence.

The important points to remember are:

descriptors are invoked by the __getattribute__() method
overriding __getattribute__() prevents automatic descriptor calls
object.__getattribute__() and type.__getattribute__() make different calls to __get__().
data descriptors always override instance dictionaries.
non-data descriptors may be overridden by instance dictionaries.

类调用__getattribute__()的时候大概是下面这样子:

def __getattribute__(self, key):
 "Emulate type_getattro() in Objects/typeobject.c"
 v = object.__getattribute__(self, key)
 if hasattr(v, '__get__'):
 return v.__get__(None, self)
 return v

下面是摘自国外一篇博客上的内容。

Given a Class “C” and an Instance “c” where “c = C(…)”, calling “c.name” means looking up an Attribute “name” on the Instance “c” like this:

Get the Class from Instance
Call the Class's special method getattribute__. All objects have a default __getattribute
Inside getattribute

Get the Class's mro as ClassParents
For each ClassParent in ClassParents
If the Attribute is in the ClassParent's dict
If is a data descriptor
Return the result from calling the data descriptor's special method __get__()
Break the for each (do not continue searching the same Attribute any further)
If the Attribute is in Instance's dict
Return the value as it is (even if the value is a data descriptor)
For each ClassParent in ClassParents
If the Attribute is in the ClassParent's dict
If is a non-data descriptor
Return the result from calling the non-data descriptor's special method __get__()
If it is NOT a descriptor
Return the value
If Class has the special method getattr
Return the result from calling the Class's special method__getattr__.

我对上面的理解是,访问一个实例的属性的时候是先遍历它和它的父类,寻找它们的__dict__里是否有同名的data descriptor如果有,就用这个data descriptor代理该属性,如果没有再寻找该实例自身的__dict__ ,如果有就返回。任然没有再查找它和它父类里的non-data descriptor,最后查找是否有__getattr__

描述符的应用场景

python的property、classmethod修饰器本身也是一个描述符,甚至普通的函数也是描述符(non-data discriptor)

django model和SQLAlchemy里也有描述符的应用

class User(db.Model):
 id = db.Column(db.Integer, primary_key=True)
 username = db.Column(db.String(80), unique=True)
 email = db.Column(db.String(120), unique=True)
 
 def __init__(self, username, email):
 self.username = username
 self.email = email
 
 def __repr__(self):
 return '<User %r>' % self.username

总结

只有当确实需要在访问属性的时候完成一些额外的处理任务时,才应该使用property。不然代码反而会变得更加??拢??艺庋?崛贸绦虮渎?芏唷R陨暇褪潜疚牡娜?磕谌荩?捎诟鋈四芰τ邢蓿?闹腥缬斜饰蟆⒙呒?砦笊踔粮拍钚源砦螅?骨胩岢霾⒅刚??/p>

Python 相关文章推荐
Python判断文件和字符串编码类型的实例
Dec 21 Python
Python pymongo模块用法示例
Mar 31 Python
django orm 通过related_name反向查询的方法
Dec 15 Python
python3+PyQt5 自定义窗口部件--使用窗口部件样式表的方法
Jun 26 Python
浅析Python+OpenCV使用摄像头追踪人脸面部血液变化实现脉搏评估
Oct 17 Python
Django 简单实现分页与搜索功能的示例代码
Nov 07 Python
OpenCV+Python--RGB转HSI的实现
Nov 27 Python
PyTorch中的C++扩展实现
Apr 02 Python
解决Python数据可视化中文部分显示方块问题
May 16 Python
python Django 反向访问器的外键冲突解决
May 20 Python
Pytorch转onnx、torchscript方式
May 25 Python
Python应用自动化部署工具Fabric原理及使用解析
Nov 30 Python
Python实现基本线性数据结构
Aug 22 #Python
Python进行数据提取的方法总结
Aug 22 #Python
详解Python实现按任意键继续/退出的功能
Aug 19 #Python
利用Python开发微信支付的注意事项
Aug 19 #Python
Python用模块pytz来转换时区
Aug 19 #Python
教你用python3根据关键词爬取百度百科的内容
Aug 18 #Python
利用Python爬取可用的代理IP
Aug 18 #Python
You might like
PHP的foreach中使用引用时需要注意的一个问题和解决方法
2014/05/29 PHP
详解php与ethereum客户端交互
2018/04/28 PHP
Ajax+Jpgraph实现的动态折线图功能示例
2019/02/11 PHP
什么是PHP7中的孤儿进程与僵尸进程
2019/04/14 PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
2020/03/24 PHP
Mac系统下搭建Nginx+php-fpm实例讲解
2020/12/15 PHP
js 省地市级联选择
2010/02/07 Javascript
Windows系统下Node.js的简单入门教程
2015/06/23 Javascript
JavaScript实现的浮动层框架用法实例分析
2015/10/10 Javascript
CSS3 media queries结合jQuery实现响应式导航
2016/09/30 Javascript
jQuery实现滚动条滚动到子元素位置(方便定位)
2017/01/08 Javascript
浅谈Emergence.js 检测元素可见性的 js 插件
2017/11/18 Javascript
Vue+Vux项目实践完整代码
2017/11/30 Javascript
微信小程序实现图片懒加载的示例代码
2017/12/13 Javascript
在nginx上部署vue项目(history模式)的方法
2017/12/28 Javascript
解决在Bootstrap模糊框中使用WebUploader的问题
2018/03/22 Javascript
利用js实现前后台传送Json的示例代码
2018/03/29 Javascript
微信小程序发送短信验证码完整实例
2019/01/07 Javascript
VUE+elementui面包屑实现动态路由详解
2019/11/04 Javascript
vue 实现移动端键盘搜索事件监听
2019/11/06 Javascript
jquery获取input输入框中的值
2019/11/13 jQuery
Javascript异步编程async实现过程详解
2020/04/02 Javascript
[02:08:58]2014 DOTA2国际邀请赛中国区预选赛 Ne VS CIS
2014/05/22 DOTA
Python打印斐波拉契数列实例
2015/07/07 Python
python中sys.argv函数精简概括
2018/07/08 Python
python GUI实现小球满屏乱跑效果
2019/05/09 Python
由面试题加深对Django的认识理解
2019/07/19 Python
Python 中判断列表是否为空的方法
2019/11/24 Python
Python监控服务器实用工具psutil使用解析
2019/12/19 Python
根据tensor的名字获取变量的值方式
2020/01/04 Python
python如何解析复杂sql,实现数据库和表的提取的实例剖析
2020/05/15 Python
用html5绘制折线图的实例代码
2016/03/25 HTML / CSS
英文版区域经理求职信
2013/10/23 职场文书
客服专员岗位职责范本
2013/11/29 职场文书
奥巴马胜选演讲稿
2014/05/15 职场文书
pytorch交叉熵损失函数的weight参数的使用
2021/05/24 Python