<p dir="ltr">The proposed version feels difficult to read, bug prone, and overwhelmingly non-Pythonic.</p>
<p dir="ltr">The door for C++ is right down the hall.... ☺</p>
<div class="gmail_quote">On Jul 11, 2015 12:26 AM, "Michael Hewitt" <<a href="mailto:michael@hewitts.us">michael@hewitts.us</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Let's compare two versions of a method taken from some code that my 11 year old son wrote yesterday:<span></span></div><div><br></div><div><b><u>Current</u></b></div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>global height</div><div>def keep_moving_gravity(self):</div><div>    self.y += self.gravity</div><div>    self.y = max(self.y, 0)</div><div>    self.y = min(self.y, height - 1)</div></blockquote><div><br></div><div><b><u>Proposed</u></b></div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>global height</div><div>def keep_moving_gravity(self):</div><div>    self y, gravity</div><div>    y += gravity</div><div>    y = max(y, 0)</div><div>    y = min(y, height - 1)</div></div></blockquote><div><br></div><div>Is anyone actually going to argue that the first version is cleaner and more readable than the second?  All I see when I read the first is 'self', 'self', 'self' -- my son's exact words to me last night.</div><div><br></div><div>As far as maintainability, the author of the first version must repeatedly make the same decisions over and over regarding how to scope each variable reference as he/she types 'y', 'gravity', and 'height'. The author of the second code makes these decisions exactly once at the top of the method and then is free to refer to each variable naturally without the mental overhead of prefixing each 'y' and 'gravity' with 'self.', but God forbid - not 'height'.  I can tell you that the mental overhead of this is taxing my son & is the cause of many painful mistakes -- just forgetting a single 'self.' prefix on one of the above field references can waste a significant amount of time.</div><div><br></div><div>As far as static analysis tools, this should honestly not be a lot of extra work, since the tools must already handle 'global' in a very similar fashion.<br></div><div><br></div><div>If the 'self.' prefix really does make code clearer, then we should do away with the 'global' scope declaration as well as automatic local scoping and require prefixing of all Python variables with 'self.', 'global.' or 'local.'.  My mind becomes numb thinking about writing such code.  To me, the existence of the keyword 'global' for automatically scoping subsequent variable references is a strong argument that a similar 'self' scoping mechanism is called for as well.</div><div><br></div><div>And, for folks who still prefer to prefix all their field references with 'self.', the proposal in no way prevents them from doing so.  It merely allows the rest of us to be a bit less wordy and more pithy in our code.</div><div><br></div><div>Mike<br><br>On Friday, July 10, 2015, Mark Lawrence <<a href="mailto:breamoreboy@yahoo.co.uk" target="_blank">breamoreboy@yahoo.co.uk</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">On 10/07/2015 23:31, Michael Hewitt wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Last night I made a post to the neopythonic blog proposing a Python 3.x<br>
feature that Guido asked me to forward to this alias.  For the full<br>
background, see the link to my post below.  For brevity, I will simply<br>
submit the proposal here.  The specific problem I am addressing is the<br>
pollution of Python methods by 'self.' to reference fields.  Here is the<br>
proposal:<br>
<br>
    The name of the first parameter to a method can be used to scope<br>
    subsequent variable references similar to the behavior of 'global'.<br>
<br>
<br>
Here are some examples:<br>
<br>
    class Foo:<br>
<br>
        def method_a(self)<br>
<br>
            self x # subsequent 'x' references are scoped to 'self'<br>
<br>
            x = 5 # same as self.x = 5<br>
<br>
        def method_b(this)<br>
<br>
            this x, y # subsequent 'x' & 'y' refs are scoped to 'this'<br>
<br>
            x = y # same as this.x = this.y<br>
<br>
        def method_c(its)<br>
<br>
            its.x = 5 # still works just like it used to<br>
<br>
<br>
This suggestion is fully backward compatible with existing Python code,<br>
but would eliminate the need to pollute future Python methods with<br>
copious 'self.' prefixes, thereby improving both readability and<br>
maintainabilty.<br>
</blockquote>
<br>
I disagree completely.  When I see:-<br>
<br>
self.x = 1<br>
<br>
I currently know exactly what I'm looking at.  All I see with this proposal is more work for my MKI eyeballs, which are already knackered, and more work for the people who maintain our static analysis tools, as you can still forget to properly scope your variables.  So -1.<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Thank you for your consideration.<br>
<br>
Michael Hewitt<br>
<br>
Original Post:<br>
<a href="http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html" target="_blank">http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html</a><br>
<br>
</blockquote>
<br>
<br>
<br>
-- <br>
My fellow Pythonistas, ask not what our language can do for you, ask<br>
what you can do for our language.<br>
<br>
Mark Lawrence<br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a>Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>
</div>
<br>_______________________________________________<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/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div>