* Menu:
* Introduction:: Introduction
* Trivia:: Trivia
* Overview of Available Modules:: Overview of Available Modules
* Common Questions::
* Advanced Questions:: Advanced Questions
* Additional Information::
* Obtaining:: Obtaining
Introduction
************
Welcome to the Python Frequently Asked Questions List. This list is
maintained by a neophyte to Python and its purpose is to serve as a
reference source for questions that I have had as a beginner that
others will most likely have also.
In case you don't know what Python is, it is an object-oriented,
extensible, interpreted programming language that bridges the gap
between C and shell prorgramming and is thus ideally suited for
"throw-away" programming and rapid prototying but has also been used for
major software development.
It competes well with TCL, Perl and Emacs Lisp - three popular
feature-packed interpreted languages. Among its strong points:
* The STDWIN class which allows the programmer to use generic graphics
requests and events so that programs are immediate portable to
X-Windows, Macintosh, Atari ST, and IBM PC.
* The regular expression module allows for easy string manipulation.
* Extensible through the development of new classes or new modules.
* Python routines are calling from the C programming language as well as
experimentally from Emacs Lisp.
The latest version of the Python FAQ is available by contacting the
author. Requests for the latest version or any other suggestions should
be sent to brannon@jove.cs.caltech.edu
Email jbw@cs.bu.edu for details about the League for Programming
Freedom.
Trivia
******
The Author
==========
The author of Python, Guido van Rossum, received a Master's degree in
mathematics and
computer science from the University of Amsterdam in 1982. He has
since been employed by CWI, a government-sponsored research institute
in the Netherlands employing about 200 people. CWI's research themes
include large areas of computer science and mathematics, pure as well
as applied. Guido has been involved in several projects at CWI: the
design and implementation of ABC, a programming language and
environment for programming by non-expert users, and the Amoeba
project, a highly advanced distributed operating system developed
jointly with the Free University of Amsterdam. In 1991 he joined a
newly formed multimedia research project at CWI, which is
investigating issues of synchronization of independent data streams,
as well as operating support and user interfaces for multimedia
applications. In his "spare time" he developed several freely
available software packages such as STDWIN, a library for portable use
of windowing interfaces and Python, an interpreted prototyping and
scripting language. He can be reached electronically at guido@cwi.nl.
(If you want to summarize his current work and interests in one word,
"multimedia" would be it.)
Since he named the programming language Python, he obviously likes to
watch Monty Python programs.
He also has some sort of program that automatically generates curious
quotes. Some of the quirkiest:
--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"Kemal Ataturk had an entire menagerie called Abdul"
--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"Did you say knives?"
"Rotating knives, yes"
--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"Repo Man's got all night, *every* night"
The Language
============
* The language is named after Monty Python, not a reptile.
* There are two operators to test for inequality `<>' and `!='
because Guido could not figure which he liked best
Overview of Available Modules
*****************************
For each type of module (built-in, standard, etc.) an exhaustive listing
of modules is not given. Instead, the more powerful and useful ones are
listed in order to whet your appetite for Python.
Built-In Modules
================
regex
-----
The `regex' module allows you to set up configurable regular
expressions which by default uses Emacs-style matching. For more info
on denoting literal characters in Python see *Note Common Questions::.
marshal
-------
The `marshal' module allows you to write Python values in a
machine-independant format so that a program which writes data to a file
will port across architectures
Standard Modules
================
string
------
Although there are a host of string operations available, the most
salient ones are listed below:
* String splitting based on a string. No string splitting based on a
regexp. Given the string `'Bill Jones:2424 Klickitat St.:Iceland'' we
can create a list of the colon-separated fields as follows:
string.split('Bill Jones:2424 Klickitat St.:'Iceland' , ':')
with the result being
['Bill Jones', '2424 Klickitat St.', 'Iceland']
* String creation by placing a string separator between the string items of a
tuple or list. For example, to create the colon-separated string above
from the list above we would issue the following command:
string.join(['Bill Jones', '2424 Klickitat St.', 'Iceland'], ':')
* Removal of whitespace. Given the string `' a b '' the Python
command
string.strip(' a b ')
would result in the string `'a b''
* Substring search. Given the string `'ahelluvashow'' the Python
command
string.index('ahelluvashow' , 'luva')
would return the integer 4
* Right, left, and center justification of strings. For example, we can
take the following strings: 'horse', 'mule' , 'ox' , 'guido' and by
repeatedly applying string.rjust(s, 10) where s is each of the above
strings we can get the following output:
horse
mule
ox
guido
stdwin
------
Using the stdwin module, the Python (and C) programmer can create
source code with graphic windowing which will run on all of the
following platforms:
* X-Windows
* Macintosh using Think C 4.0
* Alphanumeric Terminals
* MS-DOS with a dumb display using Microsoft C
* Atari ST using MWC or Turbo C
Of course no window system's potential is maximized under stdwin, but
this is offset by the portability advantage.
A brief vignette of methods in the stdwin module:
* event polling
* scrollbars
* color fetching
* semi-automatic menu widget creation
* cut buffers
* point, line, box drawing routines
* text widgets
unix
----
There is a BSD 4.3 socket interface. There is a posix module that
provides access to to operating system functionality that is
standardised by the C Standard and the POSIX standard. For example, one
automatically gets a dictionary of environmental variables. It allows
for changing the current working directory, forking, directory listing,
obtaining the current and parent process id and so forth. Another module
named `posixpath' allows one to easily perform file
naming and testing (ie, basename of a directory path, intelligently
join two directory paths, filename or directory tests).
Common Questions
****************
* How do you print something without having a carriage return appended to
it?
You end your print statement with a comma. For example,
i = 5
print 'i = ',
print i
will result in:
`i = 5'
as output.
* Do I have to leave the interpreter each time I want a modules source
code re-evaluated?
No. If your module name is shc.py and you have loaded it into the
`Python' interpreter. Type the following exactly: `reload(shc)'
* What are these numbers that show up after I do certain statements?
The Python interpreter prints the value of an expression statement
unless it is none -- this is done so you can use the interpreter as an
interactive calculator. The simplest way of avoiding the printing of the
value is to assign it to a dummy value.
* If I am looking for the string "\section{Loving Make}" why doesnt the
following regexp work: `sect_regex = '\\section{\(.*\)} .*''
To include a literal backslash in a regexp you must double it, but to
include one in a Python string literal you must QUADRUPLE it. So the
correct regexp is: `sect_regex = '\\\\section{.*}.*''
But the `\(' and `\)' worked fine!
As stated in the reference manual under "String Literals," a backslash
that is followed by a character that is not one of the Standard C escape
sequences such as `\n' or `\t' stays in the string. As far as
I know the only character that has a special meaning following a
backslash in regular expressions as well as a Python string literal is
the backslash.
* Ok, I am in the interpreter and I want to run a Python script and pass
it command line arguments. Can I do it?
Sure. Two methods:
1. import sys
sys.argv = [ '-c' , 'red' ]
execfile('colormap')
2. import os
os.system('colormap -c red')
Advanced Questions
******************
1. From gerry@camscan.co.uk:
How can I best to put a Python interpreter into, say, 12
processes given that each interpreter needed to interface to process
specific not just general library code and I don't
want the overhead of the core Python code in every process
given that I'd mananged to produce a shared library version of it on
my SUN
Answer:
It sounds like your problem is that you want to use a shared library
for most of the code of the Python interpreter, but you want each
interpreter to have different extensions, and you don't want to have
to build 12 different shared libraries (which would undo most of the
benefit of shared libraries).
Assuming you aren't using threads (all processes sharing the same
address space for data), it should be possible to do this using only a
tiny change in the code: move the initialized array of module init
functions (inittab in "config.c") out of the shared library into the
main programs, and initialize it differently in each program. Or
allocate some extra slots in it and dynamically add extra entries
before initializing the interpreter. The table is searched each time
a module isn't found, so you may even add entries even dynamically --
Python postpones the search for and the call to a module's init
function until the first time it is imported.
Alternatively, you could use dynamic loading (read misc/DYNLOAD in the
distribution), but this would probably be slow and seems unnecessary.
--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"That was never five minutes just now!"
"I'm afraid it was."
Additional Information
**********************
There are two python modes for emacs:
* A standard one packed with the Python distribution (in
$DISTRIB/misc/python-mode.el where DISTRIB==the top level of where
Python was untarred to) that works with the
shell mode that is still common in Emacs.
* An experimental mode by Terrence Brannon that works with comint (which along with
cmushell will replace the current shell-mode in Emacs 19) and allows one
to have a startup file named `.python' in which you can
automatically load in often-used modules. It also robustly facilitates
interprocess communication between Emacs and Python -- simple types as
well as lists may be passed freely between Emacs and Python. Other types
such as Python's dictionaries and Emacs' vectors and association and
property lists do not have converter routines yet written.
Obtaining
*********
Anonymous FTP to ftp.cwi.nl (IP address 192.16.184.180) in /pub filename
`python*.tar.Z' where * stands for a version number. This is a
compressed UNIX tar file containing the C source and LaTeX documentation
for the Python interpreter. It includes the Python library modules and
the `Stubcode' interpreter as well as many example Python programs.
Total disk space occupied by the distribution is about 3 megabytes;
compilation requires 1-3 megabytes depending on the configuration built,
the compile options, and so forth.
Terrence Brannon (brannon@jove.cs.caltech.edu)
medical biology via acupuncture and particle physics