On 2021-06-22 13:09, Soni L. wrote:
Think about it like this, extension methods give you the ability to make
imported functions that look like this:
foo(bar, baz)
look like this instead:
bar.foo(baz)
That's all there is to them. They're just a lie to change how you
read/write the code. Some languages have an whole operator that has a
similar function, where something like bar->foo(baz) is sugar for
foo(bar, baz). The OP doesn't specify any particular mechanism for
extension methods, so e.g. making the dot operator be implemented by a
local function in the module, which delegates to the current attribute
lookup mechanism by default, would be perfectly acceptable. It's like
deprecating the existing dot operator and introducing a completely
different one that has nothing to do with attribute lookup!
Okay, if that's the case, then I just think it's a bad idea. :-)
We already have a definition for what bar.foo does, and it's totally
under the control of the bar object (via the
__getattr__/__getattribute__ mechanism). The idea that other things
would be able to hook in there does not appeal to me at all.
I don't really understand why you would want such a thing, to be
honest. I feel it would make code way more difficult to reason about,
as it would break locality constraints every which way. Now every time
you see`bar.foo` you would have to think about all kinds of other
modules that may be hooking in and adding their own complications.
What's the point?
Mostly the whole benefit of the dot notation is that it specifies a
locally constrained relationship between the object and the attribute:
you know that bar.foo does what bar decides, and no one else gets any
say (unless bar asks for their opinion, e.g. by consulting global
variables or whatever). If we want to write foo(bar, baz). . . well, we
can just do that! What you're describing would just make existing
attribute usages harder to understand while only "adding" something we
can already do quite straightforwardly.
Imagine a similar proposal for other syntax. Suppose that in any
module I could define a function called operator_add and then other
modules could "import" this "extension" so that every use of the +
operator would somehow hook into this operator_add function. So now
every time you do 2 + 2 you might be invoking some extension behavior.
In my view that is unambiguously a road to madness, and as far as I can
tell the extension mechanism you're proposing is equally ill-advised.
--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no
path, and leave a trail."
--author unknown
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/4T35JHWPGIPWONX4PD7YPWEA2RXROSZI/
Code of Conduct: http://python.org/psf/codeofconduct/