-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescriptorstry.py
68 lines (50 loc) · 1.42 KB
/
descriptorstry.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
65
66
67
68
import random
class List(list):
minn = 0
maxx = 100
class Prop(object):
def __init__(self, maxx=None):
self._lists = {}
try:
self.maxx = int(maxx)
except (TypeError, ValueError):
self.maxx = 0
def __get__(self, instance, owner):
i = id(instance)
if i in self._lists:
r = self._lists.get(i)
else:
start = random.randint(0, self.maxx)
end = random.randint(start, self.maxx + 1)
r = range(start, end)
self._lists[i] = r
return r
def __set__(self, instance, value):
self._lists[id(instance)] = value
def __del__(self, instance):
i = id(instance)
if i in self._lists:
del self._lists[i]
class PropParams(object):
def __init__(self, prop, maxx=10):
self.prop = prop
print prop
self.prop.maxx = maxx
def setMaxx(self, maxx=10):
self.prop.maxx = maxx
class Object(object):
prop1 = Prop()
prop1_params = PropParams(prop1)
prop2 = Prop()
def __init__(self):
print 'Object created', id(self)
if __name__ == "__main__":
obj = Object()
print obj.prop1, obj.prop2
print obj.prop1, obj.prop2
obj2 = Object()
obj2.prop1 = [1, 2]
obj2.prop2 = [3, 7]
print obj2.prop1, obj2.prop2
print obj2.prop1, obj2.prop2
print type(obj2), getattr(type(obj2), 'prop1')