Dumb newbie back in shell

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Dec 11 03:47:48 EST 2007


MartinRinehart at gmail.com a écrit :
(Martin, please, don't top post - fixed)
> 
> Peter Otten wrote:
>> MartinRinehart wrote:
>>
>>> However, here's the little tester I wrote:
>>>
>>> # t.py - testing
>>>
>>> global g
>>> g = 'global var, here'
>>>
>>> def f():
>>>     print g
>>>
>>> f()
>>>
>>> It prints 'global var, here,' not an error message. Wassup?
>> Try it again with a modified f():
>>
>> def f():
>>     print g
>>     g = 42
>>
>> In Python variables that are assigned to in a function are
>> function-local by default.
>>
 >
 > question is, why did the first one work?

The answer is just above.

 > In my real code I've got
 > module-level vars and an error msg trying to use them in a function.
 > In my test example I've got them accessed from within a function w/o
 > error message.
 >
 > I am confused.


In the fist you're not rebinding the g. And FWIW, this is a FAQ.




More information about the Python-list mailing list