Provide getitem syntax for functions >>> def inc(x): ... return x + 1 >>> I = IndexCallable(inc) >>> I[3] 4
| 442 | |
| 443 | |
| 444 | class IndexCallable: |
| 445 | """Provide getitem syntax for functions |
| 446 | |
| 447 | >>> def inc(x): |
| 448 | ... return x + 1 |
| 449 | |
| 450 | >>> I = IndexCallable(inc) |
| 451 | >>> I[3] |
| 452 | 4 |
| 453 | """ |
| 454 | |
| 455 | __slots__ = ("fn",) |
| 456 | |
| 457 | def __init__(self, fn): |
| 458 | self.fn = fn |
| 459 | |
| 460 | def __getitem__(self, key): |
| 461 | return self.fn(key) |
| 462 | |
| 463 | |
| 464 | @contextmanager |
no outgoing calls
no test coverage detected