Having Trouble with Scoping Rules

Farshid Lashkari flashkNO at SPAMgmail.com
Mon Jan 30 22:09:35 EST 2006


You need to declare _expensiveObject as global inside your function. 
Whenever you assign something to a variable that resides in the global 
scope inside a function, you need to declare it as global at the 
beginning of the function. So your function should look like this

def ExpensiveObject():
     global _expensiveObject
     if not(_expensiveObject):
         _expensiveObject = "A VERY Expensive object"

     return _expensiveObject

The documentation will no doubtedly explain it better than I have

-Farshid



More information about the Python-list mailing list