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