Scope and classes

David davidshais at googlemail.com
Wed Aug 19 00:47:09 CEST 2009


Hi all,

I'm trying to understand how scopes work within a class definition.
I'll quickly illustrate with an example. Say I had the following class
definition:

class Abc:
    message = 'Hello World'

    def print_message(self):
        print message

>>> instance = Abc()
>>> instance.print_message()
NameError: global name 'message' not defined

My question is, why? message is not defined in print_message, but it
is defined in the enclosing scope (the class)?

Thanks!



More information about the Python-list mailing list