[Tutor] global statement?

Andrei Kulakov ak@silmarill.org
Wed, 15 Aug 2001 02:13:08 -0400


On Wed, Aug 15, 2001 at 03:10:55PM +0900, Haiyang wrote:
> 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 = {}
> ...          a['happyface']=1
> ...          a['sadface']=99
> ...
> >>> def B(b=a):
> ...          for item in b:
> ...               print b
> ...
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> NameError: name 'a' is not defined

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

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