I absolutely love using operator overrides in low-level objects because it can make the caller's use case WAY more intuitive. There are certainly some pitfalls though.
- If you're overriding
__getitem__
and__setitem__
to make an object more dictionary-like, make sure to also override__iter__
OR__contains__
since they're called to handle membership tests likein
andnot in
. (ref: https://docs.python.org/3/reference/expressions.html#membership-test-operations)
Enums can be a little tedious in Python, but there are a few things I try to do to streamline usage: