Create operator method.
(name, func)
| 115 | |
| 116 | |
| 117 | def _make_operator(name, func): |
| 118 | """ |
| 119 | Create operator method. |
| 120 | """ |
| 121 | |
| 122 | def method(self, other): |
| 123 | if not self._is_comparable_to(other): |
| 124 | return NotImplemented |
| 125 | |
| 126 | result = func(self.value, other.value) |
| 127 | if result is NotImplemented: |
| 128 | return NotImplemented |
| 129 | |
| 130 | return result |
| 131 | |
| 132 | method.__name__ = "__%s__" % (name,) |
| 133 | method.__doc__ = "Return a %s b. Computed by attrs." % ( |
| 134 | _operation_names[name], |
| 135 | ) |
| 136 | |
| 137 | return method |
| 138 | |
| 139 | |
| 140 | def _is_comparable_to(self, other): |