Q:

Replace a character with multiple characters using Python

I have been trying to solve the following problem using Python, and so far without success:
Assume you have a string with the characters '0', '1' and '?'. the '?' sign can be either '0' or '1'. Your goal is to print all the possible outputs for such given string.For example, the output for the string '0?1?' should be '0010', '0011', '0110' and '0111'
I have tried the following:
def comb(S):

    if not '?' in S:
        yield S
    else:
        yield comb(S.replace('?','0',1))
        yield comb(S.replace('?','1',1))             

S = '0?1??011'
S_generator = comb(S)
for s in  S_generator:
    print s

The result is strange, and is not what I am trying to get:
<generator object comb at 0x106b2ceb0>
<generator object comb at 0x106b2cf00>

Any idea why it is not working, and how I should change the code for this to work?

A:

comb() is a generator function , when you do - 
yield comb(S.replace('?','0',1))

The yield statement does not automatically loop over all the values in the generator and yield them, you have to loop over the values and yield them one by one, Example -
def comb(S):
    if not '?' in S:
        yield S
    else:
        for i in comb(S.replace('?','0',1)):
            yield i
        for i in comb(S.replace('?','1',1)):  
            yield i

Example/Demo -
>>> def comb(S):
...     if not '?' in S:
...         yield S
...     else:
...         for i in comb(S.replace('?','0',1)):
...             yield i
...         for i in comb(S.replace('?','1',1)):
...             yield i
...
>>> for s in comb('abc?def?'):
...     print(s)
...
abc0def0
abc0def1
abc1def0
abc1def1
>>> for s in comb('0?1?'):
...     print(s)
...
0010
0011
0110
0111
>>> S = '0?1??011'
>>> for s in comb(S):
...     print(s)
...
00100011
00101011
00110011
00111011
01100011
01101011
01110011
01111011

[Edit]: Note that as of Python 3.3, you can use the new yield from syntax:
yield from comb(S.replace('?','0',1))
yield from comb(S.replace('?','1',1))

