[ Team LiB ] |
9.2 Python Syntax RulesIn general, Python has a simple, statement-based syntax. But there are a few properties you need to know:
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 DelimitersPython 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 blocksThis 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 DelimitersStatements 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:
9.2.3 A Few Special CasesHere'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. |
[ Team LiB ] |