| 1 | class InstanceMethod(object): |
| 2 | "Like types.InstanceMethod, but with a reasonable (structural) equality." |
| 3 | |
| 4 | def __init__(self, im_func, im_self, im_class): |
| 5 | self.im_func = im_func |
| 6 | self.im_self = im_self |
| 7 | self.im_class = im_class |
| 8 | |
| 9 | def __call__(self, *args, **kwds): |
| 10 | firstarg = self.im_self |
| 11 | if firstarg is None: |
| 12 | if not args or not isinstance(args[0], self.im_class): |
| 13 | raise TypeError( |
| 14 | "must be called with %r instance as first argument" % ( |
| 15 | self.im_class,)) |
| 16 | firstarg = args[0] |
| 17 | args = args[1:] |
| 18 | return self.im_func(firstarg, *args, **kwds) |
| 19 | |
| 20 | def __eq__(self, other): |
| 21 | return isinstance(other, InstanceMethod) and ( |
| 22 | self.im_func == other.im_func and |
| 23 | self.im_self == other.im_self) |
| 24 | |
| 25 | def __ne__(self, other): |
| 26 | return not self.__eq__(other) |
| 27 | |
| 28 | def __hash__(self): |
| 29 | return hash((self.im_func, self.im_self)) |