[Python-ideas] Mitigating 'self.' Method Pollution

Paul Sokolovsky pmiscml at gmail.com
Sat Jul 11 02:01:23 CEST 2015


Hello,

On Fri, 10 Jul 2015 15:31:28 -0700
Michael Hewitt <michael at hewitts.us> wrote:

> Last night I made a post to the neopythonic blog proposing a Python
> 3.x feature that Guido asked me to forward to this alias.  For the
> full background, see the link to my post below.  For brevity, I will
> simply submit the proposal here.  The specific problem I am
> addressing is the pollution of Python methods by 'self.' to reference
> fields.  Here is the proposal:
> 
> The name of the first parameter to a method can be used to scope
> subsequent variable references similar to the behavior of 'global'.
> 
> 
> Here are some examples:
> 
> class Foo:
> 
> def method_a(self)
> 
> self x # subsequent 'x' references are scoped to 'self'
> 
> x = 5 # same as self.x = 5
> 
> def method_b(this)
> 
> this x, y # subsequent 'x' & 'y' refs are scoped to 'this'
> 
> x = y # same as this.x = this.y
> 
> def method_c(its)
> 
> its.x = 5 # still works just like it used to
> 
> 
> This suggestion is fully backward compatible with existing Python
> code, but would eliminate the need to pollute future Python methods
> with copious 'self.' prefixes, thereby improving both readability and
> maintainabilty.

Even the language which is bag of warts - JavaScript - has essentially
banned usage of its "with" statement:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with
. And the reason for this is (as the red box on that page says) that it
interferes with readability, maintainability, and performance. And yet
you propose to add essentially the same to Python.

If you have troubles explaining why "self" is needed to kids, I'd
suggest going along the lines of why there's capital letter at the
start of sentence, full stop at the end, and wh hmn lnggs hv vwls, vn
thgh ts pssbl t rd wtht thm.

> 
> Thank you for your consideration.
> 
> Michael Hewitt
> 
> Original Post:
> http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html



-- 
Best regards,
 Paul                          mailto:pmiscml at gmail.com


More information about the Python-ideas mailing list