stacknewbie
stacknewbie

Reputation: 11

Variable contains None even after assignment

Here is a function that I wrote:

def conc(i,s,y):
    if sheet.cell(i+1,0).value != sheet.cell(2,0).value :
        # print s
        rtrns = s
        # print rtrns
        return rtrns

    else:
        if i==list1[len(list1)-1]:
            while i<(sheet.nrows):
                # print i
                s = s + " " + unicode(sheet.cell(i,y).value).encode('cp1252', 'replace')
                i+=1
            # print s
            rtrns = s
            # print rtrns
            return rtrns
        else:
            s = s + " " + unicode(sheet.cell(i+1,y).value).encode('cp1252', 'replace')
            #return s
            conc(i+1,s,y)

In the above function, when I print the value of rtrns in the first if block, it displays the value that I need.

But when I make a call to the function

c = conc(x,c,2)     #where x fetches an integer value as an index from a list
print c

it returns None

Upvotes: 1

Views: 123

Answers (1)

Abhijit
Abhijit

Reputation: 63767

The return statement is missing in the else part of your code

else:
    s = s + " " + unicode(sheet.cell(i+1,y).value).encode('cp1252', 'replace')
    #return s
    conc(i+1,s,y)

So, there is one code path, where nothing is returned from your recursive function conc.

Change the code and add a return

else:
    s = s + " " + unicode(sheet.cell(i+1,y).value).encode('cp1252', 'replace')
    #return s
    return conc(i+1,s,y)

A recursive call always returns back to the caller. So, when you recursively call conc, and one of the code path issues a return statement, the call gets back to from where you invoked. Another important thing is, a function without an explicit return has an implicit return with None.

The Following ASCII Art should be self explanatory of what might be going wrong

foo() <------------------------------------------------------------------------
    |                                                                          |
    |                                                                          |
    |->def conc(...):                                                          |
           else:                                                             (None)
           .........                                                           |
              conc(i+1,s,y) <-------------------------------------------\      |
              return None  ----------------------------------------------U--- -|
               |                                                         |
               |                                                         | 
               \----->def conc(...):                                     |
                          else:                                        (None)
                          .........                                      |
                                conc(i+1,s,y) <----------------------\   |
                                return None   ------------------------U--|
                                |                                     |
                                |                                     |
                                |                                     |
                                \----------->def conc(...):        (rtrns)
                                                  if ... :            |
                                                  ..........          |
                                                      return rtrns----|

Upvotes: 4

Related Questions