This section describes the rules for syntax and variable names.
Here are the general rules for writing Python programs:
Statements execute one after another, unless control-flow
statements are used (if
,
while
, for
, raise
, calls, etc.).
A block is delimited by indenting all of its statements by the same amount, with spaces or tabs. A tab counts for enough spaces to move the column to a multiple of 8. Blocks can appear on the same line as a statement header if they are simple statements.
A statement ends at the end of a line, but can continue
over multiple lines if a physical line ends with a , an unclosed
()
, []
, or {}
pair, or an unclosed, triple-quoted
string. Multiple simple statements can appear on a line if they
are separated with a semicolon (;
).
Comments start with a #
(not in a string constant) and span to the end of the
line.
If a function, module file, or class begins with a string
literal, it is stored in the object’s __doc__
attribute. See the help()
function, and the pydoc
module and script in the Python
Library Reference for automated extraction and display
tools.
Generally significant only to the left of code, where indentation is used to group blocks. Blank lines and spaces are otherwise ignored except as token separators and within string constants.
This section contains the rules for user-defined names (i.e., variables) in programs.
User-defined names start with a letter or underscore
(_
), followed by any number
of letters, digits, or underscores.
User-defined names cannot be the same as any Python reserved word listed in Table 1-12.[3]
User-defined names and reserved words are always case-sensitive: SPAM, spam, and Spam are different names.
Python does not use the characters $
and ?
in its syntax, though they can
appear in string constants and comments, and $
is special in string template
substitution.
User-defined names are created by assignment but must exist when referenced (e.g., counters must be explicitly initialized to zero). See the section Namespace and Scope Rules.
Table 1-12. Python 3.0 reserved words
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
In Python 2.6, print
and
exec
are both reserved words,
as they take the form of statements, not built-in functions. Also
in Python 2.6, nonlocal
,
True
, and False
are not reserved words; the first
of these is unavailable, and the latter two are simply built-in
names. with
and as
are reserved in both 2.6 and 3.0, but
not in earlier 2.X releases unless context managers are explicitly
enabled. yield
is reserved as
of 2.3; it morphed from statement to expression later but is still
a reserved word.
Names that begin and end with two underscores (for
example, __init__
) have a
special meaning to the interpreter but are not reserved
words.
Names beginning with one underscore (e.g., _X
) and
assigned at the top level of a module are not copied out by from...*
imports (see also
the __all__
module export names
list, mentioned in the
sections The from Statement and
Pseudoprivate Attributes). In other
contexts, this is an informal convention for internal
names.
Names beginning but not ending with two underscores (e.g.,
__X
) within a class
statement are prefixed with the
enclosing class’s name (see Pseudoprivate Attributes).
The name that is just a single underscore (_
) is used in the interactive
interpreter (only) to store the result of the last
evaluation.
Built-in function and exception names (e.g., open
, SyntaxError
) are not reserved
words. They live in the last-searched scope and can be
reassigned to hide the built-in meaning in the current scope
(e.g., open=myfunction
).
Class names commonly begin with an uppercase letter (e.g.,
MyClass
), and modules with a
lowercase letter (e.g., mymodule
).
The first (leftmost) argument in a class method function
is usually named self
.
[3] In the Jython Java-based implementation, user-defined names can sometimes be the same as reserved words.