python基础教学之125 装饰器简介(python3 property装饰器实现原理与用法示例)
类别:脚本大全 浏览量:289
时间:2021-10-01 01:25:09 python基础教学之125 装饰器简介
python3 property装饰器实现原理与用法示例本文实例讲述了python3 property装饰器实现原理与用法。分享给大家供大家参考,具体如下:
学习python的同学,慢慢的都会接触到装饰器,装饰器在python里是功能强大的语法。装饰器配合python的魔法方法,能实现很多意想不到的功能。废话不多说,如果你已经掌握了闭包的原理,代码的逻辑还是可以看明白的,咱们直接进入正题。
property的意义
@property
把一个类的getter方法变成属性,如果还有setter方法,就在setter方法前面加上@method.setter
。使用类属性=property(getx,setx,delx,desc)也是可以的。
实现很简单,那么它背后的原理是什么呢?
property类的伪代码如下,里面涉及了__get__、__set__、__delete__魔法方法。decorator类是装饰器类,target是目标类。当你设置装饰器类的实例对象为目标类的x属性后,当试图访问目标类的x属性会触发装饰器类的__get__方法;当为目标类的x属性赋值时,会触发装饰器类的__setter__方法;尝试删除目标类的x属性时,会触发装饰器类的__delete__方法。当访问target.x.__doc__,可以打印出装饰器类的描述文档。事实上这种装饰器类也被称为描述符类。描述符类就是将一个特殊类的实例指派给一个类的属性。
类属性实现方式:
|
class decorator( object ): def __init__( self , fget = none, fset = none, fdel = none, doc = none): self .fget = fget self .fset = fset self .fdel = fdel self .__doc__ = doc def __get__( self , instance, owner): if instance is none: return self return self .fget(instance) def __set__( self , instance, value): self .fset(instance, value) def __delete__( self , instance): self .fdel(instance) def getter( self , fget): return decorator(fget, self .fset, self .fdel, self .__doc__) def setter( self , fset): return decorator( self .fget, fset, self .fdel, self .__doc__) def deleter( self , fdel): return decorator( self .fget, self .fset, fdel, self .__doc__) class target( object ): desc = "amazing pyhton" def __init__( self , attr = 5 ): self ._x = attr def getx( self ): return self ._x def setx( self , value): self ._x = value def delx( self ): del self ._x x = decorator(getx,setx,delx,desc) |
装饰器实现方式:
|
class decorator( object ): def __init__( self , fget = none, fset = none, fdel = none, doc = none): self .fget = fget self .fset = fset self .fdel = fdel self .__doc__ = doc def __get__( self , instance, owner): if instance is none: return self return self .fget(instance) def __set__( self , instance, value): self .fset(instance, value) def __delete__( self , instance): self .fdel(instance) def getter( self , fget): return decorator(fget, self .fset, self .fdel, self .__doc__) def setter( self , fset): return decorator( self .fget, fset, self .fdel, self .__doc__) def deleter( self , fdel): return decorator( self .fget, self .fset, fdel, self .__doc__) class target( object ): desc = "amazing pyhton" def __init__( self , attr = 5 ): self ._x = attr @decorator def show( self ): return self ._x @show .setter def show( self , value): self ._x = value @show .deleter def show( self ): del self ._x |
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/weixin_43265804/article/details/82863984
您可能感兴趣
- python装饰器使用说明(详解Python装饰器)
- python装饰器怎么编写(Python函数装饰器常见使用方法实例详解)
- python基础教学之125 装饰器简介(python3 property装饰器实现原理与用法示例)
- python装饰器初学者教程(Python3.5装饰器原理及应用实例详解)
- python 装饰器模式(python重试装饰器的简单实现方法)
- python装饰器语法与应用(python装饰器简介---这一篇也许就够了推荐)
- python装饰器怎么设置(深入了解和应用Python 装饰器 @decorator)
- 12步轻松搞定python装饰器(Python3.5装饰器典型案例分析)
- 幼小衔接-20以内看图读数 写数 数的组成练习题(幼小衔接-20以内看图读数)
- 你只要花上20天记单词,英语成绩就能从57提到100(你只要花上20天记单词)
- 夕云天际飞,亢龙化太极(夕云天际飞亢龙化太极)
- 爱情可以当饭吃吗(怎么回复)
- 高考数学题(高考数学题基础题占多少分)
- 没钱只能吃土(没钱要吃土了幽默短信发朋友圈)
热门推荐
- dockerfile启动容器服务(docker文件存放路径, 获取容器启动命令操作)
- apache配置详解(apache2.2和php5.2.17在windows下整合过程的错误解决方法)
- php中怎么判断变量类型(PHP INT类型在内存中占字节详解)
- css不显示滚动条设置(css隐藏移动端滚动条并且ios上平滑滚动的方法)
- react加载优化(React星星评分组件的实现)
- python栈和队列(Python 实现数据结构中的栈队列)
- CSS中margin和padding的区别
- ifix退出密码(详解如何在登录过期后跳出Ifram框架)
- python画折线图(python使用Plotly绘图工具绘制水平条形图)
- docker进入mysql查看路径(Docker 环境运行 Mysql 和开启 Binlog 配置主从同步的设置方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8