python之路(11)描述符
前言
描述符是用于代理另一个类的属性,一般用于大型的框架中,在实际的开发项目中较少使用,本质是一个实现了__get__(),__set__(),__delete__()其中一个方法的新式类
__get__():调用一个属性时执行
__set__():属性赋值时执行
__delete__():采用del删除属性时触发
示例
#描述符
class test:
def __get__(self, instance, owner):
print('---get方法') def __set__(self, instance, value):
print('---set方法') def __delete__(self, instance):
print('---delete方法') class test2:
name = test()#描述符代理了test2类里的name属性 t1 = test2()
#调用了被描述符代理的属性,执行__get__()方法
t1.name #---get方法
#赋值了被描述符代理的属性,执行__set__()方法
t1.name = 1 #---set方法
#赋值了被描述符代理的属性,执行__delete__()方法
del t1.name #---delete方法
属性查找的优先级
1.类属性
2.数据描述符(同时实现了__get__()和__set__()方法)
3.实例属性
4.非数据描述符(没有实现__set__()方法)
5.找不到的属性执行__getattr__()
#非数据描述符
class test:
def __get__(self, instance, owner):
print('get方法') class test2:
name = test()#描述符代理了test2类里的name属性 t1 = test2()
#在实例对象里中创建了name属性,并赋值chen
t1.name = 'chen'
print(t1.__dict__) #{'name': 'chen'}
利用描述符实现赋值类型限制(一)
#数据描述符
class Typed:
def __init__(self,key,value_type):
self.key = key
self.value_type = value_type def __get__(self, instance, owner):
print('get方法')
return instance.__dict__[self.key] def __set__(self, instance, value):
print('set方法')
if not isinstance(value,self.value_type): #判断类型
raise TypeError('传入的类型不是',self.value_type)
instance.__dict__[self.key] = value def __delete__(self, instance):
print('delete方法')
instance.__dict__.pop(self.key) class People:
name = Typed('name',str) #数据描述符代理属性
age = Typed('age',int) #数据描述符代理属性 def __init__(self,name,age):
self.name = name #赋值操作,执行__set__方法
self.age = age #测试
p1 = People('chen',21)
print(p1.__dict__) #{'name': 'chen', 'age': 21} p1 = People('chen','21') #报错:TypeError: ('传入的类型不是', <class 'int'>)
利用描述符和装饰器实现赋值类型限制(二)
#数据描述符
class Typed:
def __init__(self,key,value_type):
self.key = key
self.value_type = value_type def __get__(self, instance, owner):
print('get方法')
return instance.__dict__[self.key] def __set__(self, instance, value):
print('set方法')
if not isinstance(value,self.value_type): #判断类型
raise TypeError('传入的类型不是',self.value_type)
instance.__dict__[self.key] = value def __delete__(self, instance):
print('delete方法')
instance.__dict__.pop(self.key) #限制赋值类型的装饰器
def deco(**kwargs):
def warapper(obj):
for key,val in kwargs.items(): setattr(obj,key,Typed(key,val)) #在装饰器中给类属性实现描述符代理 return obj
return warapper @deco(name=str,age=int) #@wrapper ===>People=wrapper(People)
class People:
#使用装饰器取代了以下两步
# name = Typed('name',str) #数据描述符代理属性
# age = Typed('age',int) #数据描述符代理属性
def __init__(self,name,age):
self.name = name #赋值操作,执行__set__方法
self.age = age #测试
p1 = People('chen',21)
'''
输出:
set方法
set方法
'''
print(p1.__dict__) #{'name': 'chen', 'age': 21}
描述符实现@property
class Demoproperty:
def __init__(self,func):
print('----+')
self.func = func class People:
def __init__(self,name,age):
self.name = name #赋值操作,执行__set__方法
self.age = age @Demoproperty #eat=Demoproperty(eat)
def eat(self):
print("%s在吃饭"%self.name) p1 =People('chen',21)
p1.eat.func(p1) #chen在吃饭
瞎搞
#非数据描述符
class Demoproperty:
def __init__(self,func):
self.func = func def __get__(self, instance, owner):
print('get')
res = self.func(instance)
return res class People: def __init__(self,name,age):
self.name = name #赋值操作,执行__set__方法
self.age = age #实现了添加描述符的操作
@Demoproperty #eat=Demoproperty(eat)
def eat(self):
print("%s在吃饭"%self.name) p1 =People('chen',21)
p1.eat #chen在吃饭
描述符实现@calssmethod
classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等
class ClassMethod:
def __init__(self,func):
self.func=func def __get__(self, instance, owner): #owner是类
def feedback(*args,**kwargs): return self.func(owner,*args,**kwargs) return feedback #返回一个可以传参的方法 class People: @ClassMethod # say_hi=ClassMethod(say_hi)
def say_hi(cls,name,age):
print('我是%s,年龄%s' %(name,age)) People.say_hi('chen',21) #我是chen,年龄21
描述符实现@staticmethod
class StaticMethod:
def __init__(self,func):
self.func=func def __get__(self, instance, owner): #owner是类
def feedback(*args,**kwargs): return self.func(*args,**kwargs) return feedback #返回一个可以传参的方法 class People: @StaticMethod # say_hi=ClassMethod(say_hi)
def say_hi(name,age):
print('我是%s,年龄%s' %(name,age)) People.say_hi('chen',21) #我是chen,年龄21
python之路(11)描述符的更多相关文章
- 如何正确地使用Python的属性和描述符
关于@property装饰器 在Python中我们使用@property装饰器来把对函数的调用伪装成对属性的访问. 那么为什么要这样做呢?因为@property让我们将自定义的代码同变量的访问/设定联 ...
- python高级编程之描述符与属性03
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #属性Property #提供了一个内建描述符类型,它知道如何将一个 ...
- python高级编程之描述符与属性02
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #元描述符 #特点是:使用宿主类的一个或者多个方法来执行一个任务,可 ...
- Python 为什么要使用描述符?
学习 Python 这么久了,说起 Python 的优雅之处,能让我脱口而出的, Descriptor(描述符)特性可以排得上号. 描述符 是Python 语言独有的特性,它不仅在应用层使用,在语言的 ...
- Python中属性和描述符的简单使用
Python的描述符和属性是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题苦恼的 ...
- 流畅的python第二十章属性描述符学习记录
描述符是对多个属性运用相同存取逻辑的一种方式.例如,Django ORM 和 SQL Alchemy等 ORM 中的字段类型是描述符,把数据库记录中字段里的数据与 Python 对象的属性对应起来.描 ...
- 90% 的 Python 开发者不知道的描述符应用
经过上面的讲解,我们已经知道如何定义描述符,且明白了描述符是如何工作的. 正常人所见过的描述符的用法就是上篇文章提到的那些,我想说的是那只是描述符协议最常见的应用之一,或许你还不知道,其实有很多 Py ...
- Python进阶——什么是描述符?
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 在 Python 开发中,你可能听说过「描述符」这个概念,由于我们很少直接使用它,所以大部分开发人员 ...
- python基础学习之描述符和装饰器
描述符的了解: 描述符协议: python描述符是一个"绑定行为"的对象属性,在描述符协议中,它可以通过方法重写属性的访问.这些方法有: __get__, __set__, 和__ ...
- 【python之路11】集合数据类型(set)
集合数据类型(set):集合是不重复的无需序列 1.集合数据类型的创建 a = {11,22,33} #或 a = set() #创建空集合,不能用a={},这样创建的是字典类型 2.集合转换(将可迭 ...
随机推荐
- compaTtelrunner 和win7补丁的那些事
win7 KB2952664的补丁,卸载即可,无关大碍.该进程严重影响磁盘性能.
- 简单的C#实体映射 AutoMapper
AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 要映射实体 public class SourceModel { public int ...
- EF6实现软删除
https://www.jianshu.com/p/c65fbfe16e1a
- yum 安装 python-pip 失败解决方法
这个包在EPEL源里,要添加EPEL源才可以.然后按博客里说的方法添加,执行以下命令: sudo rpm -ivh epel-release* 第一种方式:由于epel在禁用列表里需要另外加参数yum ...
- SpringBoot文档
一.Spring Boot 入门 1.Hello World探究 1.POM文件 1.父项目 <parent> <groupId>org.springframework.b ...
- Webstorm 2018 激活破解
本文最后更新于 2018-5-4 可能会因为没有更新而失效.如已失效或需要修正,请留言! 问题 激活 webstorm 2018 最新版 解决步骤 License server:http://hb5. ...
- pyhton崩溃的第六天,又有新成员
首先在今天的刚刚开始,补充一下上次两个成员的一些特有的方法,一个是列表,一个是字典,首先列表里面多了一个反转的方法,名叫reverse,简单就是把列表中的123变成了321,还有一个方法是sort,是 ...
- Nonlinear Component Analysis as a Kernel Eigenvalue Problem
目录 引 kernel PCA kernel 的选择 性质 一些问题 代码 Scholkopf B, Smola A J, Muller K, et al. Nonlinear component a ...
- keras01 - hello world ~ 搭建第一个神经网络
import numpy as np from keras.datasets import mnist from keras.models import Sequential, Model from ...
- yolov3和darknet opencv版编译安装及基本测试
版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 一.准备工作: 安装pkg-config:sudo apt-get install pkg-confi ...