Python Documentation

Python is supplied with a large amount of documentation. Most of the documentation is of good quality, but there are a few areas where the coverage is rather thin. Navigating the documentation using the HTML version takes practice because it is organized more like a physical book than an online document and has far too few cross-reference links between pages.

Windows users are fortunate here because for them the documentation is supplied in Windows help file format. Click Start—>All Programs—Python 2.x—Python Manuals to launch the Windows help browser. This tool has both an Index and a Search function that makes finding documentation easy. For example, to find the information about string format specifiers, simply enter "formatting" in the Index line edit and the entry "formatting, string (%)" will appear.

It is well worth skimming through the documentation. We suggest that you look at the "Library Reference" page (lib.html) to see what Python's standard library offers, and clicking through to the documentation of whichever topics are of interest. This should provide an initial impression of what is available and should also help you to establish a mental picture of where you can find the documentation you are interested in.

Note that some topics are covered under more than one heading. For example, to read about strings, see "Sequence Types", "String Methods", "String Formatting Operations", and "String Services". Similarly, for files and directories, see "File and Directory Access", "Data Compression and Archiving", and "Files and Directories".

For those who prefer printed information, the following books are worth considering.

• Core PYTHON Programming by Wesley Chun. This is a Python tutorial that may be suitable if you are completely new to Python and want a slower pace than Part I of this book provides.

• Python in a Nutshell by Alex Martelli. This is an excellent reference book that gives detailed and accurate coverage of the Python language and Python's standard library.

• Python Cookbook 2nd Edition, edited by Alex Martelli, Anna Martel-li Ravenscroft, and David Ascher. This book provides lots of small practical functions, classes, snippets, and ideas, and will help broaden any Python programmer's awareness of what can be done with Python. The recipes are also available online at http://aspn. activestate.com/ASPN/Python/Cookbook.

For online Python information, the starting point is http://www.python.org. This site is also home to the Python wiki. PyQt-specific information is provided at http://www.riverbankcomputing.co.uk. The PyQt wikiisat http:// www.diotavelli.net/PyQtWiki.

When using PyQt we have access to an additional string type, QString. Unlike Python's str and unicode, QString is mutable; this means that we can change QStrings in place, inserting and removing substrings, and changing individual characters. QString has a rather different API from that provided by str and unicode. (Qt provides QString because Qt is written in C++, which does not yet have built-in Unicode support.)

QString holds Unicode characters, but depending on which version of Python we are using, the internal representation may be different from Python's Unicode representation; this doesn't really matter, since PyQt can easily convert between unicode and QString. For example:

>>>from PyQt4.QtCore import * >>> a = QString("apple") >>> b = unicode("baker") >>> print a + b applebaker >>> type(a + b)

<class 'PyQt4.QtCore.QString'>

Here we import all the classes from the QtCore module, made available to us through the PyQt4 module. When we perform operations involving QStrings and Python strings, the resultant strings are always QStrings as the type() function reveals.

When using PyQt, Qt methods that take string arguments can be given str, unicode, or QString types, and PyQt will perform any necessary conversion automatically. Qt methods that return strings always return QStrings. In view of Python's dynamic typing, we can easily become confused and not be sure whether we have a QString or a Python string. For this reason, it is wise to decide on a policy for string usage so that we always know where we stand.

The policy we use with PyQt is as follows:

• Use type str only when working with strictly 7-bit ASCII strings or with raw 8-bit data, that is, with raw bytes.

• For strings that will be used only by PyQt functions, for example, strings that are returned by one PyQt function only to be passed at some point to another PyQt function—do not convert such strings. Simply keep them as QStrings.

• In all other cases, use unicode strings, converting QStrings to unicode as soon as possible. In other words, as soon as a QString has been returned from a Qt function, always immediately convert it to type unicode.

This policy means that we avoid making incorrect assumptions about 8-bit string encodings (because we use Unicode). It also ensures that the strings we pass to Python functions have the methods that Python expects: QStrings have different methods from str and unicode, so passing them to Python functions can lead to errors. PyQt uses QString rather than unicode because when PyQt was first created, Python's Unicode support was nowhere near as good as it is today.

Was this article helpful?

0 0
Tube Jacker

Tube Jacker

Download Tube Jacker And Discover Everything You Need To Know About Jacking Unlimited Traffic From The Video Giant. The drop dead easy way to create winning video campaigns that will FLOOD your website with unstoppable FREE traffic, all on complete and total autopilot. How to exploit a sneaky method of boosting exposure and getting your videos to the top of Google within 72 hours, guaranteed.

Get My Free Ebook


Post a comment