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
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
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
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
--- 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
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
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
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
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