Python装饰器中@property使用详解
作者:小朋友2D 发布时间:2022-04-22 04:46:44
最初的声明方式
在没有@property修饰的情况下,需要分别声明get、set、delete函数,然后初始化property类,将这些方法加载进property中
class C持有property的实例化对象x
对外表现出来C().x时,实际上是调用C()中的x(property类)中设置的fset,fget,fdel,分别对应getx,setx,delx
C真正持有的x,是self._x被隐藏起来了
class C(object):
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")
property类 结合x = property(getx, setx, delx, "I'm the 'x' property.")
与property的__init__()
可以发现property接受四个参数
fget,用于获取属性值,
fset,用于设置属性值
fdel,用于删除属性
doc,属性的介绍
可以单独设置fget、fset、fdel…
x = property,x.getter(getx),x.setter(setx),x.deleter(delx)
class property(object):
def deleter(self, *args, **kwargs): # real signature unknown
""" Descriptor to change the deleter on a property. """
pass
def getter(self, *args, **kwargs): # real signature unknown
""" Descriptor to change the getter on a property. """
pass
def setter(self, *args, **kwargs): # real signature unknown
""" Descriptor to change the setter on a property. """
pass
def __delete__(self, *args, **kwargs): # real signature unknown
""" Delete an attribute of instance. """
pass
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
def __get__(self, *args, **kwargs): # real signature unknown
""" Return an attribute of instance, which is of type owner. """
pass
def __init__(self, fget=None, fset=None, fdel=None, doc=None): # known special case of
pass
使用装饰器的声明方式
需要注意,装饰器只是一个python的语法糖,可以拆解成普通使用方法,如property(getx)
@property
创建了一个实例x,对于def x(self)
实际上是C类持有x = property(fget=x)
因此,x.setter
方法指向的是property.setter
,也是起到装饰器效果x.setter(x)
(注意,前者x是property实例x,后者x是def x(self, value)
函数),x.deleter
同理
class C(object):
@property
def x(self):
"I am the 'x' property."
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
为什么property实例化后的名字与属性名一致?
换种问法就是为什么x = property(...)
可以认为是
attributes_and_methods = {
x.__name__: property(x), //声明C类持有property实例
#...
}
C = type('C', (object,), attributes_and_methods)
使用装饰器的调用过程
执行C().x时,调用的是C().x(property)绑定的fget方法,用过__get__
唤醒,setter、deleter同理
class property(object):
#...
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
...
def __get__(self, obj, objtype=None): # real signature unknown
if obj is None:
return self
if self.fget is None:
raise AttributeError("unreadable attribute")
return self.fget(obj)
来源:https://blog.csdn.net/ct2020129/article/details/122681130


猜你喜欢
- 请问css如何实现一张图片的倒影。出来的效果就像这样:下面是使用了滤镜来实现倒影的效果:<div style=" width
- 原理:使用js的定时任务函数setInterval设置时间,然后触发关闭事件参数说明title:提示框的标题msg:提示信息内容ico:显示
- 背景最近用uni-app开发小程序项目时,部分需要持久化的内容没法像其他vuex中的state那样调用,所以想着自己实现一下类似vuex-p
- 从官方文档知道linux上面编译安装的mysql默认的连接为100个,这样对于网站的需求来说是远远不够的。 mysql官方告诉我们需要修改m
- 又一年过去了,JavaScript发生了许多变化。但是,即使是2019年了,还是需要给一些帮助你编写干净、整洁、有效、且具有扩展性的代码建议
- Pygal可用来生成可缩放的矢量图形文件,对于需要在尺寸不同的屏幕上显示的图表,这很有用,可以自动缩放,自适应观看者的屏幕1、Pygal模块
- 阅读上一篇:什么是名字空间<meta http-equiv="Content-Type" co
- 使用matplotlib创建百分比堆积柱状图的思路与堆积柱状图类似,只不过bottom参数累计的不是数值而是百分比,因此,需要事先计算每组柱
- 1. datetime 库概述以不同格式显示日期和时间是程序中最常用到的功能。Python 提供了一个处理时间的标准函数库 datetime
- 所有的前提都需要获取到root权限1.结束mysql进程//Linuxsudo services mysql stop//Macbrew s
- JS 控制文本框只能输入数字<input onkeyup="value=value.replace(/[^0-9]/g,
- 在计算机普及的现代设计领域,文字的设计的工作很大一部分由计算机代替人脑完成了(很多平面设计软件中都有制作艺术汉字的引导,以及提供了数十上百种
- golang字符串比较的三种常见方法fmt.Println("go"=="go")fmt.Print
- vue项目用webpack打包想要修改静态资源路径等,找到项目根目录下的config文件夹,打开该文件夹下的index.js文件,默认如下:
- flask_wtf是flask框架的表单验证模块,可以很方便生成表单,也可以当做json数据交互的验证工具,支持热插拔。安装pip inst
- 终于下决心把python从2.7升到了3.7。懒人安装当然使用Anaconda。安装成功,编译成功。但是用pip 安装包的时候提示:pip
- 用python进行线性回归分析非常方便,有现成的库可以使用比如:numpy.linalog.lstsq例子、scipy.stat
- 一 开发环境集成开发工具:jupyter notebook 6.2.5集成开发环境:python 3.10.6第三方库:nump
- 共4个页面:form.asp; chk.asp; num.asp; count.asp,得到一个随即数字。加密解密后成成XBM图片,利用 s
- 1.collections模块collections模块自Python 2.4版本开始被引入,包含了dict、set、list、tuple以