Python 定义依赖于其他未定义的定义?

发布于 2024-09-16 04:29:15 字数 306 浏览 5 评论 0原文

我不知道该给它起什么标题,所以如果有人想编辑它:请继续。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月下凄凉 2024-09-23 04:29:15

在Python中,函数不需要按照使用顺序来定义。只要在运行时调用函数之前在某处定义它,它就应该可以工作。这是因为 Function_A() 在被调用之前才被实际评估,在这种情况下,它位于 test.py 文件的底部,此时 Function_B() 已经被定义。

test.py:

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

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

Function_A()

运行示例:

马哈茂德:~$ python test.py
我们要执行 B 功能!
我们成功了!!

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!!

紫竹語嫣☆ 2024-09-23 04:29:15

函数名称在运行时解析。因此,当 Function_A 调用 Function_B 时,直到 Function_A 实际进行调用时才会查找名称“Function_B”。因此,只要 Function_B 是在 Function_A 实际调用时定义的,一切都会正常工作。例如,这不起作用

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

Function_A()

def Function_B():
    print "Function B entered"

...因为对 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:

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.

阳光①夏 2024-09-23 04:29:15

正如所写,两个函数都被定义,但两个函数都没有被调用。

如果您在 Function_B 的定义之前调用 Function_A ,您将收到错误消息:

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

Function_A() # will fail

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

如果您调用 Function_A Function_B 定义之后,一切都会正常工作:

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

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

Function_A() # will succeed

解释器从头到尾执行文件,定义并调用函数。只有在调用函数时才会仔细查看函数体。

当执行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 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.

无需解释 2024-09-23 04:29:15

正如其他人所说,当调用 Function_A 时,会查找 Function_B 。这是一个示例,显示 Function_B 甚至可以重新定义并且仍然正常工作。

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

输出:

我们将执行 B 功能!
我们成功了!
我们要执行 B 功能!
函数 B 已被重新定义!

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!

若水般的淡然安静女子 2024-09-23 04:29:15

我可以看到你有两个问题。第一,def 后面需要冒号。二:您混淆了最终字符串的开/闭引号。

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

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

否则,此代码将按发布的方式工作。我运行这些功能没有任何问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文