Python装饰器中@property使用详解

 更新时间:2022年01月26日 14:18:40   作者:小朋友2D  
大家好,本篇文章主要讲的是Python装饰器中@property使用详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下

最初的声明方式

在没有@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)

总结

到此这篇关于Python装饰器中@property使用详解的文章就介绍到这了,更多相关Python饰器@property内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python导入不同目录下的自定义模块过程解析

    python导入不同目录下的自定义模块过程解析

    这篇文章主要介绍了python导入不同目录下的自定义模块过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • pycharm修改file type方式

    pycharm修改file type方式

    今天小编就为大家分享一篇pycharm修改file type方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • 通过python连接Linux命令行代码实例

    通过python连接Linux命令行代码实例

    这篇文章主要介绍了通过python连接Linux命令行代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Flask-Caching如何使用提高性能

    Flask-Caching如何使用提高性能

    Flask-Caching是提高Flask应用性能的工具,通过缓存数据减少重复计算,加快响应速度,本文就来介绍一下Flask-Caching使用,具有一定的参考价值,感兴趣的可以了解一下
    2025-01-01
  • Python实现线性判别分析(LDA)的MATLAB方式

    Python实现线性判别分析(LDA)的MATLAB方式

    今天小编大家分享一篇Python实现线性判别分析(LDA)的MATLAB方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • JAVA SWT事件四种写法实例解析

    JAVA SWT事件四种写法实例解析

    这篇文章主要介绍了JAVA SWT事件四种写法实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • python numpy实现多次循环读取文件 等间隔过滤数据示例

    python numpy实现多次循环读取文件 等间隔过滤数据示例

    这篇文章主要介绍了python numpy实现多次循环读取文件 等间隔过滤数据示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • django中类属性和类方法的实现

    django中类属性和类方法的实现

    在django中,类的属性可以直接在实例化对象或类中调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-10-10
  • Python中的数据类dataclass解读

    Python中的数据类dataclass解读

    这篇文章主要介绍了Python中的数据类dataclass使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Python中的index()方法使用教程

    Python中的index()方法使用教程

    这篇文章主要介绍了Python中的index()方法使用教程,是Python入门学习中的基础知识,需要的朋友可以参考下
    2015-05-05

最新评论