Python装饰器中@property深度详解

之前有写了两篇关于装饰器、property的用法,感觉虽然可能很长,但是有些冗杂。本文讲依据官方文档,进行一些更加深入浅出的讲解。

最初的声明方式

在没有@property修饰的情况下,需要分别声明get、set、delete函数,然后初始化property类,将这些方法加载进property中。下面以class C为例,其中,class C持有property的实例化对象x。

当class C对外表现出来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接受四个参数

  1. fget,用于获取属性值,
  2. fset,用于设置属性值
  3. fdel,用于删除属性
  4. doc,属性的介绍
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的语法糖,可以拆解成普通使用方法,如class C中的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)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值