TIL: Extending operand usage in Python
If you were to write some Python like this…
class Person():
pass
your_mom = Person()
assert your_mom * 100 == '100 people'
you’d get a TypeError
That’s to be expected. Your mom doesn’t know how to handle the *
operator.
You can teach her, though.
class Person():
def __mul__(self, i: int) -> str:
return f'{i} people'
your_mom = Person()
assert your_mom * 100 == '100 people'