Home > Dive Into Python > Python: a 5-minute review | << >> |
diveintopython.org | |
Python for experienced programmers | |
Chapter 1. Getting To Know Python
Here is a complete, working Python program.
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.
You can document a Python function by giving it a doc string.
A function, like everything else in Python, is an object.
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.
Python modules are objects and have several useful attributes. You can use this to easily test your modules as you write them.
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.
Lists are Python's workhorse datatype. If your only experience with lists is arrays in Visual Basic or Lists in Java, brace yourself for Python lists.
A tuple is an immutable list. A tuple can not be changed in any way once it is created.
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.
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.
List mapping is a powerful way to transform data by applying a function to every element in a list.
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.
The odbchelper.py program and its output should now make perfect sense.
Chapter 2. The Power Of Introspection
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 Chapter 1. Don't worry if the rest of the code looks intimidating; you'll learn all about it throughout this chapter.
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.
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.
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).
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.
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.
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.
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.
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.
The apihelper.py program and its output should now make perfect sense.
Chapter 3. An Object-Oriented Framework
Here is a complete, working Python program. Read the doc strings to get an overview of what it 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.
Looking over this program, several things should look vaguely familiar.
Like most languages, Python has the concept of private functions, which can not be accessed outside their module, and private class methods, which can not be accessed outside their class. But the method of declaring private functions and methods is a little peculiar.
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.
One of the cooler programming shortcuts in Python is using sequences to assign multiple values at once.
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.
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++.
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.
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.
There are more special methods than just __getitem__ and __setitem__. Some of them let you emulate functionality that you may not even know about.
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.
« Handling exceptions | Tips and tricks » |