9.2 Python Syntax Rules

In general, Python has a simple, statement-based syntax. But there are a few properties you need to know:

  • Statements execute one after another, until you say otherwise. Python normally runs statements in a file or nested block from first to last, but statements like the if (and, as you'll see, loops) cause the interpreter to jump around in your code. Because Python's path through a program is called the control flow, things like the if that affect it are called control-flow statements.

  • Block and statement boundaries are detected automatically. There are no braces or "begin/end" delimiters around blocks of code; instead, Python uses the indentation of statements under a header to group the statements in a nested block. Similarly, Python statements are not normally terminated with a semicolon; rather, the end of a line usually marks the end of the statements coded on that line.

  • Compound statements = header, ":", indented statements. All compound statements in Python follow the same pattern: a header line terminated with a colon, followed by one or more nested statements usually indented under the header. The indented statements are called a block (or sometimes, a suite). In the if statement, the elif and else clauses are part of the if, but are header lines with nested blocks of their own.

  • Blank lines, s paces, and comments are usually ignored. Blank lines are ignored in files (but not at the interactive prompt). Spaces inside statements and expressions are almost always ignored (except in string literals and indentation). Comments are always ignored: they start with a # character (not inside a string literal) and extend to the end of the current line.

  • Docstrings are ignored but saved, and displayed by tools. Python supports an additional comment form called documentation strings (docstrings for short), which, unlike # comments, are retained. Docstrings are simply strings that show up at the top of program files and some statements, and are automatically associated with objects. Their contents are ignored by Python, but they are automatically attached to objects at runtime, and may be displayed with documentation tools. Docstrings are part of Python's larger documentation and are covered at the end of Part III.

As you've seen, there are no variable type declarations in Python; this fact alone makes for a much simpler language syntax than what you may be used to. But for most new users, the lack of braces and semicolons to mark blocks and statements seems to be the most novel syntactic feature of Python, so let's explore what this means in more detail here.

9.2.1 Block Delimiters

Python detects block boundaries automatically, by line indentation the empty space to the left of your code. All statements indented the same distance to the right belong to the same block of code. In other words, the statements within a block line up vertically. The block ends at a line less indented or the end of the file, and more deeply nested blocks are simply indented further to the right than the statements in the enclosing block.

For instance, Figure 9-1 demonstrates the block structure of the following code:

x = 1 if x:     y = 2     if y:         print 'block2'     print 'block1' print 'block0'
Figure 9-1. Nested code blocks
figs/lpy2_0901.gif

This code contains three blocks: the first (the top-level of the file) is not indented at all; the second (within the outer if statement) is indented four spaces; and the third (the print statement under the nested if) is indented eight spaces.

In general, top-level (unnested) code must start in column 1. Nested blocks can start in any column; indentation may consist of any number of spaces and tabs, as long as it's the same for all the statements in a given block. That is, Python doesn't care how you indent your code; it only cares that it's done consistently. Technically, tabs count for enough spaces to move the current column number up to a multiple of 8, but it's usually not a good idea to mix tabs and spaces within a block use one or the other.

Indentation to the left of your code is the only major place in Python where whitespace matters; in most other contexts, space can be coded or not. However, indentation is really part of Python syntax, not just a stylistic suggestion: all the statements within any given single block must be indented the same, or Python reports a syntax error. This is on purpose because you don't need to explicitly mark the start and end of a nested block of code, it removes some of the syntactic clutter found in other languages.

This syntax model also enforces indentation consistency, a crucial component of readability in structured programming languages like Python. Python's syntax is sometimes called the "what you see is what you get" of languages the indentation of code unambiguously tells readers what it is associated with. Python's consistent appearance makes code easier to maintain.

Consistently-indented code always satisfies Python's rules. Moreover, most text editors (including IDLE) make it easy to follow Python's indentation model, by automatically indenting code as you type it.

9.2.2 Statement Delimiters

Statements normally end at the end of the line they appear on. This covers the vast majority of the Python statements you'll code. When statements are too long to fit on a single line, though, a few special rules may be used to make them span multiple continuation lines:

  • Statements may span lines if you're continuing an open syntactic pair. For statements that are too long to fit on one line, Python lets you continue typing the statement on the next line, if you're coding something enclosed in ( ), { }, or [ ] pairs. For instance, expressions in parenthesis and dictionary and list literals can span any number of lines; your statement doesn't end until the line on which you type the closing part of the pair ( ), }, or ]). Continuation lines can start at any indentation level.

  • Statements may span lines if they end in a backslash. This is a somewhat outdated feature, but if a statement needs to span multiple lines, you can also add a backslash (\) at the end of the prior line to indicate you're continuing on the next line. But since you can also continue by adding parentheses around long constructs, backslashes are almost never needed.

  • Other rules. Very long string literals can span lines arbitrarily. In fact, the triple-quoted string blocks we met in Chapter 5 are designed to do so. Although uncommon, you can also terminate statements with a semicolon this is sometimes used to squeeze more than one simple statement on a single line. Finally, comments and blank lines can appear anywhere.

9.2.3 A Few Special Cases

Here's what a continuation line looks like, using the open pairs rule; we can span delimited constructs across any number of lines:

L = ["Good",       "Bad",       "Ugly"]                    # Open pairs may span lines.

This works for anything in parentheses too: expressions, function arguments, functions headers (see Chapter 12), and so on. If you like using backslashes to continue you can, but it's not usually necessary:

if a == b and c == d and   \    d == e and f == g:    print 'olde'                  # Backslashes allow continuations.

Because any expression can be enclosed in parenthesis, you can usually simply wrap something in parenthesis anytime you need to span multiple lines:

if (a == b and c == d and     d == e and e == f):     print 'new'                  # But parentheses usually do too.

As a special case, Python allows you to write more than one non-compound statement (i.e., statements without nested statements) on the same line, separated by semicolons. Some coders use this form to save program file real estate, but it usually makes for more readable code if you stick to one statement per line for most of your work:

x = 1; y = 2; print x            # More than one simple statement

And finally, Python lets you move a compound statement's body up to the header line, provided the body is just a simple (non-compound) statement. You'll see this most often used for simple if statements with a single test and action:

if 1: print 'hello'              # Simple statement on header line

You can combine some of these special cases to write code that is difficult to read, but we don't recommend it; as a rule of thumb, try to keep each statement on a line of its own, and indent all except the simplest of blocks. Six months down the road, you'll be happy you did.



Learning Python
Learning Python: Powerful Object-Oriented Programming
ISBN: 0596158068
EAN: 2147483647
Year: 2003
Pages: 253
Authors: Mark Lutz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net