Newbie scopes question

Gonçalo Rodrigues op73418 at mail.telepac.pt
Wed Feb 27 14:37:35 EST 2002


As far as i understand, a class statement opens a new scope.

In the following piece of code

class Outer:
    attribute = "The outer class attribute"

    def __init__(self):
        try:
            print attribute
        except NameError:
            print "Attribute not defined!"


def Outer_function():
    local_var = "Local var of the outer function"

    def inner_function():
        print local_var

    inner_function()

Outer_function()
a = Outer()

What we get printed is

Local var of the outer function
Attribute not defined!

This means that function and class scopes behave differently as fas as
nesting goes. Could someone explain why is this?

Thanx,
Gonçalo Rodrigues



More information about the Python-list mailing list