<div dir="ltr">Had not this been discussed here earlier this year?<br><br>(And despite there being perceived dangers to readability in the long term, was accepted?)<br><br>Here it is on an archive:<br><a href="https://mail.python.org/pipermail/python-ideas/2017-February/044551.html">https://mail.python.org/pipermail/python-ideas/2017-February/044551.html</a><br><br>And anyway - along that discussion, despite dislikng the general idea, I got convinced that<div>creating an outside method that makes "super" or "__class__" work was rather complicated.<br><br>Maybe we could just have a decorator for that, that would properly create the __class__ cell?</div><div><br></div><div><br></div><div> js</div><div> -><-</div><div><div><div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 4 August 2017 at 08:32, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Paul, and welcome!<br>
<span class=""><br>
<br>
On Fri, Aug 04, 2017 at 07:39:56AM +0000, Paul Laos wrote:<br>
> Hi folks<br>
> I was thinking about how sometimes, a function sometimes acts on classes, and<br>
> behaves very much like a method.<br>
<br>
</span>I'm not really sure what you mean by "acts on classes". I can only think<br>
of a function which takes a class as a parameter, and modifies the<br>
class. Like a class decorator. Or possibly a classmethod. But that's not<br>
what you seem to mean below. So I'm not quite certain I understand your<br>
proposal.<br>
<span class=""><br>
<br>
> Adding new methods to classes existing classes<br>
> is currently somewhat difficult,<br>
<br>
</span>If the class is written in Python, it isn't difficult at all, it is<br>
trivially easy. First define your method:<br>
<br>
def method(self, arg):<br>
pass<br>
<br>
<br>
Then inject it onto the class using ordinary attribute assignment:<br>
<br>
TheClass.method = method<br>
<br>
And we're done!<br>
<br>
If the class is a built-in, or otherwise written in C, then "somewhat<br>
difficult" is an understatement. I think it can't be done at all.<br>
<span class=""><br>
<br>
> and having pseudo methods would make that easier.<br>
<br>
</span>I'm not sure that "easier" in this case would be better.<br>
<span class=""><br>
<br>
> Code example: (The syntax can most likely be improved upon)<br>
> def has_vowels(self: str):<br>
> for vowel in ["a", "e,", "i", "o", "u"]:<br>
> if vowel in self: return True<br>
<br>
<br>
</span>How does Python, and for that matter the human reader, know which<br>
class or classes that method is injected into? My guess is it looks at<br>
the annotation. But that's a big change: annotations are currently<br>
guaranteed to have no runtime semantics (apart from being stored in the<br>
function's __annotation__ attribute). I'm not saying that can't be done,<br>
but there may be consequences we haven't thought of.<br>
<br>
If we say dir(str), will "has_vowels" show up?<br>
<br>
How about vars(str)?<br>
<br>
How does this interact with metaclasses?<br>
<span class=""><br>
<br>
<br>
> This allows one to wring `string.has_vowels()` instead of `has_vowels(string)`,<br>
> which would make it easier to read,<br>
<br>
</span>Well that's one opinion.<br>
<span class=""><br>
<br>
> and would make it easier to add<br>
> functionality to existing classes, without having to extend them. This would be<br>
> useful for builtins or imported libraries, so one can fill in "missing" methods.<br>
<br>
</span><a href="http://www.virtuouscode.com/2008/02/23/why-monkeypatching-is-destroying-ruby/" rel="noreferrer" target="_blank">http://www.virtuouscode.com/<wbr>2008/02/23/why-monkeypatching-<wbr>is-destroying-ruby/</a><br>
<br>
I think monkeypatching is great, so long as I'm the only one that does<br>
it. When other people do it, invariably they introduce bugs into my code<br>
by monkeypatching other things I didn't expect to be monkeypatched.<br>
<span class=""><br>
<br>
> * Simple way to extend classes<br>
> * Improves readability<br>
> * Easy to understand<br>
<br>
</span>I'll agree with the first one of those, if by "simple" you mean<br>
"somebody else did all the work to make this syntax do<br>
what I want it to do".<br>
<br>
The work behind the scenes is not likely to be simple: for starters,<br>
allowing monkeypatching of built-ins is likely going to require a rather<br>
big re-design of the Python interpreter.<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Steve<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</div></div></blockquote></div><br></div>