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

Steven D'Aprano steve at pearwood.info
Sat Jul 11 19:21:16 CEST 2015

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 

In dynamic scoping, the scope of variables depends on the call chain. So 
if you write:

    def spam():

and then *call* it from function eggs(), spam gets x from the scope 
of eggs. This is nothing like Michael's proposal.

> I.e. you'd like bare variables to be (sometimes) scoped to the namespace of
> the instance eventually created somewhere outside of the class definition
> (quite likely only created under runtime dependent conditions).

self is always an instance created outside of the class definition, 
under runtime dependent conditions. You are describing every instance of 
every class. You can't create an instance of class Spam *inside the 
class definition* of Spam, because the class doesn't exist yet:

class Spam:
    x = Spam()  # doesn't work

> In
> 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 

s = "hello world"
def func():



More information about the Python-ideas mailing list