When trying out code that assigns a GUID to class instances, I wrote something similar to the following:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
x_id = 0 # immutable
x_id_list = [0] # mutable
def fx(x):
    global x_id # Disable to get "UnboundLocalError: local variable 'x_id' referenced before assignment"
    if x is None:
        x_id += 2
        x_id_list[0] += 2
    else:
        x_id += 1
        x_id_list[0] += 1
        return (x_id - 1)
    return x_id
expected = [x for x in xrange(10)]
actual = [fx(x) for x in expected]
assert(expected == actual), "expected = {}, actual = {}".format(expected, actual)
print x_id_list
print x_id
Notice that only the immutable x_id throws the UnboundLocalError if its global scope is not defined, but the mutable x_id_list continues to work fine without its global scope needing to be defined.
Why is that?
 
     
     
    