<< >>

diveintopython.org |

Python for experienced programmers

## Appendix B. A 5-minute review

Chapter 1. Getting To Know Python

- 1.1. Diving in

  > Here is a complete, working Python program.

- 1.2. Declaring functions

  > Python has functions like most other languages, but it does not have separate interface and implementation declarations like C++ or Java. When you need a function, just declare it and code it.

- 1.3. Documenting functions

  > You can document a Python function by giving it a `doc string`.

- 1.4. Everything is an object

  > A function, like everything else in Python, is an object.

- 1.5. Indenting code

  > Python functions have no explicit `begin` or `end`, or any brackets or braces that would mark where the function code starts and stops. The only delimiter is a colon (“`:`”) and the indentation of the code itself.

- 1.6. Testing modules

  > Python modules are objects and have several useful attributes. You can use this to easily test your modules as you write them.

- 1.7. Dictionaries 101

  > One of Python's built-in datatypes is the dictionary, which defines one-to-one relationships between keys and values. This is like an associative array in Perl, a `Map` in Java, or the `Scripting.Dictionary` object in VBScript.

- 1.8. Lists 101

  > Lists are Python's workhorse datatype. If your only experience with lists is arrays in Visual Basic or `List`s in Java, brace yourself for Python lists.

- 1.9. Tuples 101

  > A tuple is an immutable list. A tuple can not be changed in any way once it is created.

- 1.10. Defining variables

  > Python has local and global variables like most other languages, but it has no explicit variable declarations. Variables spring into existence by being assigned a value, and are automatically destroyed when they go out of scope.

- 1.11. Formatting strings

  > Python supports formatting values in strings, like the `sprintf` function in C. The most basic usage is to simply insert a value in the place of the `%s` placeholder.

- 1.12. Mapping lists

  > List mapping is a powerful way to transform data by applying a function to every element in a list.

- 1.13. Joining lists and splitting strings

  > You have a list of key-value pairs in the form `key=value`, and you want to join them into a single string. To join any list of strings into a single string, use the `join` method of a string object.

- 1.14. Summary

  > The `odbchelper.py` program and its output should now make perfect sense.

Chapter 2. The Power Of Introspection

- 2.1. Diving in

  > Here is a complete, working Python program. You should understand a good deal about it just by looking at it. The numbered lines illustrate concepts covered in Getting To Know Python. Don't worry if the rest of the code looks intimidating; you'll learn all about it throughout this chapter.

- 2.2. Importing modules using from module import

  > Python has two ways of importing modules. Both are useful, and you should know when to use each. One way, `import module`, you've already seen in chapter 1. The other way accomplishes the same thing but works in subtlely and importantly different ways.

- 2.3. Optional and named arguments

  > Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. Futhermore, arguments can be specified in any order by using named arguments. Stored procedures in SQL Server Transact/SQL can do this; if you're a SQL Server scripting guru, you can skim this part.

- 2.4. type, str, dir, and other built-in functions

  > Python has a small set of extremely useful built-in functions. All other functions are partitioned off into modules. This was actually a conscious design decision, to keep the core language from getting bloated like other scripting languages (cough cough, Visual Basic).

- 2.5. Getting object references with getattr

  > You already know that Python functions are objects. What you don't know is that you can get a reference to a function without knowing its name until run-time, using the `getattr` function.

- 2.6. Filtering lists

  > As you know, Python has powerful capabilities for mapping lists into other lists. This can be combined with a filtering mechanism, where some elements in the list are mapped while others are skipped entirely.

- 2.7. The and-or trick

  > If you're a C hacker, you are certainly familiar with the `bool ? `a` : `b`` expression, which evaluates to `a` if `bool` is true, and `b` otherwise. Like many programming tricks, this is seductively convenient. You can accomplish the same thing in Python, but you need to understand precisely how it works to avoid a subtle pitfall.

- 2.8. Using lambda functions

  > Python supports an interesting syntax that lets you define one-line mini-functions on the fly. Borrowed from Lisp, these so-called `lambda` functions can be used anywhere a function is required.

- 2.9. Putting it all together

  > The last line of code, the only one we haven't deconstructed yet, is the one that does all the work. But by now the work is easy, because everything we need is already set up just the way we need it. All the dominoes are in place; it's time to knock them down.

- 2.10. Summary

  > The `apihelper.py` program and its output should now make perfect sense.

Chapter 3. An Object-Oriented Framework

- 3.1. Diving in

  > Here is a complete, working Python program. Read the `doc string`s of the module, the classes, and the functions to get an overview of what this program does and how it works. As usual, don't worry about the stuff you don't understand; that's what the rest of the chapter is for.

- 3.2. Defining classes

  > Python is fully object-oriented: you can define your own classes, inherit from your own or built-in classes, and instantiate the classes you've defined.

- 3.3. Instantiating classes

  > Instantiating classes in Python is straightforward. To instantiate a class, simply call the class as if it were a function, passing the arguments that the `__init__` method defines. The return value will be the newly created object. There is no explicit `new` operator like C++.

- 3.4. UserDict: a wrapper class

  > As you've seen, `FileInfo` is a class that acts like a dictionary. To explore this further, let's look at the `UserDict` class in the `UserDict` module, which is the ancestor of our `FileInfo` class. This is nothing special; the class is written in Python and stored in a `.py` file, just like our code. In particular, it's stored in the `lib` directory in your Python installation.

- 3.5. Special class methods

  > In addition to normal class methods, there are a number of special methods which Python classes can define. Instead of being called directly in code (like normal methods), special methods are called for you by Python in particular circumstances or when specific syntax is used.

- 3.6. Advanced special class methods

  > There are more special methods than just `__getitem__` and `__setitem__`. Some of them let you emulate functionality that you may not even know about.

- 3.7. Class attributes

  > You already know about data attributes, which are variables owned by a specific instance of a class. Python also supports class attributes, which are variables owned by the class itself.

- 3.8. Private functions

  > Like most languages, Python has the concept of private functions, which can not be called from outside their module; private class methods, which can not be called from outside their class; and private attributes, which can not be accessed from outside their class. Unlike most languages, whether a Python function, method, or attribute is private or public is determined entirely by its name.

- 3.9. Handling exceptions

  > Like many object-oriented languages, Python has exception handling via `try...except` blocks. C++, Java, and Delphi have always had this; Visual Basic is allegedly getting it in version 7, Powerbuilder in version 8. The syntax in Python is similar to C++, Java, and Delphi.

- 3.10. File objects

  > Python has a built-in function, `open`, for opening a file on disk. `open` returns a file object, which has methods and attributes for getting information about and manipulating the opened file.

- 3.11. for loops

  > Like most other languages, Python has `for` loops. The only reason you haven't seen them until now is that Python is good at so many other things that you don't need them as often.

- 3.12. Assigning multiple values at once

  > One of the cooler programming shortcuts in Python is using sequences to assign multiple values at once.

- 3.13. More on modules

  > Sorry, you've reached the end of the book that's been written so far. Please check back at http://diveintopython.org/ to see if there are any updates.

« Further reading | Tips and tricks »