A scoping question

It's me itsme at yahoo.com
Tue Dec 28 14:34:36 EST 2004


This must be another newbie gotchas.

Consider the following silly code, let say I have the following in file1.py:

#=============
import file2
global myBaseClass
myBaseClass = file2.BaseClass()
myBaseClass.AddChild(file2.NextClass())
#=============

and in file2.py, I have:

#=============
global myBaseClass
class BaseClass:
    def __init__(self):
        self.MyChilds = []
     ...
    def AddChild(NewChild):
        self.MyChilds.append(NewChild)
    ...
class NextClass:
    def __init__(self):
        for eachChild in myBaseClass.MyChilds:  # <- ERROR
            ...
#=============

When I run this, Python complains that myBaseClass is undefined in the last
line above.

What am I doing wrong?  (Yes, I know I am thinking too much in C).  I
thought the global declaration would have been sufficient but it's obviously
not.

Thanks,





More information about the Python-list mailing list