c++ template-like invocation in python

so the other day for whatever reason i really wanted to invoke python functions as if they were c++ template functions… like so: template <int x> int add(int y) { return x + y; } printf("%d\n", add<5>(3)); if we ignore semantics, add<5>(3) seems to be a valid python expression – roughly translating to add.__lt__(5).__gt__(3) i quickly whipped up a prototype: class TemplateAdd: def __init__(self): self.x = None def __lt__(self, x): self.x = x return self def __gt__(self, y): return self.x + y add = TemplateAdd() print(add<5>(3)) but somehow it prints True… thanks, chained comparisons. the expression add<5>(3) evaluates to add.__lt__(5) and 5 > 3, and with no way to overload the and operator, the dream is dead unless i come to terms with the deranged syntax of (add<5)>3 (doesnt <5)>3 kind of look like a fish?) ...

January 29, 2025