Need help with Python scoping rules

Carl Banks pavlovevidence at
Wed Aug 26 16:58:04 CEST 2009

On Aug 26, 7:09 am, kj <no.em... at> wrote:
> In <16b72319-8023-471c-ba40-8025aa6d4... at> Carl Banks <pavlovevide... at> writes:
> >> First, one of the goals of OO is encapsulation, not only at the
> >> level of instances, but also at the level of classes. =A0Your comment
> >> suggests that Python does not fully support class-level encapsulation.
> >I can't answer this, I don't even know what you are talking about.
> Yes, you do.  As I said in another post, Python offers some degree
> of class-level encapsulation (e.g. class variables).  But it is
> limited by the fact that these class-encapsulated elements can't
> always be accessed from within the class itself, which is kind of
> silly.

Nope, you're wrong.  Class variables are accessible wherever a class
exists.  The apparent silliness of this is because you are confusing
classes with class statements.

You know that the class isn't created until the class statement exits,
don't you?

Yeah, it's a little surprising that you can't access class scope from
a function, but that has nothing to do with encapsulation.

> >1. One of the key aspects of Python's design is that attributes must
> >be accessed explicitly with dot notation.  Accessing class scopes from
> >nested functions would (seemingly) allow access to class attributes
> >without the dotted notation.  Therefore it is not allowed.
> It would be trivial to define a keyword (e.g. this, or if you
> prefer, __this__), valid only within a class statement, and that
> the interpreter would recognize as "the current class", even before
> this class is full defined.

Your solution to this "problem" is to add a keyword to Python.
Laughable.  (BTW, it's not trivial.)

Carl Banks

More information about the Python-list mailing list