python主要功能_Python主要功能
python主要功能
Python main function is executed only when it’s being executed as a python program. As you know, we can also 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 and then use if __name__ == '__main__' condition to execute this function.
对于python main函数,我们必须定义⼀个 ,然后使⽤if __name__ == '__main__'条件执⾏该函数。
If the python source file is imported as , python interpreter sets the __name__ value to module name, so the if condition will return false and main method will not be executed.
如果将python源⽂件作为导⼊,则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新手代码及作用下图显⽰了将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主要功能
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论