[Tutor] global statement?

alan.gauld@bt.com alan.gauld@bt.com
Wed, 15 Aug 2001 14:29:20 +0100


> > Could anyone please tell me WHY function B can't take 
> > globalized dictionary 'a' as a default value?
> > How can I make it work?

>>> def A():
...   global a
...   a = {}
...   # etc/...

> You didn't run A(), so a variable doesn't exist yet. 
> If you run A(), you can then define B like this..

Now that's interesting. I didn't realize global would have 
that effect, I expected a name error on the assignment.
Instead it simply creates a new name in the global namespace.

I still don't like it as a matter of style but I'm interested 
that it does work and in fact creates new variables in the 
imported modules namespace if placed in a module...

Now, is that better than doing

import foo
foo.a = 56

??? I'm not sure.
I'm off to ponder.

Alan G





> 
> -- 
> Cymbaline: intelligent learning mp3 player - python, linux, console.
> get it at: cy.silmarill.org
> 
>