[Python-ideas] Mitigating 'self.' Method Pollution
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
> > 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
It's not *exactly* dynamic scope, hence "resembling."
> In dynamic scoping, the scope of variables depends on the call chain.
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
> > 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
> s = "hello world"
> def 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...
More information about the Python-ideas