forked from doctaphred/phrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslots.py
73 lines (61 loc) · 1.43 KB
/
slots.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
69
70
71
72
73
def slotted(*names, **defaults):
"""Convert a class to use slots, with optional default values.
>>> @slotted('x', a=1, b=2)
... class C:
... d = 4
>>> c = C(b=5)
>>> c.a
1
>>> c.b
5
>>> c.d
4
>>> c.a = 6
>>> c.a
6
>>> del c.a
>>> c.a
Traceback (most recent call last):
...
AttributeError: a
>>> c.x
Traceback (most recent call last):
...
AttributeError: x
>>> c.x = 7
>>> c.x
7
>>> del c.x
>>> c.x
Traceback (most recent call last):
...
AttributeError: x
>>> C(x=8).x
8
>>> c.nope = 'negative'
Traceback (most recent call last):
...
AttributeError: 'C' object has no attribute 'nope'
>>> vars(c)
Traceback (most recent call last):
...
TypeError: vars() argument must have __dict__ attribute
>>> C.x
<member 'x' of 'C' objects>
>>> C.a
<member 'a' of 'C' objects>
>>> C.d
4
"""
def slotsify(cls):
def __init__(self, **overrides):
values = self.__defaults__.copy()
values.update(overrides)
for name, value in values.items():
setattr(self, name, value)
attrs = vars(cls).copy()
attrs['__slots__'] = names + tuple(defaults)
attrs['__defaults__'] = defaults
attrs['__init__'] = __init__
return type(cls.__name__, (), attrs)
return slotsify