-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproperty.py
66 lines (47 loc) · 1.31 KB
/
property.py
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
__author__ = 'jszheng'
# 在__dict__中的东西是attribute (用buildin方法 vars(obj)获取obj.__dict__) locals(), globals()
# 而property是指getter/setter方法的重载[方法属性], 其实就是描述符的另外一种表现
class Person:
def __init__(self, name):
self._name = name
def getName(self):
return self._name
def setName(self, value):
self._name = value
def delName(self):
del self._name
name = property(getName, setName, delName, 'name property doc')
# 用property方式
class deco_property:
def __init__(self, name):
self._name = name
@property
def name(self):
"name property docs"
print('fetching...')
return self._name
@name.setter
def name(self, value):
print("change...")
self._name = value
@name.deleter
def name(self):
print('remove...')
del self._name
# 描述符
if __name__ == '__main__':
bob = Person('Bob Smith')
print(bob.name)
bob.name = 'Robert Smith'
print(bob.name)
print()
bob = deco_property('Bob Smith')
print(bob.name)
bob.name = 'Robert Smith'
print(bob.name)
del bob.name
print('-'*20)
sue = deco_property('Sue Jones')
print(sue.name)
print(deco_property.name.__doc__)
print()