please help explain this result

2010-10-17 Thread Yingjie Lan
Hi, I played with an example related to namespaces/scoping. The result is a little confusing: a=1 def f(): a = a + 1 return a f() I suppose I will get 2 ( 'a' is redefined as a local variable, whose value is obtained by the value of the global variable 'a' plus 1). But

Re: please help explain this result

2010-10-17 Thread Nobody
On Sun, 17 Oct 2010 03:58:21 -0700, Yingjie Lan wrote: I played with an example related to namespaces/scoping. The result is a little confusing: a=1 def f(): a = a + 1 return a f() UnboundLocalError: local variable 'a' referenced before assignment If you want to modify a

Re: please help explain this result

2010-10-17 Thread Steven D'Aprano
On Sun, 17 Oct 2010 03:58:21 -0700, Yingjie Lan wrote: Hi, I played with an example related to namespaces/scoping. The result is a little confusing: [snip example of UnboundLocalError] Python's scoping rules are such that if you assign to a variable inside a function, it is treated as a

Re: please help explain this result

2010-10-17 Thread Yingjie Lan
From: Nobody nob...@nowhere.com The determination of local or global is made when the def statement is executed, not when the function is called. Thanks a lot for your reply, which is of great help! So, I assume that when the 'def' is executed, any name occurred will be categorized as

Re: please help explain this result

2010-10-17 Thread Yingjie Lan
--- On Sun, 10/17/10, Steven D'Aprano st...@remove-this-cybersource.com.au wrote: (1) If you assign to a variable *anywhere* in the function, it is a local *everywhere* in the function. There is no way to have a variable refer to a local in some places of a function and a global in

Re: please help explain this result

2010-10-17 Thread Paul Kölle
Am 17.10.2010 13:48, schrieb Steven D'Aprano: On Sun, 17 Oct 2010 03:58:21 -0700, Yingjie Lan wrote: Hi, I played with an example related to namespaces/scoping. The result is a little confusing: [snip example of UnboundLocalError] Python's scoping rules are such that if you assign to a

Re: please help explain this result

2010-10-17 Thread TomF
On 2010-10-17 10:21:36 -0700, Paul Kölle said: Am 17.10.2010 13:48, schrieb Steven D'Aprano: On Sun, 17 Oct 2010 03:58:21 -0700, Yingjie Lan wrote: Hi, I played with an example related to namespaces/scoping. The result is a little confusing: [snip example of UnboundLocalError] Python's

Re: please help explain this result

2010-10-17 Thread Paul Kölle
Am 17.10.2010 19:51, schrieb TomF: On 2010-10-17 10:21:36 -0700, Paul Kölle said: Am 17.10.2010 13:48, schrieb Steven D'Aprano: On Sun, 17 Oct 2010 03:58:21 -0700, Yingjie Lan wrote: Hi, I played with an example related to namespaces/scoping. The result is a little confusing: [snip

Re: please help explain this result

2010-10-17 Thread Gregory Ewing
Yingjie Lan wrote: So, I assume that when the 'def' is executed, any name occurred will be categorized as either local or global (maybe nonlocal?). Actually it happens earlier than that -- the bytecode compiler does the categorization, and generates different bytecodes for accessing these