Slices can be used not only in the right side of assignment, but also in the left one.
Slices have third argument: step. Here we'll select all the odd elements of a list:
There's an example of awfully quirky code in http://www.python.org/dev/peps/pep-3333/#the-server-gateway-side, where they use triple assignment with slices like this:
If you want to protect the percent sign in sprintf-style template, use double percent sign. This might be used for secondary expansion within the same template:
Sprintf supports expansion not only from tuple, but from a dict as well:
Another nice usage of sprintf is to combine it with locals/globals to output values of all the variables in the namespace.
Protocols for getters/setters/deleters: property and descriptor
There was a popular tradition in various programming languages, such as Java, to specify methods, called setters and getters in classes of APIs (Django or GTK are examples of API; by saying API I mean libraries and/or engines) to be called when the value of a field (term field is equivalent to term property, equivalent to instance variable) of that class's object is get or set. Python adopts this tradition through property and descriptor protocols, supplying getters, setters and deleters.
Suppose that you want an attribute to be dynamically calculated from values of other attributes. It makes sense in case those get changed:
Built-in function property can be used as a decorator for a getter-method. In this example we used rec.area as if it was a variable, not a method, thanks to @property decorator. setter and deleter decorators are called @foo.setter and @foo.deleter, where foo is the name of property, you want to set.
There's also a built-in class, named property. Given set, get and delete methods, it creates a property from them:
pdb can debug function, called from another module. It has 2 types of syntax for that:
Note that you have to see "another_module" in your current namespace to set breakpoint within, thus you import it. Also, note that you shouldn't set breakpoint onto function definition line, cause it is not executed and never reached! For that purpose you can use alternative syntax b module:function.
http://winpdb.org/docs/breakpoints/ - useful examples
http://moinmo.in/DebugWithWinpdb - attach debugger to already running script
http://habrahabr.ru/post/84629/ - about poor performance of GIL (in russian)
http://dabeaz.blogspot.ru/2010/01/presentation-on-new-python-gil.html - about poor performance of GIL (in english)