Declare a variable global

yinglcs at gmail.com yinglcs at gmail.com
Mon Feb 19 12:04:19 EST 2007


Hi,

I have the following code:

colorIndex = 0;

def test():
     print colorIndex;

This won't work.   But it works if i do this:

colorIndex = 0;

def test():
     global colorIndex;
     print colorIndex;

My question is why do I have to explicit declaring 'global' for
'colorIndex'?  Can't python automatically looks in the global scope
when i access 'colorIndex' in my function 'test()'?

Thank you.




More information about the Python-list mailing list