| 1 |
class RevealAccess(object): |
| 2 |
""" |
| 3 |
A data descriptor that sets and returns values |
| 4 |
normally and prints a message logging their access. |
| 5 |
""" |
| 6 |
def __init__(self, initval=None, name='var'): |
| 7 |
self.val = initval |
| 8 |
self.name = name |
| 9 |
|
| 10 |
def __get__(self, obj, objtype): |
| 11 |
print('Retrieving', self.name) |
| 12 |
return self.val |
| 13 |
|
| 14 |
def __set__(self, obj, val): |
| 15 |
print('Updating', self.name) |
| 16 |
self.val = val |
| 17 |
|
| 18 |
def just_a_method(self): |
| 19 |
pass |
| 20 |
|
| 21 |
class C(object): |
| 22 |
x = RevealAccess(10, 'var "x"') |
| 23 |
|
| 24 |
x |
| 25 |
|
| 26 |
x.just_a_method |
| 27 |
y = 5.0 |
| 28 |
def __init__(self): |
| 29 |
|
| 30 |
self.x |
| 31 |
|
| 32 |
|
| 33 |
self.just_a_method |
| 34 |
|
| 35 |
C.just_a_method |
| 36 |
|
| 37 |
m = C() |
| 38 |
|
| 39 |
m.x |
| 40 |
|
| 41 |
m.y |
| 42 |
|
| 43 |
C.x |
| 44 |
|
| 45 |
|
| 46 |
m.just_a_method |
| 47 |
|
| 48 |
C.just_a_method |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class B(): |
| 54 |
@property |
| 55 |
def r(self): |
| 56 |
return 1 |
| 57 |
@r.setter |
| 58 |
def r(self, value): |
| 59 |
return '' |
| 60 |
def t(self): |
| 61 |
return '' |
| 62 |
p = property(t) |
| 63 |
|
| 64 |
|
| 65 |
B().r(). |
| 66 |
|
| 67 |
B().r |
| 68 |
|
| 69 |
|
| 70 |
B().p |
| 71 |
|
| 72 |
B().p(). |
| 73 |
|
| 74 |
class PropClass(): |
| 75 |
def __init__(self, a): |
| 76 |
self.a = a |
| 77 |
@property |
| 78 |
def ret(self): |
| 79 |
return self.a |
| 80 |
|
| 81 |
@ret.setter |
| 82 |
def ret(self, value): |
| 83 |
return 1.0 |
| 84 |
|
| 85 |
def ret2(self): |
| 86 |
return self.a |
| 87 |
ret2 = property(ret2) |
| 88 |
|
| 89 |
@property |
| 90 |
def nested(self): |
| 91 |
""" causes recusions in properties, should work """ |
| 92 |
return self.ret |
| 93 |
|
| 94 |
@property |
| 95 |
def nested2(self): |
| 96 |
""" causes recusions in properties, should not work """ |
| 97 |
return self.nested2 |
| 98 |
|
| 99 |
@property |
| 100 |
def join1(self): |
| 101 |
""" mutual recusion """ |
| 102 |
return self.join2 |
| 103 |
|
| 104 |
@property |
| 105 |
def join2(self): |
| 106 |
""" mutual recusion """ |
| 107 |
return self.join1 |
| 108 |
|
| 109 |
|
| 110 |
PropClass("").ret |
| 111 |
|
| 112 |
PropClass().ret. |
| 113 |
|
| 114 |
|
| 115 |
PropClass("").ret2 |
| 116 |
|
| 117 |
PropClass().ret2 |
| 118 |
|
| 119 |
|
| 120 |
PropClass(1).nested |
| 121 |
|
| 122 |
PropClass().nested. |
| 123 |
|
| 124 |
|
| 125 |
PropClass(1).nested2 |
| 126 |
|
| 127 |
PropClass().nested2. |
| 128 |
|
| 129 |
|
| 130 |
PropClass(1).join1 |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
class E(object): |
| 136 |
a = '' |
| 137 |
def __init__(self, a): |
| 138 |
self.a = a |
| 139 |
|
| 140 |
def f(x): |
| 141 |
return x |
| 142 |
f = staticmethod(f) |
| 143 |
|
| 144 |
f.__func |
| 145 |
|
| 146 |
@staticmethod |
| 147 |
def g(x): |
| 148 |
return x |
| 149 |
|
| 150 |
def s(cls, x): |
| 151 |
return x |
| 152 |
s = classmethod(s) |
| 153 |
|
| 154 |
@classmethod |
| 155 |
def t(cls, x): |
| 156 |
return x |
| 157 |
|
| 158 |
@classmethod |
| 159 |
def u(cls, x): |
| 160 |
return cls.a |
| 161 |
|
| 162 |
e = E(1) |
| 163 |
|
| 164 |
e.f(1) |
| 165 |
|
| 166 |
E.f(1) |
| 167 |
|
| 168 |
e.g(1) |
| 169 |
|
| 170 |
E.g(1) |
| 171 |
|
| 172 |
|
| 173 |
e.s(1) |
| 174 |
|
| 175 |
E.s(1) |
| 176 |
|
| 177 |
e.t(1) |
| 178 |
|
| 179 |
E.t(1) |
| 180 |
|
| 181 |
|
| 182 |
e.u(1) |
| 183 |
|
| 184 |
E.u(1) |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
from functools import partial |
| 191 |
|
| 192 |
|
| 193 |
class Memoize(): |
| 194 |
def __init__(self, func): |
| 195 |
self.func = func |
| 196 |
|
| 197 |
def __get__(self, obj, objtype): |
| 198 |
if obj is None: |
| 199 |
return self.func |
| 200 |
|
| 201 |
return partial(self, obj) |
| 202 |
|
| 203 |
def __call__(self, *args, **kwargs): |
| 204 |
|
| 205 |
return self.func(*args, **kwargs) |
| 206 |
|
| 207 |
|
| 208 |
class MemoizeTest(): |
| 209 |
def __init__(self, x): |
| 210 |
self.x = x |
| 211 |
|
| 212 |
@Memoize |
| 213 |
def some_func(self): |
| 214 |
return self.x |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
MemoizeTest(10).some_func() |
| 219 |
|
| 220 |
|
| 221 |
MemoizeTest.some_func(MemoizeTest(10.0)) |