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>Where are you stuck in producing your own trace?  You've shown that you know how to use <strong>print</strong> statements.  I assume that you also know how to search other examples of recursion traces; StackOverflow has many.</p>

<p>To get you started, here's your code with a couple more <strong>print</strong> statements.</p>

<pre><code>def test (name):
    print ("ENTER test, name=", name)

    if not name:
        yield ""

    else:
        first=name[:1]

        for sub in test(name[1:]):
            print ("  LOOP\tname", name, "\tfirst", first, "\tsub", sub)

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

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

<p>Output:</p>

<pre><code>ENTER test, name= abc
ENTER test, name= bc
ENTER test, name= c
ENTER test, name= 
  LOOP  name c  first c     sub 
  LOOP  name bc     first b     sub c
  LOOP  name abc    first a     sub bc
YIELDED: abc
YIELDED: Abc
  LOOP  name abc    first a     sub Bc
YIELDED: aBc
YIELDED: ABc
  LOOP  name bc     first b     sub C
  LOOP  name abc    first a     sub bC
YIELDED: abC
YIELDED: AbC
  LOOP  name abc    first a     sub BC
YIELDED: aBC
YIELDED: ABC
</code></pre>

<p>Does this get you moving in a useful direction?</p>

python python-3.x recursion generator yield