Local and global lists/arrays

Hi all,

I was trying to understand local and global arrays/lists in Python. Consider the following code snippets -
Code 1 -
def f():
a = np.append(a,3)
a = np.array([12,3,4])

This gives a ‘local variable ‘a’ referenced before assignment’ error.

Code 2-
def f():
a =[12,3,4]

This prints [12,3,4,3] twice.

In both code 1 and code 2 a is a global variable. But with lists it is possible to change the value within f whereas other global variables do not allow their values to be changed within a function without the global keyword. I think this difference is because in case of lists f() knows the memory address whereas for arrays f() only knows the contents of it. Is this the right way to think about this ?

Thanking in advance for comments and suggestions.

We cannot see the code correctly because it is missing indentation. Please put the code parts between triple backtick markers to show them properly:

``` python
# your code here

By Rajorshi Chattppadhyay via Discussions on Python.org at 16Jun2022 10:19:

I was trying to understand local and global arrays/lists in Python.

First, it is nothing to do with the type of value (list/array). ANy
variable will show this behaviour.

What you’re seeing is that in Python, variables are local to a function
if they are assigned to. So in your function:

def f():
    a = np.append(a,3)

“a” is a local variable because you assign to it. It is unrelated to
the global “a” you use elsewhere. This is how we avoid having to declare

If you really want to access the global and assign to it, you need to
declare the name as global:

def f():
    global a
    a = np.append(a,3)

But really, it is better to pass “a” to the function:

def f(a):
    a = np.append(a,3)

and call:


you can pass the global “a” into the f() function, where it is
modified and printed. There’s no relationship between the global and
local things named “a”. The association is from passing “a” to the

Bt really, try to avoid using global variables. They’re convenient and
useful in tiny snippted of code like this (although you have already
tripped over problems, hence your question), but they don’t work nearly
as well in larger programmes.

Cameron Simpson cs@cskk.id.au

No, you are not correct in your analysis.

In Code 1, a is not a global variable, it is a local variable. That is why you get the error:

UnboundLocalError: local variable 'a' referenced before assignment

When the Python interpreter tells you a variable is local, it is not lying! You should believe it.

It is local because you assign to the variable a:

# Assigning to `a` makes it local, unless declared global.
a = np.append(a, 3)

So your code tries to append 3 to the local variable a, which doesn’t exist yet.

In Code 2, there is no assignment, so a is treated as global.

Numpy arrays do not support in-place append, so you cannot easily append to them like you can append to a list. np.append makes a copy of the array.

We can get the same error for lists by using assignment:

def f():
    a = a + [3]

a = [12, 3, 4]

will fail the same way as Code 1.