开发者

Python definitions relying on other undefined definitions?

I didn't know what to title this, so if anyone wants to edit it: Go ahead.

def Function_A()
 print "We're going to function B!"
 Function_B()

def Function_B()
 开发者_Python百科print "We made it!'

This is a beginner question, but the solution hasn't occurred to me as I've been spoiled by compiled languages. You can see here, Function_A leads to Function_B. At runtime, Function_B will not be defined when Function_A is called, so it can't happen. How do I get around this?


In Python, functions do not need to be defined in order of use. As long as it's defined somewhere before the function is called at runtime it should work. This is because Function_A() is not actually evaluated until it's called, which in this is case is at the bottom of the this test.py file at which point Function_B() is already defined.

test.py:

def Function_A():
    print "We're going to function B!"
    Function_B()

def Function_B():
    print "We made it!!"

Function_A()

Example run:

mahmoud:~$ python test.py
We're going to function B!
We made it!!


Function names are resolved at runtime. So when Function_A calls Function_B, the name "Function_B" is not looked up until Function_A actually makes the call. So everything works fine as long as Function_B is defined by the time Function_A is actually called. This, for example, would not work:

def Function_A():
    print "Function A entered"
    Function_B()

Function_A()

def Function_B():
    print "Function B entered"

... because the call to Function_A is before Function_B's definition.


As written, both functions are being defined, but neither of the functions is called.

If you call Function_A before the definition of Function_B you will get an error:

def Function_A():
 print "We're going to function B!"
 Function_B()

Function_A() # will fail

def Function_B():
 print "We made it!"

If you call Function_A after the definition of Function_B everything will work fine:

def Function_A():
 print "We're going to function B!"
 Function_B()

def Function_B():
 print "We made it!"

Function_A() # will succeed

The interpreter executes the file from beginning to end, defining and calling functions as they come along. The function bodies will only be closely looked at when the function is called.

When executing Function_A and reaching its second line, the interpreter will look in the global namespace to find something called Function_B. In the first example there hasn't been anything defined yet, while in the second example there is a Function_B that can be called.


As others have said, Function_B is looked up when Function_A is called. Here is an example that shows Function_B can even be redefined and still work properly.

def Function_A():
    print "We're going to function B!"
    Function_B()

# Function_A() # This would cause a NameError

def Function_B():
    print "We made it!"

Function_A()

def Function_B():
    print "Function B has been redefined!"

Function_A()

output:

We're going to function B!
We made it!
We're going to function B!
Function B has been redefined!


You have two problems I can see. One, you need colons after def. Two: You've mixed up the open / close quotes on your final string.

def Function_A():
 print "We're going to function B!"
 Function_B()

def Function_B():
 print "We made it!"

Otherwise, this code works as posted. I have no problems running these functions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜