Re: Having Trouble with Scoping Rules
- From: Charles Krug <cdkrug@xxxxxxx>
- Date: Tue, 31 Jan 2006 06:39:59 -0600
On 2006-01-31, Fredrik Lundh <fredrik@xxxxxxxxxxxxxx> wrote:
>
> def ExpensiveObject():
> global _expensiveObject
> if _expensiveObject is None:
> _expensiveObject = "A VERY Expensive object"
> print "CREATED VERY EXPENSIVE OBJECT"
> return _expensiveObject
>
> if __name__ == "__main__":
> print _expensiveObject
> print ExpensiveObject()
> print ExpensiveObject()
> print ExpensiveObject()
>
> which prints
>
> None
> CREATED VERY EXPENSIVE OBJECT
> A VERY Expensive object
> A VERY Expensive object
> A VERY Expensive object
>
> and works perfectly fine if you import it from another module:
>
Ah, I had another error that broke it on import. Testing with a
dictionary showed me the "creates another object" error. Using a string
constant for that was just a "between the ears" problem that would have
shown me the error much quicker.
Thanks.
.
- References:
- Having Trouble with Scoping Rules
- From: Charles Krug
- Re: Having Trouble with Scoping Rules
- From: Farshid Lashkari
- Re: Having Trouble with Scoping Rules
- From: Charles Krug
- Re: Having Trouble with Scoping Rules
- From: Fredrik Lundh
- Having Trouble with Scoping Rules
- Prev by Date: Re: backreference in regexp
- Next by Date: Help with XML-SAX program ... it's driving me nuts ...
- Previous by thread: Re: Having Trouble with Scoping Rules
- Next by thread: Re: Having Trouble with Scoping Rules
- Index(es):