The UnboundLocalError occurs because, when you make an assignment to a variable in a scope, that variable is automatically considered by Python to be local to that scope and shadows any similarly named variable in any outer scope.
In the append function, you are not making an assignment per se, and therefore there is no scope error.