Argument passing in Python
<p>I don't quite understand what is going with variable 'current_label', which to me seems to be defined in the enclosing code of function 'ts_r', which would make it visible from inside 'ts_r'? But when I run the code below, it complains that local variable 'current_label' is referenced before assignment... Note that it does <em>not</em> complain about 'visited' or 'f', and that it won't complain if I initialize 'current_label' with [len(g)]. </p>

<pre><code>def topological_sort(g):

    visited = zeros((len(g)), dtype='int32')
    f = zeros((len(g)), dtype='int32')
    current_label = len(g) # [] so it is seen inside ts_r

    def ts_r(n):
        for nn in [v for v in g[n] if not visited[v]]:
            visited[nn] = 1
            ts_r(nn)
        f[n] = current_label
        current_label -= 1

    for i in range(len(g)):
        if not visited[i]: 
            ts_r(i)

    return f
</code></pre>
