Re: Costly object creation (was : Having Trouble with Scoping Rules)

2006-01-31 Thread Charles Krug
On 2006-01-31, bruno at modulix <[EMAIL PROTECTED]> wrote: > See other answers in this thread for how to solve the UnboundLocalError > problem. > > Now about your *real* problem - which is nothing new -, you may want to > read about some known solutions: > > http://en.wikipedia.org/wiki/Singleton_p

Re: Having Trouble with Scoping Rules

2006-01-31 Thread Charles Krug
On 2006-01-31, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > > def ExpensiveObject(): > global _expensiveObject > if _expensiveObject is None: > _expensiveObject = "A VERY Expensive object" > print "CREATED VERY EXPENSIVE OBJECT" > return _expensiveO

Re: Costly object creation (was : Having Trouble with Scoping Rules)

2006-01-31 Thread bruno at modulix
Charles Krug wrote: > List: > > I've a module that's not doing what I expect. My guess is that I don't > quite understand the scoping rules the way I should. > > I have an object that's costly to create. My thought was to create it > at the module level like this: (snip) > What's the correct

Re: Having Trouble with Scoping Rules

2006-01-30 Thread Fredrik Lundh
Charles Krug wrote: > > 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 > Okay, that

Re: Having Trouble with Scoping Rules

2006-01-30 Thread Charles Krug
On 2006-01-31, Farshid Lashkari <[EMAIL PROTECTED]> wrote: > 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 functio

Re: Having Trouble with Scoping Rules

2006-01-30 Thread Michael Spencer
Charles Krug wrote: > List: > ... > # expensive Object Module > > _expensiveObject = None > def ExpensiveObject(): > > if not(_expensiveObject): > _expensiveObject = "A VERY Expensive object" > > return _expensiveObject > ... > I obviously missed some part of the scoping rules.

Re: Having Trouble with Scoping Rules

2006-01-30 Thread Farshid Lashkari
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():

Having Trouble with Scoping Rules

2006-01-30 Thread Charles Krug
List: I've a module that's not doing what I expect. My guess is that I don't quite understand the scoping rules the way I should. I have an object that's costly to create. My thought was to create it at the module level like this: # expensive Object Module _expensiveObject = None def Expensiv