脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python装饰器中@property使用详解

Python装饰器中@property使用详解

2022-09-04 11:59小朋友2D Python

大家好,本篇文章主要讲的是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被隐藏起来了

?
1
2
3
4
5
6
7
8
9
10
11
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)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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同理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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(...)

可以认为是

?
1
2
3
4
5
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同理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/ct2020129/article/details/122681130

延伸 · 阅读

精彩推荐
  • Python详解python的数字类型变量与其方法

    详解python的数字类型变量与其方法

    大家都知道数字数据类型存储数值,它们是不可变的数据类型,这意味着改变数据类型的结果值,需要一个新分配的对象。这篇文章给大家详细介绍了pyt...

    偶木2842020-09-13
  • PythonJupyter Notebook 远程访问配置详解

    Jupyter Notebook 远程访问配置详解

    这篇文章主要介绍了Jupyter Notebook 远程访问配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    MachinePlay11422021-08-24
  • Python解决python中set与dict的无序问题

    解决python中set与dict的无序问题

    这篇文章主要介绍了解决python中set与dict的无序问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    big蟒蛇10932021-09-18
  • Python100行Python代码实现自动抢火车票(附源码)

    100行Python代码实现自动抢火车票(附源码)

    又到年底了,相信对于在外地的朋友们来说,火车票是到年底最头痛的一件事了,但作为程序员的你怎么能一样呢?快发挥你的特长,下面这篇文章主要给...

    然学科技26582021-01-03
  • Python我们在程序员节组织了一场游戏,竟还用 Python 去验证其公平性?

    我们在程序员节组织了一场游戏,竟还用 Python 去验证其公平性

    程序员节,公司举办了一个抽奖活动,采用的方式是掷六次骰子,组成一个六位数,再对群里的人数取模,计算的结果就是中奖的人的编号。但这种方式公...

    数据和云7672021-11-02
  • PythonPython 字符串去除空格的五种方法

    Python 字符串去除空格的五种方法

    这篇文章主要介绍了Python 字符串去除空格的五种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    基咯咯9122021-09-28
  • PythonPython中那些神一样的算法

    Python中那些神一样的算法

    我一直说python是非常优美的语言,那到底如何个美呢,其中有一个特性就是简洁。有的时候做产品不是要做加法而且要做减法,并且做到极致。...

    编程小清3982021-04-24
  • Python详解Python 正则表达式模块

    详解Python 正则表达式模块

    这篇文章主要介绍了Python 正则表达式模块详解,分为两部分,包括基础语法总结,re模块的相关知识,需要的朋友可以参考下...

    Cassie14929492366268492021-04-16