python主要功能_Python主要功能

python主要功能

Python main function is executed only when it’s being executed as a python program. As you know, we can also import a python program as a module, in that case python main method should not execute.

仅当将Python main函数作为python程序执行时才执行。 如您所知,我们还可以 python程序作为模块导入 ,在这种情况下,不应执行python main方法。

Python主要功能 (Python main function)

Main function is the entry point of any program. But python interpreter executes the source file code sequentially and doesn’t call any method if it’s not part of the code. But if it’s directly part of the code then it will be executed when the file is imported as a module.

主要功能是任何程序的入口。 但是python解释器会顺序执行源文件代码,并且如果它不是代码的一部分,则不会调用任何方法。 但是,如果直接将其作为代码的一部分,则在将文件作为模块导入时将执行该代码。

That’s why there is a special technique to define main method in python program, so that it gets executed only when the program is run directly and not executed when imported as a module. Let’s see how to define python main function in a simple program.

这就是为什么有一种特殊的技术来定义python程序中的main方法,以便仅在程序直接运行时才执行,而在作为模块导入时则不执行。 让我们看看如何在一个简单的程序中定义python main函数。

python_main_function.py

python_main_function.py

print("Hello")

print("__name__ value: ", __name__)


def main():
    print("python main function")


if __name__ == '__main__':
    main()
  • When a python program is executed, python interpreter starts executing code inside it. It also sets few implicit variable values, one of them is __name__ whose value is set as __main__.

    当执行python程序时,python解释器开始在其中执行代码。 它还设置了一些隐式变量值,其中之一是__name__其值设置为__main__
  • For python main function, we have to define a function and then use if __name__ == '__main__' condition to execute this function.

    对于python main函数,我们必须定义一个函数 ,然后使用if __name__ == '__main__'条件执行该函数。
  • If the python source file is imported as module, python interpreter sets the __name__ value to module name, so the if condition will return false and main method will not be executed.

    如果将python源文件作为module导入,则python解释器__name__值设置为模块名,因此if条件将返回false且不会执行main方法。
  • Python provides us flexibility to keep any name for main method, however it’s best practice to name it as main() method. Below code is perfectly fine, however not recommended.
    def main1():
        print("python main function")
    
    
    if __name__ == '__main__':
        main1()

    Python为我们提供了灵活的方式来保留main方法的任何名称,但是最好的做法是将其命名为main()方法。 下面的代码是完全可以的,但是不建议这样做。

Below image shows the output when python_main_function.py is executed as source file.

下图显示了将python_main_function.py作为源文件执行时的输出。

Python主要功能作为模块 (Python main function as module)

Now let’s use above python source file as a module and import in another program.

现在,让我们使用上面的python源文件作为模块并导入另一个程序。

python_import.py

python_import.py

import python_main_function

print("Done")

Now when above program is executed, below output is produced.

现在,当执行上面的程序时,将产生下面的输出。

Hello
__name__ value:  python_main_function
Done

Notice that first two lines are getting printed from python_main_function.py source file. Notice the value of __name__ is different and hence main method is not executed.

注意,前两行是从python_main_function.py源文件中打印出来的。 请注意, __name__的值不同,因此不会执行main方法。

Notice that python program statements are executed line by line, so it’s important to define the main() method first before the if condition to execute main method. Otherwise you will get error as NameError: name 'main' is not defined.

请注意,python程序语句是逐行执行的,因此在要执行main方法的if条件之前首先定义main()方法很重要。 否则,您将收到错误,因为NameError: name 'main' is not defined

That’s all about python main function.

这就是关于python main函数的全部内容。

Reference: Python Docs

参考: Python文档

翻译自: https://www.journaldev.com/17752/python-main-function

python主要功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值