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

David Mertz mertz at gnosis.cx
Sat Jul 11 21:25:44 CEST 2015

On Sat, Jul 11, 2015 at 10:21 AM, Steven D'Aprano <steve at pearwood.info>

> On Sat, Jul 11, 2015 at 08:23:39AM -0700, David Mertz wrote:
> > You are confusing scoping with attribute access. 'self' is not a lexical
> > scope. In a way, if you squint just right, it resembles a dynamic scope
> (or
> > would under your proposal). But Python isn't elisp, and we don't want to
> > have dynamic scoping.
> I believe you are misunderstanding either dynamic scoping or Michael's
> proposal.

It's not *exactly* dynamic scope, hence "resembling."

> In dynamic scoping, the scope of variables depends on the call chain.

class Foo():
    def __init__(self, val):
        self.val = val

if random() < .5:
    a, b = Foo(1), Foo(2)
    a, b = Foo(2), Foo(1)

What's the value of the attribute .val in the namespace 'a' after this code

You're right that this is always true of instances, that they are
*dynamically* created at runtime.  C.f. "resembling".

I didn't claim that  the proposal changes the scoping *semantics* of
Python, per se.  But right now, we have this convenient *syntactic* marker
that .val is going to live in the namespace whose place is held in the
definition by the name 'self'.  Under his proposal, readability suffers
because we no longer have that marker on the variable itself.

> > contrast, actual Python is far simpler... all variables are lexically
> local
> > when defined, unless explicitly declared to have a different and specific
> > *lexical* scope.
> That's not correct. If it were, this would fail with NameError or
> UnboundLocalError:
> s = "hello world"
> def func():
>     print(s)
> func()
Take a look at what I wrote.  's' is *defined* in the enclosing scope of
the function definition (i.e. probably the module scope; although perhaps
your code is copied from some other nested scope, which would make 's'
nonlocal rather than global).  Module scope is also "lexical locality".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150711/9bff4534/attachment-0001.html>

More information about the Python-ideas mailing list