"Everytime you add two objects in Python you have the possibility of hitting a system defined add, or __add__ or __getattr__ or __getattribute__, or __radd__, or __getattr__ (looking for __radd__), etc"
That's pretty much a solved problem. Lookup the method once, cache it for next time, deoptimize if you're wrong or the definition of the class changes. If it keeps changing build up a larger cache.
Comments
"Everytime you add two objects in Python you have the possibility of hitting a system defined add, or __add__ or __getattr__ or __getattribute__, or __radd__, or __getattr__ (looking for __radd__), etc"
That's pretty much a solved problem. Lookup the method once, cache it for next time, deoptimize if you're wrong or the definition of the class changes. If it keeps changing build up a larger cache.
Ruby has exactly the same behaviour, and we can still get it down to really simple machine code http://www.chrisseaton.com/rubytruffle/how-method-dispatch-w....