Need explanation on recursive and generator example
<p>I have this recursive function which is used to generate all possible up and lower cases to any string value you pass it to it.</p>

<p>Here is the code sample and the output:</p>

<pre><code>def test (name):

    if not name:
        yield ""

    else:
        first=name[:1]

        for sub in test(name[1:]):

            yield first.lower()+sub
            yield first.upper()+sub
            #print (first)

for x in test("abc"):
    print (x)
</code></pre>

<p>the output will be like this : </p>

<pre><code>abc
Abc
aBc
ABc
abC
AbC
aBC
ABC
</code></pre>

<p>and if I add print function under the two yield functions:</p>

<pre><code>print(first)
</code></pre>

<p>the out put will be like this : </p>

<pre><code>abc
Abc
a
aBc
ABc
a
b
abC
AbC
a
aBC
ABC
a
b
c
</code></pre>

<p>I just want a clear explanation for each step and what is happening and why I got these value each time and how does it work, because recursive and generators together made me lost here.</p>

<p>thanks .... 
yesterday i just took a paper ,a pen and i trace this function from stack view and i understand it ...  </p>

<p>it's recursive so the function will call itself from the second letter to the end of whole string each time till the the function "test" called with "" value ... </p>

<p>then i have to apply the for loop each time from the last to the first ... 
the last will have variables like this : first ="c" , sub=""</p>

<pre><code>for each sub :
    first.lower+sub &gt;&gt; c 
    first.upper+sub&gt;&gt;  C
</code></pre>

<p>then back to the previous call will take the last result 
so for each "c" and for each "C" will apply </p>

<pre><code>first.lower()+sub &gt; bc bC 
first.upper()+sub &gt; bC BC
</code></pre>

<p>then back to the first call with last result<br>
right now we have sub = bc,Bc,bC,BC
and first ='a'</p>

<pre><code> for each sub :

first.lower+sub
first.upper+sub

abc
Abc
aBc
ABc
abC
AbC 
aBC
ABC
</code></pre>

python python-3.x recursion generator yield