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
猜你喜欢
- 新建一个项目 app02在 app02/ 下创建 urls.py:from django.conf.urls import urlfrom
- 微信小程序全称微信公众平台·小程序,原名微信公众平台·应用号(简称微信应用号)声明•微信小程序开发工具类似于一个轻量级的IDE集成开发环境,
- 首先说一下需求,我需要将数据以分钟为单位进行分组,然后每一分钟内的数据作为一行输出,因为不同时间的数据量不一样,所以所有数据按照最长的那组数
- 1、首先看SSM(Spring+SpringBoot+Mybatis)的依赖<project xmlns="http://m
- LyScript是一款x64dbg主动化操控插件,经过Python操控X64dbg,完成了远程动态调试,解决了逆向工作者剖析漏洞,寻觅指令片
- 文件打开方式当我们用open()函数去打开文件的时候,有好几种打开的模式。'r'->只读'w'->
- 循环是我们经常用到的一个概念,比如,循环计算数字叠加、循环输出文字内容等。循环是运行重复内容的一个最简单的方法,简化了代码流程,增加了时效性
- 修改my.ini或my.conf,将sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_US
- 前言Python 的一大优点就是丰富的类库,所以我们经常会用 pip 来安装各种库,所以对于Python开发用户来讲,PIP安装软件包是家常
- Python2默认是不支持中文的,一般我们在程序的开头加上#-*-coding:utf-8-*-来解决这个问题,但是在我用open()方法打
- 翻译:用法:zeros(shape, dtype=float, order='C')返回:返回来一个给定形状和类型的用0填充
- 这篇博客将介绍如何通过OpenCV中图像修复的技术——cv2.inpaint() 去除旧照片中的小噪音、笔划等。并提供一个可交互式的程序,利
- // 格式化字符串 Fmt("{0}.[{id}].{name}",{id:1,name:'
- 前言时隔108天,何同学在B站发布了最新的视频,《【何同学】我用108天开了个灯…》。那么就让我们用爬虫,爬取视频的弹
- ASP+javascript实现可显示和隐藏的树型菜单实例:<script language=&qu
- ASP生成柱型体,折线图,饼图源代码。一:纯ASP代码生成图表函数2——折线图;二:纯ASP代码生成图表函数1——柱状图 ;三:纯
- 一 Process对象的join方法在主进程运行过程中如果想并发地执行其他的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情
- pytorch中基本的变量类型当属FloatTensor(以下都用floattensor),而Variable(以下都用variable)是
- 前言上一篇暴力破解文章:一个简单的Python暴力破解网站登录密码脚本测试靶机为Pikachu漏洞练习平台暴力破解模块下的 &ldq
- 本文实例讲述了Python中逗号的三种作用。分享给大家供大家参考。具体分析如下:最近研究python 遇到个逗号的问题 一直没弄