开发者

Python is printing more than I want

开发者 https://www.devze.com 2023-01-12 08:15 出处:网络
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):

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")


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?


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.


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.


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.


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'


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.)


You are using print statement to print whatever is returned by the function. Thus in the "else" case you need to return rather than print in the function.

The correct code is therefore:

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:
        return 'no match found'

print subStringMatchExact("banana","x")
0

精彩评论

暂无评论...
验证码 换一张
取 消