Click to Visit Homepage : zzyzz.top


Property - 特性(Python)

  1 Property - Python 特性
2 不同的书籍对 property 一词的翻译有所不同, 我们将 property 翻译成 '特性' 以区别于 attribute 一词. 3 先看看 property 类在 Python 中的定义, 4 结构, 5 class property(object): 6 def __init__(self, fget=None, fset=None, fdel=None, doc=None): 7 ... 8 ... 9 ... 10 简略表示为, 11 class property(fget=None, fset=None, fdel=None, doc=None) 12 13 其中参数, 14 fget 是一个'取值'(get attribute value)函数 15 fset 是一个'设值'(set attribute value)函数 16 fdel 是一个'删除值'(delete attribute value)函数 17 doc 对所返回的 property attribute 的描述(docstring) 18 19 property 的典型用法(以通过 property 管理属性 A.a 为例), 20 21 class A(object): 22 def __init__(self): 23 self._a = None 24 25 def geta(self): 26 """ This docstring in fget method """ 27 print('geta is called') 28 return self._a 29 30 def seta(self, value): 31 print('seta is called') 32 self._a = value 33 34 def dela(self): 35 print('dela is called') 36 del self._a 37 38 a = property(geta, seta, dela, "Typical use of \'property\'") 39 #a = property(geta, seta, dela) 40 41 if __name__ == '__main__': 42 tester1 = A() 43 print(vars(A)) #1 44 print(vars(tester1)) #2 45 tester1.a = 'hello world' #3 46 print(tester1.a) #4 47 del tester1.a #5 48 print(vars(A)) #6 49 print(vars(tester1)) #7 50 print(A.a.__doc_) #8 51 tester1.a = 'hello world2' #9 52 print(vars(tester1)) #9 53 print(tester1.a) 54 55 Output, 56 {'__module__': '__main__', '__init__': <function A.__init__ at 0x02DB62B8>, 'geta': <function A.geta at 0x02DB6270>, 57 'seta': <function A.seta at 0x02DB6228>, 'dela': <function A.dela at 0x02DB61E0>, 'a': <property object at 0x02DB92A0>, 58 '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None} 59 #1 类对象中存在被命名为 'a' property 对象 60 61 {'_a': None} #2 实例实际所拥有时是 _a, 62 seta is called #3 会调用 'seta' 63 geta is called 64 hello world #4 将调用 'geta' 65 dela is called #5 调用 'dela' 66 {'__module__': '__main__', '__init__': <function A.__init__ at 0x02DB62B8>, 'geta': <function A.geta at 0x02DB6270>, 67 'seta': <function A.seta at 0x02DB6228>, 'dela': <function A.dela at 0x02DB61E0>, 'a': <property object at 0x02DB92A0>, 68 '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None} 69 70 {} #6 实例的 '_a' 属性被删除 71 return self._a, AttributeError: 'A' object has no attribute '_a' #7 虽然调用了 del 类对象中仍然存在被命名为 'a' property 对象 72 73 Typical use of 'property' #8 第四个参数 docstring 存在, property a 的 __doc__ 属性为所提供 docstring 参数 74 This docstring in fget method #8 没有设置第四个该参数, 则 property a 的 __doc__ 会引用 geta 的 docstring 75 76 seta is called #9 在次设置 property a 77 {'_a': 'hello world2'} #9 '_a' 回到 '实例' 属性中 78 geta is called #9 79 hello world2 #9 80 81 注, abc = A(), abc.a 将调用 'geta' ; abc.a = 3 会调用 'seta'; del abc.a 调用 'dela' 82 若第四个参数 docstring 存在, property a 的 __doc__ 属性为所提供 docstring 参数(A.a.__doc__); 83 若没有设置该参数, 则 property a 的 __doc__ 会引用 geta 的 docstring. 84 If abc is an instance of A, abc.a will invoke the getter, abc.a = value will invoke the setter and del abc.a the deleter. 85 If given, doc will be the docstring of the property attribute. Otherwise, the property will copy fget’s docstring (if it exists). 86 87 88 property 类, 更实用的是 以装饰器 @property 来实现 '只读' readonly 属性(若实例中没有实现 a.setter, a 便成为'只读' readonly 属性), 89 例子(与上例等价形式的 @property 的实现), 90 91 class B(object): 92 def __init__(self): 93 self._a = None 94 95 @property # 将 'a' 装饰成 property 96 def a(self): 97 """ This docstring in fget method""" 98 print('fget is called') 99 return self._a 100 101 # The @property decorator turns the a() method into a “getter” 102 # for a read-only attribute with the same name, and it sets the 103 # docstring for a to “This docstring in fget method” 104 # 如类中没有实现 a.setter, a 便成为'只读' readonly 属性. 105 106 @a.setter # 装饰 property a 的 setter 107 def a(self, value): 108 print('fset is called') 109 self._a = value 110 111 @a.deleter # 装饰 property a 的 deleter 112 def a(self): 113 print('fdel is called') 114 del self._a 115 116 *** 注, 务必保证 setter 和 deleter 所装饰的 '函数' 与 被装饰的 '特性' 函数 名字一致 117 Be sure to give the additional functions the same name as the original property (a in this case.)

 

posted @ 2017-09-25 15:39  zzYzz  阅读(411)  评论(0)    收藏  举报


Click to Visit Homepage : zzyzz.top