Online Book Reader

Home Category

Learning Python - Mark Lutz [192]

By Root 1549 0
on a cold winter’s day!); frankly, though, I couldn’t think of a better pass example at this point in the book.

We’ll see other places where pass makes more sense later—for instance, to ignore exceptions caught by try statements, and to define empty class objects with attributes that behave like “structs” and “records” in other languages. A pass is also sometime coded to mean “to be filled in later,” to stub out the bodies of functions temporarily:

def func1():

pass # Add real code here later

def func2():

pass

We can’t leave the body empty without getting a syntax error, so we say pass instead.

* * *

Note


Version skew note: Python 3.0 (but not 2.6) allows ellipses coded as ... (literally, three consecutive dots) to appear any place an expression can. Because ellipses do nothing by themselves, this can serve as an alternative to the pass statement, especially for code to be filled in later—a sort of Python “TBD”:

def func1():

... # Alternative to pass

def func2():

...

func1() # Does nothing if called

Ellipses can also appear on the same line as a statement header and may be used to initialize variable names if no specific type is required:

def func1(): ... # Works on same line too

def func2(): ...

>>> X = ... # Alternative to None

>>> X

Ellipsis

This notation is new in Python 3.0 (and goes well beyond the original intent of ... in slicing extensions), so time will tell if it becomes widespread enough to challenge pass and None in these roles.

* * *

continue

The continue statement causes an immediate jump to the top of a loop. It also sometimes lets you avoid statement nesting. The next example uses continue to skip odd numbers. This code prints all even numbers less than 10 and greater than or equal to 0. Remember, 0 means false and % is the remainder of division operator, so this loop counts down to 0, skipping numbers that aren’t multiples of 2 (it prints 8 6 4 2 0):

x = 10

while x:

x = x−1 # Or, x -= 1

if x % 2 != 0: continue # Odd? -- skip print

print(x, end=' ')

Because continue jumps to the top of the loop, you don’t need to nest the print statement inside an if test; the print is only reached if the continue is not run. If this sounds similar to a “goto” in other languages, it should. Python has no “goto” statement, but because continue lets you jump about in a program, many of the warnings about readability and maintainability you may have heard about goto apply. continue should probably be used sparingly, especially when you’re first getting started with Python. For instance, the last example might be clearer if the print were nested under the if:

x = 10

while x:

x = x−1

if x % 2 == 0: # Even? -- print

print(x, end=' ')

break

The break statement causes an immediate exit from a loop. Because the code that follows it in the loop is not executed if the break is reached, you can also sometimes avoid nesting by including a break. For example, here is a simple interactive loop (a variant of a larger example we studied in Chapter 10) that inputs data with input (known as raw_input in Python 2.6) and exits when the user enters “stop” for the name request:

>>> while True:

... name = input('Enter name:')

... if name == 'stop': break

... age = input('Enter age: ')

... print('Hello', name, '=>', int(age) ** 2)

...

Enter name:mel

Enter age: 40

Hello mel => 1600

Enter name:bob

Enter age: 30

Hello bob => 900

Enter name:stop

Notice how this code converts the age input to an integer with int before raising it to the second power; as you’ll recall, this is necessary because input returns user input as a string. In Chapter 35, you’ll see that input also raises an exception at end-of-file (e.g., if the user types Ctrl-Z or Ctrl-D); if this matters, wrap input in try statements.

Loop else

When combined with the loop else clause, the break statement can often eliminate the need for the search status flags used in other languages. For instance, the following piece of code determines whether a positive integer y is prime by searching

Return Main Page Previous Page Next Page

®Online Book Reader