为什么不运行main()函数? (什么是Python脚本的入口点?)

发布于 2025-01-23 18:11:15 字数 284 浏览 4 评论 0原文

我有此代码:

import sys

def random(size=16):
    return open(r"C:\Users\ravishankarv\Documents\Python\key.txt").read(size)

def main():
    key = random(13)
    print(key)

当我尝试运行脚本时,没有任何错误,但似乎没有发生任何错误。我希望它可以从密钥文件中打印一些内容,但没有打印任何内容。

怎么了?如何使代码运行?

I have this code:

import sys

def random(size=16):
    return open(r"C:\Users\ravishankarv\Documents\Python\key.txt").read(size)

def main():
    key = random(13)
    print(key)

When I try running the script, there are no errors, but nothing appears to happen. I expected it to print some content from the key file, but nothing is printed.

What is wrong? How do I make the code run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

小姐丶请自重 2025-01-30 18:11:15

您根本没有称呼自己的主要功能,因此Python解释器不会为您称呼。

将其添加为最后一行,始终将其调用:

main()

或者,如果您使用常见的情况:

if __name__ == "__main__":
    main()

仅当该模块由Python Instraper执行为启动代码时,它才能确保您的主方法被调用。有关此方面的更多信息:如果__name __name__ ==“ __-main __”:do? >

如果您想知道如何编写“主”函数, guido van rossum (Python的创建者)在2003年写了关于它的文章。

You've not called your main function at all, so the Python interpreter won't call it for you.

Add this as the last line to just have it called at all times:

main()

Or, if you use the commonly seen:

if __name__ == "__main__":
    main()

It will make sure your main method is called only if that module is executed as the starting code by the Python interpreter. More about that here: What does if __name__ == "__main__": do?

If you want to know how to write a 'main' function, Guido van Rossum (the creator of Python) wrote about it in 2003.

迟月 2025-01-30 18:11:15

Python不像其他语言自动调用main()函数。您所做的只是定义您的功能。

您必须手动调用您的主要功能:

main()

另外,您通常可以在某些代码中看到这一点:

if __name__ == '__main__':
    main()

Python isn't like other languages where it automatically calls the main() function. All you have done is defined your function.

You have to manually call your main function:

main()

Also, you may commonly see this in some code:

if __name__ == '__main__':
    main()
仙女 2025-01-30 18:11:15

确实发生了一些事情,它只是不引人注目的

Python从上到下运行脚本。 def是一个语句,并且在遇到时,它像其他任何语句一样在遇到时执行。但是,其效果是创建函数(并为其分配名称),不称其为。同样,import是一个加载其他模块的语句(并以其自己的全局变量上下文,并使 代码运行),并为其分配一个名称。

因此,当示例代码运行时,会发生三件事:

  • sys标准库模块运行的代码,然后在我们自己的模块的全局变量中使用name sys被绑定到该模块

  • 。对于随机,然后名称随机绑定到该功能

  • main main <的代码创建函数/code>,然后名称main绑定到该功能


没有任何调用函数,因此未调用它们。由于未调用它们,因此内部的代码没有运行 - 它仅用于创建功能。由于该代码未运行,因此未读取文件,没有任何内容print ed。

与其他某些语言不同,没有“特殊”函数名称

,python 不在乎 一个函数命名为main或其他任何内容。它不会自动运行。

正如Python的禅宗所说,“明确胜于隐式”。如果我们希望调用一个函数,则必须调用它。自动运行的唯一事情是最高级别的东西,因为这些是我们明确给出的说明。

该脚本从许多实际脚本的顶部开始

,您可能会看到一行,如果__name__ =='__-main __':。这是不是“脚本启动的地方”。该脚本运行到上下。

请阅读如果__name __ ==“ __ main __”:做什么?代码>语句(简短版本:如果其他人导入 s此文件作为模块,请确保跳过顶级代码的一部分)。它不是强制性的,并且它确实 没有任何特殊的“信号”目的来说明代码在哪里开始运行。它是 如果语句,这是一个完全正常的,那就是检查略有不寻常的条件。没有什么需要您在脚本中使用它(除了要检查检查的内容),也没有什么可以阻止您多次使用它。没有什么可以阻止您检查__名称__是否等于其他值(这只是...几乎肯定是没有用的)。

Something does happen, it just isn't noticeable

Python runs scripts from top to bottom. def is a statement, and it executes when it is encountered, just like any other statement. However, the effect of this is to create the function (and assign it a name), not to call it. Similarly, import is a statement that loads the other module (and makes its code run top to bottom, with its own global-variable context), and assigns it a name.

When the example code runs, therefore, three things happen:

  • The code for the sys standard library module runs, and then the name sys in our own module's global variables is bound to that module

  • A function is created from the code for random, and then the name random is bound to that function

  • A function is created from the code for main, and then the name main is bound to that function

There is nothing to call the functions, so they aren't called. Since they aren't called, the code inside them isn't run - it's only used to create the functions. Since that code doesn't run, the file isn't read and nothing is printed.

There are no "special" function names

Unlike in some other languages, Python does not care that a function is named main, or anything else. It will not be run automatically.

As the Zen of Python says, "Explicit is better than implicit". If we want a function to be called, we have to call it. The only things that run automatically are the things at top level, because those are the instructions we explicitly gave.

The script starts at the top

In many real-world scripts, you may see a line that says if __name__ == '__main__':. This is not "where the script starts". The script runs top to bottom.

Please read What does if __name__ == "__main__": do? to understand the purpose of such an if statement (short version: it makes sure that part of your top-level code is skipped if someone else imports this file as a module). It is not mandatory, and it does not have any kind of special "signalling" purpose to say where the code starts running. It is just a perfectly normal if statement, that is checking a slightly unusual condition. Nothing requires you to use it in a script (aside from wanting to check what it checks), and nothing prevents you from using it more than once. Nothing prevents you from checking whether __name__ is equal to other values, either (it's just... almost certainly useless).

野生奥特曼 2025-01-30 18:11:15

python中没有这样的main方法,您要做的是:

if __name__ == '__main__':
    main()

There's no such main method in python, what you have to do is:

if __name__ == '__main__':
    main()
说谎友 2025-01-30 18:11:15

您没有调用该功能。将main()放在代码底部。

You're not calling the function. Put main() at the bottom of your code.

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