On Tue, Jun 22, 2021 at 9:56 PM Steven D'Aprano email@example.com wrote:
On Tue, Jun 22, 2021 at 09:12:53PM +1000, Chris Angelico wrote:
The must be no semantic difference between:
regardless of whether `method` is a regular method or an extension method.
And this is a problem.
If its a problem for getattr, it is a problem for dot syntax, because they are essentially the same thing.
Ahh but that is precisely the problem.
How is getattr defined?
The same as it is defined now, except with some minor tweaks to support extension methods.
Do those tweaks include reaching back into the module that called it? How magical will it be?
Is it counted as being in the current module?
`getattr`? No, that's a builtin. You can shadow it or delete it if you want, it's just a public API to the underlying functionality built into the interpreter. Dot syntax won't be affected.
Let me clarify then.
We shall assume for the moment that the builtins module does not have any extension methods registered. (I suppose it could, but then you get all the action-at-a-distance of monkey-patching AND the problems of extension methods, so I would hope people don't do this.) This means that the getattr() function, being a perfectly straight-forward function, is not going to see any extension methods.
# whatever the actual syntax is @extend(list) def in_order(self): return sorted(self)
stuff = [1, 5, 2] stuff.in_order() # == [1, 2, 5] getattr(stuff, "in_order")() # AttributeError
Does the getattr function see the extension methods? If so, which? If not, how can getattr return the same thing as attribute lookup does?
How do you inform getattr of which extension methods it should be looking at?
And what about this?
f = functools.partial(getattr, stuff) f("in_order")
NOW which extension methods should apply? Those registered here? Those registered in the builtins? Those registered in functools?
Yes, monkey-patching *is* cleaner, because the object is the same object no matter how you look it up.
(Oh, and another wrinkle, although a small one: Code objects would need to keep track of their modules. Currently functions do, but code objects don't. But that seems unlikely to introduce further complications.)