Python 定义依赖于其他未定义的定义?
我不知道该给它起什么标题,所以如果有人想编辑它:请继续。
def Function_A()
print "We're going to function B!"
Function_B()
def Function_B()
print "We made it!'
这是一个初学者问题,但我还没有想到解决方案,因为我已经被编译语言宠坏了。您可以在这里看到,Function_A 通向 Function_B。在运行时,调用 Function_A 时,Function_B 不会被定义,因此不会发生这种情况。我该如何解决这个问题?
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()
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在Python中,函数不需要按照使用顺序来定义。只要在运行时调用函数之前在某处定义它,它就应该可以工作。这是因为 Function_A() 在被调用之前才被实际评估,在这种情况下,它位于 test.py 文件的底部,此时 Function_B() 已经被定义。
test.py:
运行示例:
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:
Example run:
函数名称在运行时解析。因此,当 Function_A 调用 Function_B 时,直到 Function_A 实际进行调用时才会查找名称“Function_B”。因此,只要 Function_B 是在 Function_A 实际调用时定义的,一切都会正常工作。例如,这不起作用:
...因为对 Function_A 的调用是在 Function_B 的定义之前。
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:
... because the call to Function_A is before Function_B's definition.
正如所写,两个函数都被定义,但两个函数都没有被调用。
如果您在
Function_B
的定义之前调用Function_A
,您将收到错误消息:如果您调用
Function_A
Function_B
定义之后,一切都会正常工作:解释器从头到尾执行文件,定义并调用函数。只有在调用函数时才会仔细查看函数体。
当执行
Function_A
并到达第二行时,解释器将在全局命名空间中查找名为Function_B
的内容。在第一个示例中尚未定义任何内容,而在第二个示例中则有一个可以调用的Function_B
。As written, both functions are being defined, but neither of the functions is called.
If you call
Function_A
before the definition ofFunction_B
you will get an error:If you call
Function_A
after the definition ofFunction_B
everything will work fine: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 calledFunction_B
. In the first example there hasn't been anything defined yet, while in the second example there is aFunction_B
that can be called.正如其他人所说,当调用
Function_A
时,会查找Function_B
。这是一个示例,显示Function_B
甚至可以重新定义并且仍然正常工作。输出:
我们将执行 B 功能!
我们成功了!
我们要执行 B 功能!
函数 B 已被重新定义!
As others have said,
Function_B
is looked up whenFunction_A
is called. Here is an example that showsFunction_B
can even be redefined and still work properly.output:
We're going to function B!
We made it!
We're going to function B!
Function B has been redefined!
我可以看到你有两个问题。第一,def 后面需要冒号。二:您混淆了最终字符串的开/闭引号。
否则,此代码将按发布的方式工作。我运行这些功能没有任何问题。
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.
Otherwise, this code works as posted. I have no problems running these functions.