/Doc/reference/toplevel_components.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 125 lines · 82 code · 43 blank · 0 comment · 0 complexity · 1a04a1f5cfcb5946096c2ea7a32cca29 MD5 · raw file

  1. .. _top-level:
  2. ********************
  3. Top-level components
  4. ********************
  5. .. index:: single: interpreter
  6. The Python interpreter can get its input from a number of sources: from a script
  7. passed to it as standard input or as program argument, typed in interactively,
  8. from a module source file, etc. This chapter gives the syntax used in these
  9. cases.
  10. .. _programs:
  11. Complete Python programs
  12. ========================
  13. .. index:: single: program
  14. .. index::
  15. module: sys
  16. module: __main__
  17. module: __builtin__
  18. While a language specification need not prescribe how the language interpreter
  19. is invoked, it is useful to have a notion of a complete Python program. A
  20. complete Python program is executed in a minimally initialized environment: all
  21. built-in and standard modules are available, but none have been initialized,
  22. except for :mod:`sys` (various system services), :mod:`__builtin__` (built-in
  23. functions, exceptions and ``None``) and :mod:`__main__`. The latter is used to
  24. provide the local and global namespace for execution of the complete program.
  25. The syntax for a complete Python program is that for file input, described in
  26. the next section.
  27. .. index::
  28. single: interactive mode
  29. module: __main__
  30. The interpreter may also be invoked in interactive mode; in this case, it does
  31. not read and execute a complete program but reads and executes one statement
  32. (possibly compound) at a time. The initial environment is identical to that of
  33. a complete program; each statement is executed in the namespace of
  34. :mod:`__main__`.
  35. .. index::
  36. single: UNIX
  37. single: command line
  38. single: standard input
  39. Under Unix, a complete program can be passed to the interpreter in three forms:
  40. with the :option:`-c` *string* command line option, as a file passed as the
  41. first command line argument, or as standard input. If the file or standard input
  42. is a tty device, the interpreter enters interactive mode; otherwise, it executes
  43. the file as a complete program.
  44. .. _file-input:
  45. File input
  46. ==========
  47. All input read from non-interactive files has the same form:
  48. .. productionlist::
  49. file_input: (NEWLINE | `statement`)*
  50. This syntax is used in the following situations:
  51. * when parsing a complete Python program (from a file or from a string);
  52. * when parsing a module;
  53. * when parsing a string passed to the :keyword:`exec` statement;
  54. .. _interactive:
  55. Interactive input
  56. =================
  57. Input in interactive mode is parsed using the following grammar:
  58. .. productionlist::
  59. interactive_input: [`stmt_list`] NEWLINE | `compound_stmt` NEWLINE
  60. Note that a (top-level) compound statement must be followed by a blank line in
  61. interactive mode; this is needed to help the parser detect the end of the input.
  62. .. _expression-input:
  63. Expression input
  64. ================
  65. .. index:: single: input
  66. .. index:: builtin: eval
  67. There are two forms of expression input. Both ignore leading whitespace. The
  68. string argument to :func:`eval` must have the following form:
  69. .. productionlist::
  70. eval_input: `expression_list` NEWLINE*
  71. .. index:: builtin: input
  72. The input line read by :func:`input` must have the following form:
  73. .. productionlist::
  74. input_input: `expression_list` NEWLINE
  75. .. index::
  76. object: file
  77. single: input; raw
  78. single: raw input
  79. builtin: raw_input
  80. single: readline() (file method)
  81. Note: to read 'raw' input line without interpretation, you can use the built-in
  82. function :func:`raw_input` or the :meth:`readline` method of file objects.