I want开发者_如何转开发 to know whether there is a main function in Python as in C, etc. ?
The short answer is no. Typically,
if __name__ == '__main__':
import sys
argc = len(sys.argv)
argv = sys.argv
is the closest thing you get to a main
in Python. More info here.
No. Python scripts are executed from beginning to end, which means that there is no need for a main()
function.
Having said that, many scripts use a main sentinel, which checks the value of a certain global to see if the script/module is being run directly:
if __name__ == '__main__':
dosomething()
Python is executed from top to down like a script. There is no main function. However, when you want to define as certain execution structure, you can choose to check if the magic attribute __name__
is set to __main__
, which it does only when it is executed directly through an interpretor (that is not imported as module).
There are some ways of defining the __name__ == '__main__'
construct. This article gives some pointers.
精彩评论