Return a callable object that calls the given method on its operand. Unlike the builtin `operator.methodcaller`, instances of this class are cached and arguments are passed at call time instead of build time.
| 1204 | |
| 1205 | |
| 1206 | class methodcaller: |
| 1207 | """ |
| 1208 | Return a callable object that calls the given method on its operand. |
| 1209 | |
| 1210 | Unlike the builtin `operator.methodcaller`, instances of this class are |
| 1211 | cached and arguments are passed at call time instead of build time. |
| 1212 | """ |
| 1213 | |
| 1214 | __slots__ = ("method",) |
| 1215 | method: str |
| 1216 | |
| 1217 | @property |
| 1218 | def func(self) -> str: |
| 1219 | # For `funcname` to work |
| 1220 | return self.method |
| 1221 | |
| 1222 | def __new__(cls, method: str): |
| 1223 | try: |
| 1224 | return _method_cache[method] |
| 1225 | except KeyError: |
| 1226 | self = object.__new__(cls) |
| 1227 | self.method = method |
| 1228 | _method_cache[method] = self |
| 1229 | return self |
| 1230 | |
| 1231 | def __call__(self, __obj, *args, **kwargs): |
| 1232 | return getattr(__obj, self.method)(*args, **kwargs) |
| 1233 | |
| 1234 | def __reduce__(self): |
| 1235 | return (methodcaller, (self.method,)) |
| 1236 | |
| 1237 | def __str__(self): |
| 1238 | return f"<{self.__class__.__name__}: {self.method}>" |
| 1239 | |
| 1240 | __repr__ = __str__ |
| 1241 | |
| 1242 | |
| 1243 | class itemgetter: |
no outgoing calls
searching dependent graphs…