Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

Python recursive function call with if statement

I have a question regarding function-calls using if-statements and recursion. I am a bit confused because python seems to jump into the if statements block even if my function returns "False"

Here is an example:

1    def function_1(#param):
2       if function_2(#param):
3           #do something
4           if x<y:
5               function_1(#different parameters)
6           if x>y:
7               function_1(#different parameters)

My function_2 returns "False" but python continues the code on line 5 for example. Can anyone explain this behavior? Thanks in advance for any answers.

edit: Sorry, forgot the brackets

concrete example:

1    def findExit(field, x, y, step):
2        if(isFieldFree(field, x, y)):
3            field[y][x] = filledMarker
4            findExit(field, x + 1, y, step+1)
5            findExit(field, x - 1, y, step+1)
6            findExit(field, x, y + 1, step+1)
7            findExit(field, x, y - 1, step+1)
8        elif(isFieldEscape(field, x, y)):
9            way.append(copy.deepcopy(field))
10            wayStep.append(step+1)

    def isFieldFree(field, x, y):
        if field[y][x] == emptyMarker:
            return True
        else:
            return False

    def isFieldEscape(field, x, y):
        if field[y][x] == escapeMarker:
            return True
        else:
            return False

After both functions "isFieldFree" and "isFieldEscape" return False python continues the code in line 5 sometimes in line 6.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Short answer:

That's because you're not actually calling the function. You can call the function by using the parenthesis.

if function2():
    ...

Long answer:

Functions in Python are a first class citizen (functional paradigm), and so it is completely valid to refer to a function just by its name. The following is valid syntax:

def hello():
    print("Hello")

hello_sayer = hello
hello_sayer() # print "Hello"

The next concept in play is truth-ness of non-Boolean variables. In Python, the following are considered False-y

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}. instances of user-defined classes, if the class defines
  • a nonzero() or len() method, when that method returns the integer zero or bool value False.

Everything else is True-ish. Since a function name falls under none of the above categories, it is considered True-ish when tested in a conditional context.

Reference: https://docs.python.org/3/library/stdtypes.html#truth-value-testing

Edit: The earlier question was incomplete, and did not have a function call. For the new question, AChampion's answer is the correct one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...