File: toplevel_components.rst

package info (click to toggle)
python2.7 2.7.16-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,500 kB
  • sloc: python: 469,695; ansic: 444,315; sh: 17,604; asm: 14,304; makefile: 4,899; objc: 761; exp: 499; cpp: 128; xml: 76
file content (125 lines) | stat: -rw-r--r-- 3,478 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

.. _top-level:

********************
Top-level components
********************

.. index:: single: interpreter

The Python interpreter can get its input from a number of sources: from a script
passed to it as standard input or as program argument, typed in interactively,
from a module source file, etc.  This chapter gives the syntax used in these
cases.


.. _programs:

Complete Python programs
========================

.. index:: single: program

.. index::
   module: sys
   module: __main__
   module: __builtin__

While a language specification need not prescribe how the language interpreter
is invoked, it is useful to have a notion of a complete Python program.  A
complete Python program is executed in a minimally initialized environment: all
built-in and standard modules are available, but none have been initialized,
except for :mod:`sys` (various system services), :mod:`__builtin__` (built-in
functions, exceptions and ``None``) and :mod:`__main__`.  The latter is used to
provide the local and global namespace for execution of the complete program.

The syntax for a complete Python program is that for file input, described in
the next section.

.. index::
   single: interactive mode
   module: __main__

The interpreter may also be invoked in interactive mode; in this case, it does
not read and execute a complete program but reads and executes one statement
(possibly compound) at a time.  The initial environment is identical to that of
a complete program; each statement is executed in the namespace of
:mod:`__main__`.

.. index::
   single: UNIX
   single: command line
   single: standard input

A complete program can be passed to the interpreter
in three forms: with the :option:`-c` *string* command line option, as a file
passed as the first command line argument, or as standard input. If the file
or standard input is a tty device, the interpreter enters interactive mode;
otherwise, it executes the file as a complete program.


.. _file-input:

File input
==========

All input read from non-interactive files has the same form:

.. productionlist::
   file_input: (NEWLINE | `statement`)*

This syntax is used in the following situations:

* when parsing a complete Python program (from a file or from a string);

* when parsing a module;

* when parsing a string passed to the :keyword:`exec` statement;


.. _interactive:

Interactive input
=================

Input in interactive mode is parsed using the following grammar:

.. productionlist::
   interactive_input: [`stmt_list`] NEWLINE | `compound_stmt` NEWLINE

Note that a (top-level) compound statement must be followed by a blank line in
interactive mode; this is needed to help the parser detect the end of the input.


.. _expression-input:

Expression input
================

.. index:: single: input

.. index:: builtin: eval

There are two forms of expression input.  Both ignore leading whitespace. The
string argument to :func:`eval` must have the following form:

.. productionlist::
   eval_input: `expression_list` NEWLINE*

.. index:: builtin: input

The input line read by :func:`input` must have the following form:

.. productionlist::
   input_input: `expression_list` NEWLINE

.. index::
   object: file
   single: input; raw
   single: raw input
   builtin: raw_input
   single: readline() (file method)

Note: to read 'raw' input line without interpretation, you can use the built-in
function :func:`raw_input` or the :meth:`readline` method of file objects.