iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python装饰器中@property使用详解
  • 400
分享到

Python装饰器中@property使用详解

2024-04-02 19:04:59 400人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录最初的声明方式使用装饰器的声明方式使用装饰器的调用过程总结最初的声明方式 在没有@property修饰的情况下,需要分别声明get、set、delete函数,然后初始化prope

最初的声明方式

在没有@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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python装饰器中@property使用详解

本文链接: https://www.lsjlt.com/news/163852.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作