Re: Having Trouble with Scoping Rules
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Having Trouble with Scoping Rules

From: Charles Krug <cdkrug@aol.com>
Date: Tue Jan 31 2006 - 13:39:59 CET

On 2006-01-31, Fredrik Lundh <fredrik@pythonware.com> 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.
Received on Tue Feb 7 20:20:05 2006