Hi William,

Thanks for the description of C# extension methods, but I think that 
like Britons and Americans, we're in danger of being divided by a common 
language. (To paraphrase Churchill.)

On Sun, Jun 20, 2021 at 10:56:37PM -0000, William Pickard wrote:

> While IDK about Kotlin's extension methods, I do know that C# (A 
> highly popular programming language) also has extension methods so 
> I'll talk about those.
> 
> In C#, extension methods are plain static functions defined in a plain 
> static class, the only key difference between normal static methods 
> and these extension methods is that the first argument is prefixed 
> with the "this" keyword.

I'm unsure what you mean by "static functions" and whether they are the 
same thing as "static methods". I believe that a static method is 
something different in Python and C#.

When you say *prefixed by*, surely you don't actually mean a literal 
prefix? Using Python syntax:

    # I want the first argument to be called "param".
    def extension(thisparam):

because that would be silly *wink* so I guess that you mean this:

    def extension(this, param):

except that in Python, we spell it "self" rather than "this", and it is 
not a keyword. So as far as the interpreter is concerned, whether 
spelled as "this", "self" or something else, that's just a regular 
function that takes two parameters, neither of which has any special or 
pre-defined meaning.


> You can invoke extension methods as if they were defined on the type 
> they're extending without actually changing the type, the only 
> requirement is that the namespace that the class containing the method 
> is used (via "using").

Let me see if I can interpret that, in Python terms.

Suppose we have a module X.py which defines a list extension method 
imaginatively called "extension". In order to use that method from my 
module, I would have to say:

    using X

first, after which:

    hasattr(list, 'extension')

would return True. Otherwise, it would continue to return False. So 
modules have to opt-in to use the extension method, rather than having 
the methods foist on them as in monkey-patching.

Am I close?

I think this sounds much more promising, since it avoids the downsides 
of monkey-patching. The problem is that in a language like C#, and I 
presume Koitlin, methods are resolved at compile time, but in Python 
they are resolved at runtime.

So `using` would have to be some sort of registration system, which 
would require every attribute lookup to go through the registration 
system looking for only those extension methods which are being used by 
the current module.

I expect that would be slow and complex. But maybe I'm just not clever 
enough to think of an efficient way of handling it :-(


-- 
Steve
_______________________________________________
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/3WSJPQGYRETFNVKNMIAPQ7DB52JD4ZDQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to