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

M.-A. Lemburg mal at egenix.com
Sat Jul 11 11:54:53 CEST 2015


On 11.07.2015 00:31, Michael Hewitt 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

-1 on this.

How would I know that x is "bound" to self later on in the method
without scanning the whole method body for a possible reference
to self.x ?

As Python programmer, you default to assume that x = 5 refers
to a local variable assignment (globals aren't used much, and
when they are, the global definition is usually written as first
statement in a function/method).

If we'd have something like:

with self:
     x = 5
     y = 6

as in Pascal, this would be explicit and it's also clear that
there's special scoping going on in the body of the with
statement block.

I still wouldn't like this much, but at least it follows
explicit is better than implicit.

Overall, I don't believe much in keystroke optimizations -
editors provide all the help you need these days to avoid
typing too much, while still allowing you to use descriptive
identifiers throughout your program. And that results in
much better readability than any scoping tricks ;-)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> mxODBC Plone/Zope Database Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/


More information about the Python-ideas mailing list