tags:

views:

177

answers:

6

Why is it that below code prints 'None' as well as what I tell it to write when no match is found?

def subStringMatchExact(target,key):
    list=[]
    for fsi in range (len(target)):
        if key==target[fsi:fsi+len(key)]:
            list=list+[fsi,]
            #return list
    if list!=[]:
        return list
    else:
        print 'no match found'

print subStringMatchExact("banana","x")
+9  A: 

Let's look at this.

print subStringMatchExact("banana","x")

What value is returned by subStringMatchExact("banana","x")? Don't look at what it does. Look at what it returns. For a hint on what a function returns, read the return statements. All of them. If there is no value on a return or no return, it returns None.

What does print do with that value?

S.Lott
Hi All,Tnx for the tip!better get the basics right first :)tnx againBaba
Baba
What does "Tnx" mean?
S.Lott
@S.Lott: I believe it's short for 'thanx'
John Machin
Which is itself short for the actual English word: thanks. Itself a short form of "thank you".
JUST MY correct OPINION
@John Machin and @JUST MY correct OPINION: How do you know these things? Why didn't @Baba use the actual English word?
S.Lott
@S.Lott: This is off-topic; you should ask two separate new questions in the meta forum. However here are brief answers: (1) `http://www.internetslang.com/TNX.asp` (2) I can't channel @Baba; maybe you can.
John Machin
@John Machin: They aren't meta questions -- they're specific questions specifically addressed at @Baba. I'm not sure why you answered, since you can't channel @Baba. My point is this: folks shouldn't use non-words like "Tnx". Rather than be argumentative, I felt it better to ask @Baba what "tnx" meant, hoping that they would take the hint.
S.Lott
Hi All,I was not aware that abbreviations like tnx are unwelcome so let me rephrase: thanks all for your submissions and your answers to my questions. Much appreciated.Baba
Baba
+3  A: 

Because there is a implicit return None at the end of every function. This means that when you don't return anything, as in the else block of your example, your function returns None anyway. So, subStringMatchExact("banana","x") returns None and this gets printed.

THC4k
+2  A: 

Think about what happens in the else clause here. In the if clause your function returns a value. In the else clause, what does it return?

That's why you're getting the result you are. The print statement is printing the return value of the function, no matter which branch of the if is taken. If a function exits without explicitly returning a value, it returns None.

Omnifarious
+1  A: 

Whenever you don't explicitly return a value from a function in Python, None is implicitly returned.
By printing the return value of the subStringMatchExact function even when no match is found, you get than None, since the else clause doesn't end the flow with a return statement.

abyx
A: 

Try something like this:

def subStringMatchExact(target,key):
    list=[]
    for fsi in range (len(target)):
        if key==target[fsi:fsi+len(key)]:
            list=list+[fsi,]
            #return list
    return list

This way you're always returning a list, and the code calling subStringMatchExact can worry about what to do with the empty list (i.e. print not found, throw error etc.)

joshaidan
-1 The last four lines of your answer can be replaced simply by `return list`
John Machin
Replaced last four lines with "return list"
joshaidan
+1  A: 

use return 'no match found' instead of print 'no match found'

def subStringMatchExact(target, key):
    if key in target:
        n_target  = len(target)
        n_key = len(key)
        return [n for n in xrange(n_target) if key == target[n:n + n_key]]   
    else:
        return 'no match found'
killown
Or even "raise SomeError('no match found')"
viraptor