15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.1.3. Multiple Statement Groups as Suites ( : )<br />

Groups of individual statements making up a single code block are called "suites" in <strong>Python</strong> (as we<br />

introduced in Chapter 2). Compound or complex statements, such as if, while, def, and class, are<br />

those that require a header line and a suite. Header lines begin the statement (with the keyword) and<br />

terminate with a colon ( tt ) and are followed by one or more lines that make up the suite. We will refer<br />

to the combination of a header line and a suite as a clause.<br />

3.1.4. Suites Delimited via Indentation<br />

As we introduced in Section 2.10, <strong>Python</strong> employs indentation as a means of delimiting blocks of code.<br />

Code at inner levels are indented via spaces or tabs. Indentation requires exact indentation; in other<br />

words, all the lines of code in a suite must be indented at the exact same level (e.g., same number of<br />

spaces). Indented lines starting at different positions or column numbers are not allowed; each line<br />

would be considered part of another suite and would more than likely result in syntax errors.<br />

<strong>Core</strong> Style: Indent with four spaces and avoid using tabs<br />

As someone who is perhaps new to block delimitation using<br />

whitespace, a first obvious question might be: How many spaces<br />

should I use? We think that two is too short, and six to eight is too<br />

many, so we suggest four spaces for everyone. Also, because tabs<br />

vary in the number of spaces depending on your system, we<br />

recommend not using tabs if there is any hint of cross-platform<br />

development. Both of these style guidelines are also supported by<br />

Guido van Rossum, the creator of <strong>Python</strong>, and documented in the<br />

<strong>Python</strong> Style Guide. You will find the same suggestions in our style<br />

guide in Section 3.4.<br />

A new code block is recognized when the amount of indentation has increased, an d its termination is<br />

signaled by a "dedentation," or a reduction of indentation matching a previous level's. Code that is not<br />

indented, i.e., the highest level of code, is considered the "main" portion of the script.<br />

The decision to create code blocks in <strong>Python</strong> using indentation was based on the belief that grouping<br />

code in this manner is more elegant and contributes to the ease of reading to which we alluded earlier.<br />

It also helps avoid "dangling-else"-type problems, including ungrouped single statement clauses (those<br />

where a C if statement does not use braces at all, but has two indented statements following). The<br />

second statement will execute regardless of the conditional, leading to more programmer confusion until<br />

the light bulb finally blinks on.<br />

Finally, no "holy brace wars" can occur when using indentation. In C (also C++ and Java), starting<br />

braces may be placed on the same line as the header statement, or may start the very next line, or may<br />

be indented on the next line. Some like it one way, some prefer the other, etc. You get the picture.<br />

3.1.5. Multiple Statements on a Single Line ( ; )<br />

The semicolon ( ; ) allows multiple statements on a single line given that neither statement starts a new<br />

code block. Here is a sample snip using the semicolon:<br />

import sys; x = 'foo'; sys.stdout.write(x + '\n')

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!