On 2021-06-21 9:39 p.m., Steven D'Aprano wrote:
Fourth step is that you go ahead and use lists as normal. Whether you use getattr or dot syntax, any extension methods defined in spam.py will show up, as if they were actual list methods.
hasattr(, 'head') # returns True list.tail # returns the spam.tail function object (unbound method)
They're not monkey-patched: other modules don't see that.
Python is a dynamic language. Maybe you're using hasattr/getattr to forward something from A to B. If "other modules don't see that" then this must work as if there were no extension methods in place. So you actually wouldn't want the local load_attr override to apply to those. If you did... well, just call the override directly. If the override was called __opcode_load_attr_impl__ you'd just call __opcode_load_attr_impl__ directly instead of going through getattr. There needs to be an escape hatch for this.
Or you *could* have getattr be special (called by load_attr) and overridable, and builtins.getattr be the escape hatch, but nobody would like that.