Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Entry point
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Python=== [[Python (programming language)|Python]] programs are evaluated top-to-bottom, as is usual in scripting languages: the entry point is the start of the source code. Since definitions must precede use, programs are typically structured with definitions at the top and the code to execute at the bottom (unindented), similar to code for a [[one-pass compiler]], such as in Pascal. Alternatively, a program can be structured with an explicit <code>main</code> function containing the code to be executed when a program is executed directly, but which can also be invoked by importing the program as a module and calling the function. This can be done by the following idiom, which relies on the internal variable <code>__name__</code> being set to <code>__main__</code> when a program is executed, but not when it is imported as a module (in which case it is instead set to the module name); there are many variants of this structure:<ref>{{cite web |url=https://www.artima.com/weblogs/viewpost.jsp?thread=4829 |title=Python main() functions |author=Guido van Rossum |author-link=Guido van Rossum |date=May 15, 2003 |postscript=, |access-date=June 29, 2015 |archive-date=July 11, 2015 |archive-url=https://web.archive.org/web/20150711062438/http://www.artima.com/weblogs/viewpost.jsp?thread=4829 |url-status=live }}[https://www.artima.com/forums/flat.jsp?forum=106&thread=4829 comments]</ref><ref>[http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#modules-scripts Code Like a Pythonista: Idiomatic Python] {{Webarchive|url=https://web.archive.org/web/20140527204143/http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#modules-scripts |date=2014-05-27 }}βon Python scripts used as modules</ref><ref>{{cite web |url=http://nedbatchelder.com/blog/200306/python_main_functions.html |title=Python main() functions |author=Ned Batchelder |date=6 June 2003 |access-date=29 June 2015 |archive-date=20 September 2015 |archive-url=https://web.archive.org/web/20150920223603/http://nedbatchelder.com/blog/200306/python_main_functions.html |url-status=live }}</ref> <syntaxhighlight lang="python"> import sys def main(argv): n = int(argv[1]) print(n + 1) if __name__ == "__main__": sys.exit(main(sys.argv)) </syntaxhighlight> In this idiom, the call to the named entry point <code>main</code> is explicit, and the interaction with the operating system (receiving the arguments, calling system exit) are done explicitly by library calls, which are ultimately handled by the Python runtime. This contrasts with C, where these are done ''implicitly'' by the runtime, based on convention.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)