forked from misakar/pythonCookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.9.py
53 lines (42 loc) · 1.26 KB
/
8.9.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
#!/usr/bin/env python
# encoding: utf-8
"""
8.9.py
~~~~~~
类描述符的使用
描述符是具有对属性字典存取函数(set, get, del)的类
使用描述符在类变量的层面初始化属性
从而可以添加属性操作,实现更复杂的功能
描述符还可以代码复用,广泛应用于大型框架中
"""
class Integer:
"""一个描述符类"""
def __init__(self, x):
"""定义变量操作"""
self.x = x
def __get__(self, instance, cls):
"""有两种访问方式
1实例访问: Class.__get__(instance, Class)
2类访问 : Class.__get__(None, Class)"""
if instance is None:
return self
else:
return instance.__dict__.get(self.x) # self.x 为键
def __set__(self, instance, value):
if not isinstance(value, int):
raise TypeError('Expect a int')
instance.__dict__[self.x] = value # get只是访问字典中的值
def __delete__(self, instance):
del instance.__dict__[self.x]
# 使用这个描述符类
class Value:
x = Integer('x')
y = Integer('y')
def __init__(self, x, y):
self.x = x
self.y = y
# main
v = Value
v.x = 1
v.y = 1.2
Value.x