Python3 property属性

简介:

python3中的property有一个很有意思的功能,它能将类中的方法像类属性一样调用!

1
class  property (fget = None , fset = None , fdel = None , doc = None )

我们先来简单了解一下这个property类,下面看一下官网给出的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class  C:
     def  __init__( self ):
         self ._x  =  None
 
     def  getx( self ):
         return  self ._x
 
     def  setx( self , value):
         self ._x  =  value
 
     def  delx( self ):
         del  self ._x
 
     =  property (getx, setx, delx,  "I'm the 'x' property." )   # 这里的x相当于类属性
     
=  C()   # 生成一个对象
c.x  =  10   # 设置self._x=10,实际调用的就是类中setx方法
c.x   # 获取self._x的值,实际调用的就是类中getx方法
del  c.x   # 删除self._x的值,实际调用的就是类中delx方法

    是不是感觉很有意思,很不可思议!property中fget是一个函数,它获取属性值;fset是一个函数,它设置一个属性值;fdel是一个函数,它删除一个属性值;doc为该属性创建一个docstring。你只要在使用时在对应的形参位置放上你写的对应函数,就可以轻松使用了。


我们还可以将property作为装饰器来使用,还是使用官网的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class  C:
     def  __init__( self ):
         self ._x  =  None
 
     @ property
     def  x( self ):
         """I'm the 'x' property."""
         return  self ._x
 
     @x.setter
     def  x( self , value):
         self ._x  =  value
 
     @x.deleter
     def  x( self ):
         del  self ._x

    property对象有getter、setter、deleter三个方法,getter获取属性值,setter设置属性值,deleter设置属性值,这个例子的效果跟上一个例子的效果完全相同!我们看到里面的方法名是一模一样的,但是达到的效果却是不同的。第一个x方法是获取属性值,第二个x方法是设置属性值,第三个x方法是删除属性值。

    你看到这里是不是以为这一切都是property帮你做到的,错,错,错!其实property只做了一件事件,它将你的方法能像类属性一样使用,至于里面的查、删、改,其实都是你自己写的函数实现的!fget、fset、fdel、setter、deleter这些仅仅只是名字而且,方便你识别,其他什么作用都没有!

我们来看一下property的源代码:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class  property ( object ):
     """
     property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
     
     fget is a function to be used for getting an attribute value, and likewise
     fset is a function for setting, and fdel a function for del'ing, an
     attribute.  Typical use is to define a managed attribute 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.")
     
     Decorators make defining new properties or modifying existing ones easy:
     
     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
     """
     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 property.__init__
         """
         property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
         
         fget is a function to be used for getting an attribute value, and likewise
         fset is a function for setting, and fdel a function for del'ing, an
         attribute.  Typical use is to define a managed attribute 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.")
         
         Decorators make defining new properties or modifying existing ones easy:
         
         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
         
         # (copied from class doc)
         """
         pass
 
     @ staticmethod  # known case of __new__
     def  __new__( * args,  * * kwargs):  # real signature unknown
         """ Create and return a new object.  See help(type) for accurate signature. """
         pass
 
     def  __set__( self * args,  * * kwargs):  # real signature unknown
         """ Set an attribute of instance to value. """
         pass
 
     fdel  =  property ( lambda  self object (),  lambda  self , v:  None lambda  self None )   # default
 
     fget  =  property ( lambda  self object (),  lambda  self , v:  None lambda  self None )   # default
 
     fset  =  property ( lambda  self object (),  lambda  self , v:  None lambda  self None )   # default
 
     __isabstractmethod__  =  property ( lambda  self object (),  lambda  self , v:  None lambda  self None )   # default

    看到上面的源代码恍然大悟没,fdel、fget、fset都只是执行你函数里面的代码而已!所以我们就记住一句话就够了:“property能让你的方法像类属性一样使用”。

本文转自戴柏阳的博客博客51CTO博客,原文链接http://blog.51cto.com/daibaiyang119/1972460如需转载请自行联系原作者


daibaiyang119

相关文章
|
2月前
|
SQL 定位技术 API
GEE python:按照矢量中的几何位置、属性名称和字符串去筛选矢量集合
GEE python:按照矢量中的几何位置、属性名称和字符串去筛选矢量集合
33 0
|
3月前
|
监控 API Python
京东商品sku属性数据接口Python
京东商品sku属性数据接口Python
27 0
|
4月前
|
Python
python ndarray属性分析介绍
python ndarray属性分析介绍
|
5月前
|
Python
103 python高级 - 内建属性
103 python高级 - 内建属性
24 0
|
5月前
|
Python
97 python高级 - 属性property
97 python高级 - 属性property
26 0
|
4月前
|
计算机视觉 Python
OpenCV获取视频文件的属性并动态显示实战(附Python源码)
OpenCV获取视频文件的属性并动态显示实战(附Python源码)
47 0
|
3天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
37 0
|
5月前
|
Python
python 获取、修改 时间戳 时间属性 文件时间
python 获取、修改 时间戳 时间属性 文件时间
32 0
|
1月前
|
程序员 Python
【python基础知识】python怎么查看对象的属性
【python基础知识】python怎么查看对象的属性
17 0
|
2月前
|
存储 Python
解释Python中的`__dict__`属性的作用。
【2月更文挑战第3天】【2月更文挑战第7篇】