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.

8.9. pass Statement<br />

One <strong>Python</strong> statement not found in C is the pass statement. Because <strong>Python</strong> does not use curly braces<br />

to delimit blocks of code, there are places where code is syntactically required. We do not have the<br />

equivalent empty braces or single semicolon the way C does to indicate "do nothing." If you use a<br />

<strong>Python</strong> statement that expects a sub-block of code or suite, and one is not present, you will get a syntax<br />

error condition. For this reason, we have pass, a statement that does absolutely nothingit is a true NOP,<br />

to steal the "No OPeration" assembly code jargon. Style- and development-wise, pass is also useful in<br />

places where your code will eventually go, but has not been written yet (in stubs, for example):<br />

def foo_func():<br />

pass<br />

or<br />

if user_choice == 'do_calc':<br />

pass<br />

else:<br />

pass<br />

This code structure is helpful during the development or debugging stages because you want the<br />

structure to be there while the code is being created, but you do not want it to interfere with the other<br />

parts of the code that have been completed already. In places where you want nothing to execute, pass<br />

is a good tool to have in the box.<br />

Another popular place is with exception handling, which we will take a look at in Chapter 10; this is<br />

where you can track an error if it occurs, but take no action if it is not fatal (you just want to keep a<br />

record of the event or perform an operation under the covers if an error occurs).

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

Saved successfully!

Ooh no, something went wrong!