This code scales each leaf by a factor, but i dont understand one part of the function
<pre><code>#checking if node is a leaf
def is_leaf(item):  
    return type(item) != tuple

#performing function on every element in sequence
def map(fn, seq):
    if seq == ():
        return ()
    else:
        return (fn(seq[0]), ) + map(fn, seq[1:])

#scaling each leaf by factor
def scale_tree(tree, factor):
    def scale_func(subtree):         **#dont understand this part**
        if is_leaf(subtree):
            return factor * subtree
        else:
            return scale_tree(subtree, factor)
    return map(scale_func, tree)

tup = ((3, 2), 1, (4,), 5)      
print(scale_tree(tup, 2))
</code></pre>

<p>how does the function scale_func know what is the argument to be called if it is not stated like scale_tree?</p>

<p>From a quick read, scale_tree is calling map on its return statement with scale_func and the argument <code>tree</code> as parameters. The map function will then call the function passed as the first argument (scale_func in this case) with the sequence passed as a second argument (tree in this case) as an argument to scale_func if the sequence is not an empty tuple.</p>

<p>I would make a little diagram but writing on tablets is hard, sorry.</p>

python