I got the hint that it's possible to overwrite Python built-in functions to be used only for a small code segment.
Example:
import contextlib
@contextlib.contextmanager
def tuple_builtins_handler():
original_eq = tuple.__eq__
try:
tuple.__eq__ = my_eq # defined elsewhere
yield
finally:
tuple.__eq__ = original_eq
I get a
TypeError: cannot set '__eq__' attribute of immutable type 'tuple'.
Is there any alternative?
I could define a class but that seems not to be the right way.
- I want to restrict 'my' tuples to two elements of type integer, so in the strict oo-sense this is not what is meant bei inheritance or subclassing.
- I want to use this in some sections of my code, so not for public usage, and therefore there's no need for using a new class which sems to be a kind of overkill.
- I still think that overwriting (and restoring) the built-in functions is the least expensive way.