Questions about no more nested imports

Matt mbarnett at ualberta.ca
Sun Mar 11 19:01:28 EST 2001


In article <98h1k4$ai4$0 at 216.39.170.247>, whisper at oz.net (Dave LeBlanc) 
wrote:


> Ok, read the PEP (more or less again). It's not all that clear, but
> does answer my original questions.
>
> Could someone please clarify the scoping wrt to classes? I would
> expect that the following code would refer to the variable at the
> class scope, but it's not clear to me if it would do so:
>
> class foo
>  bar = 1
>  def fee
>   print bar

AFAIK that won't work in the new scoping rules. Specifically see the
discussion section at http://python.sourceforge.net/peps/pep-0227.html , but
it sounds like bar is unresolved:

    The
    name resolution rules are typical for statically scoped languages,
    with three primary exceptions:

        - Names in class scope are not accessible.

    <snip>

    If a name binding
    operation occurs in a class definition, it creates an attribute on
    the resulting class object.  To access this variable in a method,
    or in a function nested within a method, an attribute reference
    must be used, either via self or via the class name.


In your example fee sounds like it needs to be defined along the lines of:

class foo:
    bar = 1
    def fee(self):
        print self.bar

I could be wrong about this (I don't have access to a 2.1 binary), but
that's how it reads to me.




More information about the Python-list mailing list