| | 115 | |
| | 116 | class AnObject(object): |
| | 117 | |
| | 118 | def __new__(cls, foo=None, bar=None, baz=None): |
| | 119 | self = object.__new__(cls) |
| | 120 | self.foo = foo |
| | 121 | self.bar = bar |
| | 122 | self.baz = baz |
| | 123 | return self |
| | 124 | |
| | 125 | def __cmp__(self, other): |
| | 126 | return cmp((type(self), self.foo, self.bar, self.baz), |
| | 127 | (type(other), other.foo, other.bar, other.baz)) |
| | 128 | |
| | 129 | def __eq__(self, other): |
| | 130 | return type(self) is type(other) and \ |
| | 131 | (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) |
| | 132 | |
| | 133 | class AnInstance: |
| | 134 | |
| | 135 | def __init__(self, foo=None, bar=None, baz=None): |
| | 136 | self.foo = foo |
| | 137 | self.bar = bar |
| | 138 | self.baz = baz |
| | 139 | |
| | 140 | def __cmp__(self, other): |
| | 141 | return cmp((type(self), self.foo, self.bar, self.baz), |
| | 142 | (type(other), other.foo, other.bar, other.baz)) |
| | 143 | |
| | 144 | def __eq__(self, other): |
| | 145 | return type(self) is type(other) and \ |
| | 146 | (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) |
| | 147 | |
| | 148 | class AState(AnInstance): |
| | 149 | |
| | 150 | def __getstate__(self): |
| | 151 | return { |
| | 152 | '_foo': self.foo, |
| | 153 | '_bar': self.bar, |
| | 154 | '_baz': self.baz, |
| | 155 | } |
| | 156 | |
| | 157 | def __setstate__(self, state): |
| | 158 | self.foo = state['_foo'] |
| | 159 | self.bar = state['_bar'] |
| | 160 | self.baz = state['_baz'] |
| | 161 | |
| | 162 | class ACustomState(AnInstance): |
| | 163 | |
| | 164 | def __getstate__(self): |
| | 165 | return (self.foo, self.bar, self.baz) |
| | 166 | |
| | 167 | def __setstate__(self, state): |
| | 168 | self.foo, self.bar, self.baz = state |
| | 169 | |
| | 170 | class InitArgs(AnInstance): |
| | 171 | |
| | 172 | def __getinitargs__(self): |
| | 173 | return (self.foo, self.bar, self.baz) |
| | 174 | |
| | 175 | def __getstate__(self): |
| | 176 | return {} |
| | 177 | |
| | 178 | class InitArgsWithState(AnInstance): |
| | 179 | |
| | 180 | def __getinitargs__(self): |
| | 181 | return (self.foo, self.bar) |
| | 182 | |
| | 183 | def __getstate__(self): |
| | 184 | return self.baz |
| | 185 | |
| | 186 | def __setstate__(self, state): |
| | 187 | self.baz = state |
| | 188 | |
| | 189 | class NewArgs(AnObject): |
| | 190 | |
| | 191 | def __getnewargs__(self): |
| | 192 | return (self.foo, self.bar, self.baz) |
| | 193 | |
| | 194 | def __getstate__(self): |
| | 195 | return {} |
| | 196 | |
| | 197 | class NewArgsWithState(AnObject): |
| | 198 | |
| | 199 | def __getnewargs__(self): |
| | 200 | return (self.foo, self.bar) |
| | 201 | |
| | 202 | def __getstate__(self): |
| | 203 | return self.baz |
| | 204 | |
| | 205 | def __setstate__(self, state): |
| | 206 | self.baz = state |
| | 207 | |
| | 208 | class Reduce(AnObject): |
| | 209 | |
| | 210 | def __reduce__(self): |
| | 211 | return self.__class__, (self.foo, self.bar, self.baz) |
| | 212 | |
| | 213 | class ReduceWithState(AnObject): |
| | 214 | |
| | 215 | def __reduce__(self): |
| | 216 | return self.__class__, (self.foo, self.bar), self.baz |
| | 217 | |
| | 218 | def __setstate__(self, state): |
| | 219 | self.baz = state |
| | 220 | |
| | 221 | class MyInt(int): |
| | 222 | |
| | 223 | def __eq__(self, other): |
| | 224 | return type(self) is type(other) and int(self) == int(other) |
| | 225 | |
| | 226 | class MyList(list): |
| | 227 | |
| | 228 | def __init__(self, n=1): |
| | 229 | self.extend([None]*n) |
| | 230 | |
| | 231 | def __eq__(self, other): |
| | 232 | return type(self) is type(other) and list(self) == list(other) |
| | 233 | |
| | 234 | class MyDict(dict): |
| | 235 | |
| | 236 | def __init__(self, n=1): |
| | 237 | for k in range(n): |
| | 238 | self[k] = None |
| | 239 | |
| | 240 | def __eq__(self, other): |
| | 241 | return type(self) is type(other) and dict(self) == dict(other) |