| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \section{Built-in Functions \label{built-in-funcs}} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The Python interpreter has a number of functions built into it that | 
					
						
							|  |  |  | are always available.  They are listed here in alphabetical order. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \setindexsubitem{(built-in function)} | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{__import__}{name\optional{, globals\optional{, locals\optional{, fromlist}}}} | 
					
						
							| 
									
										
										
										
											1999-06-10 22:09:20 +00:00
										 |  |  | This function is invoked by the | 
					
						
							|  |  |  | \keyword{import}\stindex{import} statement.  It mainly | 
					
						
							|  |  |  | exists so that you can replace it with another function that has a | 
					
						
							|  |  |  | compatible interface, in order to change the semantics of the | 
					
						
							|  |  |  | \keyword{import} statement.  For examples of why and how you would do | 
					
						
							|  |  |  | this, see the standard library modules | 
					
						
							|  |  |  | \module{ihooks}\refstmodindex{ihooks} and | 
					
						
							|  |  |  | \refmodule{rexec}\refstmodindex{rexec}.  See also the built-in module | 
					
						
							|  |  |  | \refmodule{imp}\refbimodindex{imp}, which defines some useful | 
					
						
							|  |  |  | operations out of which you can build your own | 
					
						
							|  |  |  | \function{__import__()} function. | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | For example, the statement `\code{import} \code{spam}' results in the | 
					
						
							| 
									
										
										
										
											1998-02-13 21:26:35 +00:00
										 |  |  | following call: | 
					
						
							|  |  |  | \code{__import__('spam',} \code{globals(),} \code{locals(), [])}; | 
					
						
							|  |  |  | the statement \code{from} \code{spam.ham import} \code{eggs} results | 
					
						
							|  |  |  | in \code{__import__('spam.ham',} \code{globals(),} \code{locals(),} | 
					
						
							|  |  |  | \code{['eggs'])}. | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | Note that even though \code{locals()} and \code{['eggs']} are passed | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | in as arguments, the \function{__import__()} function does not set the | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | local variable named \code{eggs}; this is done by subsequent code that | 
					
						
							|  |  |  | is generated for the import statement.  (In fact, the standard | 
					
						
							|  |  |  | implementation does not use its \var{locals} argument at all, and uses | 
					
						
							|  |  |  | its \var{globals} only to determine the package context of the | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | \keyword{import} statement.) | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | When the \var{name} variable is of the form \code{package.module}, | 
					
						
							|  |  |  | normally, the top-level package (the name up till the first dot) is | 
					
						
							|  |  |  | returned, \emph{not} the module named by \var{name}.  However, when a | 
					
						
							|  |  |  | non-empty \var{fromlist} argument is given, the module named by | 
					
						
							|  |  |  | \var{name} is returned.  This is done for compatibility with the | 
					
						
							|  |  |  | bytecode generated for the different kinds of import statement; when | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | using \samp{import spam.ham.eggs}, the top-level package \code{spam} | 
					
						
							|  |  |  | must be placed in the importing namespace, but when using \samp{from | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | spam.ham import eggs}, the \code{spam.ham} subpackage must be used to | 
					
						
							|  |  |  | find the \code{eggs} variable. | 
					
						
							| 
									
										
										
										
											1998-12-04 15:32:17 +00:00
										 |  |  | As a workaround for this behavior, use \function{getattr()} to extract | 
					
						
							|  |  |  | the desired components.  For example, you could define the following | 
					
						
							|  |  |  | helper: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | import string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def my_import(name): | 
					
						
							|  |  |  |     mod = __import__(name) | 
					
						
							|  |  |  |     components = string.split(name, '.') | 
					
						
							|  |  |  |     for comp in components[1:]: | 
					
						
							|  |  |  |         mod = getattr(mod, comp) | 
					
						
							|  |  |  |     return mod | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{abs}{x} | 
					
						
							|  |  |  |   Return the absolute value of a number.  The argument may be a plain | 
					
						
							| 
									
										
										
										
											1997-06-02 17:21:20 +00:00
										 |  |  |   or long integer or a floating point number.  If the argument is a | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  |   complex number, its magnitude is returned. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{apply}{function, args\optional{, keywords}} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | The \var{function} argument must be a callable object (a user-defined or | 
					
						
							|  |  |  | built-in function or method, or a class object) and the \var{args} | 
					
						
							| 
									
										
										
										
											1998-10-01 15:35:43 +00:00
										 |  |  | argument must be a sequence (if it is not a tuple, the sequence is | 
					
						
							|  |  |  | first converted to a tuple).  The \var{function} is called with | 
					
						
							|  |  |  | \var{args} as the argument list; the number of arguments is the the length | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | of the tuple.  (This is different from just calling | 
					
						
							|  |  |  | \code{\var{func}(\var{args})}, since in that case there is always | 
					
						
							|  |  |  | exactly one argument.) | 
					
						
							| 
									
										
										
										
											1995-10-08 01:06:46 +00:00
										 |  |  | If the optional \var{keywords} argument is present, it must be a | 
					
						
							|  |  |  | dictionary whose keys are strings.  It specifies keyword arguments to | 
					
						
							|  |  |  | be added to the end of the the argument list. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-03-19 19:10:14 +00:00
										 |  |  | \begin{funcdesc}{buffer}{object\optional{, offset\optional{, size}}} | 
					
						
							|  |  |  | The \var{object} argument must be an object that supports the | 
					
						
							|  |  |  | buffer call interface (such as strings, arrays, and buffers). A new | 
					
						
							|  |  |  | buffer object will be created which references the \var{object} argument. | 
					
						
							|  |  |  | The buffer object will be a slice from the beginning of \var{object} | 
					
						
							|  |  |  | (or from the specified \var{offset}). The slice will extend to the | 
					
						
							|  |  |  | end of \var{object} (or will have a length given by the \var{size} | 
					
						
							|  |  |  | argument). | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | \begin{funcdesc}{callable}{object} | 
					
						
							|  |  |  | Return true if the \var{object} argument appears callable, false if | 
					
						
							|  |  |  | not.  If this returns true, it is still possible that a call fails, | 
					
						
							|  |  |  | but if it is false, calling \var{object} will never succeed.  Note | 
					
						
							|  |  |  | that classes are callable (calling a class returns a new instance); | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | class instances are callable if they have a \method{__call__()} method. | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{chr}{i} | 
					
						
							|  |  |  |   Return a string of one character whose \ASCII{} code is the integer | 
					
						
							|  |  |  |   \var{i}, e.g., \code{chr(97)} returns the string \code{'a'}.  This is the | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   inverse of \function{ord()}.  The argument must be in the range [0..255], | 
					
						
							| 
									
										
										
										
											2000-04-06 14:45:19 +00:00
										 |  |  |   inclusive; \exception{ValueError} will be raised if \var{i} is | 
					
						
							|  |  |  |   outside that range. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{cmp}{x, y} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Compare the two objects \var{x} and \var{y} and return an integer | 
					
						
							|  |  |  |   according to the outcome.  The return value is negative if \code{\var{x} | 
					
						
							|  |  |  |   < \var{y}}, zero if \code{\var{x} == \var{y}} and strictly positive if | 
					
						
							|  |  |  |   \code{\var{x} > \var{y}}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{coerce}{x, y} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Return a tuple consisting of the two numeric arguments converted to | 
					
						
							|  |  |  |   a common type, using the same rules as used by arithmetic | 
					
						
							|  |  |  |   operations. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{compile}{string, filename, kind} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Compile the \var{string} into a code object.  Code objects can be | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   executed by an \keyword{exec} statement or evaluated by a call to | 
					
						
							|  |  |  |   \function{eval()}.  The \var{filename} argument should | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   give the file from which the code was read; pass e.g. \code{'<string>'} | 
					
						
							|  |  |  |   if it wasn't read from a file.  The \var{kind} argument specifies | 
					
						
							|  |  |  |   what kind of code must be compiled; it can be \code{'exec'} if | 
					
						
							| 
									
										
										
										
											1995-07-07 22:58:28 +00:00
										 |  |  |   \var{string} consists of a sequence of statements, \code{'eval'} | 
					
						
							|  |  |  |   if it consists of a single expression, or \code{'single'} if | 
					
						
							|  |  |  |   it consists of a single interactive statement (in the latter case, | 
					
						
							|  |  |  |   expression statements that evaluate to something else than | 
					
						
							|  |  |  |   \code{None} will printed). | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-02 06:04:02 +00:00
										 |  |  | \begin{funcdesc}{complex}{real\optional{, imag}} | 
					
						
							| 
									
										
										
										
											1999-03-25 21:23:26 +00:00
										 |  |  |   Create a complex number with the value \var{real} + \var{imag}*j or | 
					
						
							|  |  |  |   convert a string or number to a complex number. | 
					
						
							| 
									
										
										
										
											1997-04-02 06:04:02 +00:00
										 |  |  |   Each argument may be any numeric type (including complex). | 
					
						
							|  |  |  |   If \var{imag} is omitted, it defaults to zero and the function | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   serves as a numeric conversion function like \function{int()}, | 
					
						
							| 
									
										
										
										
											1999-03-25 21:23:26 +00:00
										 |  |  |   \function{long()} and \function{float()}; in this case it also | 
					
						
							|  |  |  |   accepts a string argument which should be a valid complex number. | 
					
						
							| 
									
										
										
										
											1997-04-02 06:04:02 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{delattr}{object, name} | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   This is a relative of \function{setattr()}.  The arguments are an | 
					
						
							| 
									
										
										
										
											1994-08-16 22:15:11 +00:00
										 |  |  |   object and a string.  The string must be the name | 
					
						
							|  |  |  |   of one of the object's attributes.  The function deletes | 
					
						
							|  |  |  |   the named attribute, provided the object allows it.  For example, | 
					
						
							| 
									
										
										
										
											1995-03-07 10:14:09 +00:00
										 |  |  |   \code{delattr(\var{x}, '\var{foobar}')} is equivalent to | 
					
						
							| 
									
										
										
										
											1994-08-16 22:15:11 +00:00
										 |  |  |   \code{del \var{x}.\var{foobar}}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-16 22:10:27 +00:00
										 |  |  | \begin{funcdesc}{dir}{\optional{object}} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Without arguments, return the list of names in the current local | 
					
						
							| 
									
										
										
										
											1997-12-30 20:38:16 +00:00
										 |  |  |   symbol table.  With an argument, attempts to return a list of valid | 
					
						
							|  |  |  |   attribute for that object.  This information is gleaned from the | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   object's \member{__dict__}, \member{__methods__} and \member{__members__} | 
					
						
							| 
									
										
										
										
											1997-12-30 20:38:16 +00:00
										 |  |  |   attributes, if defined.  The list is not necessarily complete; e.g., | 
					
						
							|  |  |  |   for classes, attributes defined in base classes are not included, | 
					
						
							|  |  |  |   and for class instances, methods are not included. | 
					
						
							|  |  |  |   The resulting list is sorted alphabetically.  For example: | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | >>> import sys | 
					
						
							|  |  |  | >>> dir() | 
					
						
							|  |  |  | ['sys'] | 
					
						
							|  |  |  | >>> dir(sys) | 
					
						
							|  |  |  | ['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout'] | 
					
						
							|  |  |  | >>>  | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{divmod}{a, b} | 
					
						
							| 
									
										
										
										
											1997-12-30 20:38:16 +00:00
										 |  |  |   Take two numbers as arguments and return a pair of numbers consisting | 
					
						
							|  |  |  |   of their quotient and remainder when using long division.  With mixed | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   operand types, the rules for binary arithmetic operators apply.  For | 
					
						
							|  |  |  |   plain and long integers, the result is the same as | 
					
						
							|  |  |  |   \code{(\var{a} / \var{b}, \var{a} \%{} \var{b})}. | 
					
						
							| 
									
										
										
										
											1999-05-06 14:46:35 +00:00
										 |  |  |   For floating point numbers the result is \code{(\var{q}, \var{a} \%{} | 
					
						
							|  |  |  |   \var{b})}, where \var{q} is usually \code{math.floor(\var{a} / | 
					
						
							|  |  |  |   \var{b})} but may be 1 less than that.  In any case \code{\var{q} * | 
					
						
							|  |  |  |   \var{b} + \var{a} \%{} \var{b}} is very close to \var{a}, if | 
					
						
							|  |  |  |   \code{\var{a} \%{} \var{b}} is non-zero it has the same sign as | 
					
						
							|  |  |  |   \var{b}, and \code{0 <= abs(\var{a} \%{} \var{b}) < abs(\var{b})}. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{eval}{expression\optional{, globals\optional{, locals}}} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   The arguments are a string and two optional dictionaries.  The | 
					
						
							| 
									
										
										
										
											1995-01-10 10:50:24 +00:00
										 |  |  |   \var{expression} argument is parsed and evaluated as a Python | 
					
						
							|  |  |  |   expression (technically speaking, a condition list) using the | 
					
						
							|  |  |  |   \var{globals} and \var{locals} dictionaries as global and local name | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  |   space.  If the \var{locals} dictionary is omitted it defaults to | 
					
						
							|  |  |  |   the \var{globals} dictionary.  If both dictionaries are omitted, the | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   expression is executed in the environment where \keyword{eval} is | 
					
						
							| 
									
										
										
										
											1995-01-10 10:50:24 +00:00
										 |  |  |   called.  The return value is the result of the evaluated expression. | 
					
						
							|  |  |  |   Syntax errors are reported as exceptions.  Example: | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | >>> x = 1 | 
					
						
							|  |  |  | >>> print eval('x+1') | 
					
						
							|  |  |  | 2 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   This function can also be used to execute arbitrary code objects | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   (e.g.\ created by \function{compile()}).  In this case pass a code | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   object instead of a string.  The code object must have been compiled | 
					
						
							|  |  |  |   passing \code{'eval'} to the \var{kind} argument. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-03-07 10:14:09 +00:00
										 |  |  |   Hints: dynamic execution of statements is supported by the | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   \keyword{exec} statement.  Execution of statements from a file is | 
					
						
							|  |  |  |   supported by the \function{execfile()} function.  The | 
					
						
							|  |  |  |   \function{globals()} and \function{locals()} functions returns the | 
					
						
							|  |  |  |   current global and local dictionary, respectively, which may be | 
					
						
							|  |  |  |   useful to pass around for use by \function{eval()} or | 
					
						
							|  |  |  |   \function{execfile()}. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{execfile}{file\optional{, globals\optional{, locals}}} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  |   This function is similar to the | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   \keyword{exec} statement, but parses a file instead of a string.  It | 
					
						
							|  |  |  |   is different from the \keyword{import} statement in that it does not | 
					
						
							|  |  |  |   use the module administration --- it reads the file unconditionally | 
					
						
							|  |  |  |   and does not create a new module.\footnote{It is used relatively | 
					
						
							|  |  |  |   rarely so does not warrant being made into a statement.} | 
					
						
							| 
									
										
										
										
											1995-01-10 10:50:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   The arguments are a file name and two optional dictionaries.  The | 
					
						
							|  |  |  |   file is parsed and evaluated as a sequence of Python statements | 
					
						
							|  |  |  |   (similarly to a module) using the \var{globals} and \var{locals} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  |   dictionaries as global and local name space.  If the \var{locals} | 
					
						
							|  |  |  |   dictionary is omitted it defaults to the \var{globals} dictionary. | 
					
						
							| 
									
										
										
										
											1995-01-10 10:50:24 +00:00
										 |  |  |   If both dictionaries are omitted, the expression is executed in the | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   environment where \function{execfile()} is called.  The return value is | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  |   \code{None}. | 
					
						
							| 
									
										
										
										
											1995-01-10 10:50:24 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{filter}{function, list} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | Construct a list from those elements of \var{list} for which | 
					
						
							|  |  |  | \var{function} returns true.  If \var{list} is a string or a tuple, | 
					
						
							|  |  |  | the result also has that type; otherwise it is always a list.  If | 
					
						
							|  |  |  | \var{function} is \code{None}, the identity function is assumed, | 
					
						
							| 
									
										
										
										
											1995-03-07 10:14:09 +00:00
										 |  |  | i.e.\ all elements of \var{list} that are false (zero or empty) are | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | removed. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{float}{x} | 
					
						
							| 
									
										
										
										
											1997-04-02 06:04:02 +00:00
										 |  |  |   Convert a string or a number to floating point.  If the argument is a | 
					
						
							| 
									
										
										
										
											1998-12-07 17:13:18 +00:00
										 |  |  |   string, it must contain a possibly signed decimal or floating point | 
					
						
							| 
									
										
										
										
											1999-02-18 16:08:36 +00:00
										 |  |  |   number, possibly embedded in whitespace; this behaves identical to | 
					
						
							|  |  |  |   \code{string.atof(\var{x})}.  Otherwise, the argument may be a plain | 
					
						
							|  |  |  |   or long integer or a floating point number, and a floating point | 
					
						
							|  |  |  |   number with the same value (within Python's floating point | 
					
						
							|  |  |  |   precision) is returned. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   \strong{Note:} When passing in a string, values for NaN\index{NaN} | 
					
						
							|  |  |  |   and Infinity\index{Infinity} may be returned, depending on the | 
					
						
							|  |  |  |   underlying C library.  The specific set of strings accepted which | 
					
						
							|  |  |  |   cause these values to be returned depends entirely on the C library | 
					
						
							|  |  |  |   and is known to vary. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-07-22 19:21:45 +00:00
										 |  |  | \begin{funcdesc}{getattr}{object, name\optional{, default}} | 
					
						
							|  |  |  |   Return the value of the named attributed of \var{object}.  \var{name} | 
					
						
							|  |  |  |   must be a string.  If the string is the name of one of the object's | 
					
						
							|  |  |  |   attributes, the result is the value of that attribute.  For example, | 
					
						
							|  |  |  |   \code{getattr(x, 'foobar')} is equivalent to \code{x.foobar}.  If the | 
					
						
							|  |  |  |   named attribute does not exist, \var{default} is returned if provided, | 
					
						
							|  |  |  |   otherwise \exception{AttributeError} is raised. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-07-07 22:58:28 +00:00
										 |  |  | \begin{funcdesc}{globals}{} | 
					
						
							|  |  |  | Return a dictionary representing the current global symbol table. | 
					
						
							|  |  |  | This is always the dictionary of the current module (inside a | 
					
						
							|  |  |  | function or method, this is the module where it is defined, not the | 
					
						
							|  |  |  | module from which it is called). | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{hasattr}{object, name} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   The arguments are an object and a string.  The result is 1 if the | 
					
						
							|  |  |  |   string is the name of one of the object's attributes, 0 if not. | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   (This is implemented by calling \code{getattr(\var{object}, | 
					
						
							|  |  |  |   \var{name})} and seeing whether it raises an exception or not.) | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{hash}{object} | 
					
						
							|  |  |  |   Return the hash value of the object (if it has one).  Hash values | 
					
						
							| 
									
										
										
										
											1997-12-30 20:38:16 +00:00
										 |  |  |   are integers.  They are used to quickly compare dictionary | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   keys during a dictionary lookup.  Numeric values that compare equal | 
					
						
							|  |  |  |   have the same hash value (even if they are of different types, e.g. | 
					
						
							|  |  |  |   1 and 1.0). | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{hex}{x} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  |   Convert an integer number (of any size) to a hexadecimal string. | 
					
						
							| 
									
										
										
										
											1997-01-14 18:44:23 +00:00
										 |  |  |   The result is a valid Python expression.  Note: this always yields | 
					
						
							|  |  |  |   an unsigned literal, e.g. on a 32-bit machine, \code{hex(-1)} yields | 
					
						
							|  |  |  |   \code{'0xffffffff'}.  When evaluated on a machine with the same | 
					
						
							|  |  |  |   word size, this literal is evaluated as -1; at a different word | 
					
						
							|  |  |  |   size, it may turn up as a large positive number or raise an | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   \exception{OverflowError} exception. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{id}{object} | 
					
						
							| 
									
										
										
										
											2000-06-29 03:46:46 +00:00
										 |  |  |   Return the `identity' of an object.  This is an integer (or long | 
					
						
							|  |  |  |   integer) which is guaranteed to be unique and constant for this | 
					
						
							|  |  |  |   object during its lifetime.  Two objects whose lifetimes are | 
					
						
							|  |  |  |   disjunct may have the same \function{id()} value.  (Implementation | 
					
						
							|  |  |  |   note: this is the address of the object.) | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-08 12:30:22 +00:00
										 |  |  | \begin{funcdesc}{input}{\optional{prompt}} | 
					
						
							| 
									
										
										
										
											1998-06-17 15:16:40 +00:00
										 |  |  |   Equivalent to \code{eval(raw_input(\var{prompt}))}. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-17 17:45:52 +00:00
										 |  |  | \begin{funcdesc}{int}{x\optional{, radix}} | 
					
						
							|  |  |  |   Convert a string or number to a plain integer.  If the argument is a | 
					
						
							|  |  |  |   string, it must contain a possibly signed decimal number | 
					
						
							|  |  |  |   representable as a Python integer, possibly embedded in whitespace; | 
					
						
							|  |  |  |   this behaves identical to \code{string.atoi(\var{x}\optional{, | 
					
						
							|  |  |  |   \var{radix}})}.  The \var{radix} parameter gives the base for the | 
					
						
							|  |  |  |   conversion and may be any integer in the range $[2, 36]$.  If | 
					
						
							|  |  |  |   \var{radix} is specified and \var{x} is not a string, | 
					
						
							|  |  |  |   \exception{TypeError} is raised. | 
					
						
							|  |  |  |   Otherwise, the argument may be a plain or | 
					
						
							|  |  |  |   long integer or a floating point number.  Conversion of floating | 
					
						
							|  |  |  |   point numbers to integers is defined by the C semantics; normally | 
					
						
							|  |  |  |   the conversion truncates towards zero.\footnote{This is ugly --- the | 
					
						
							|  |  |  |   language definition should require truncation towards zero.} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-03 16:03:27 +00:00
										 |  |  | \begin{funcdesc}{intern}{string} | 
					
						
							|  |  |  |   Enter \var{string} in the table of ``interned'' strings and return | 
					
						
							|  |  |  |   the interned string -- which is \var{string} itself or a copy. | 
					
						
							|  |  |  |   Interning strings is useful to gain a little performance on | 
					
						
							|  |  |  |   dictionary lookup -- if the keys in a dictionary are interned, and | 
					
						
							|  |  |  |   the lookup key is interned, the key comparisons (after hashing) can | 
					
						
							|  |  |  |   be done by a pointer compare instead of a string compare.  Normally, | 
					
						
							|  |  |  |   the names used in Python programs are automatically interned, and | 
					
						
							|  |  |  |   the dictionaries used to hold module, class or instance attributes | 
					
						
							|  |  |  |   have interned keys.  Interned strings are immortal (i.e. never get | 
					
						
							|  |  |  |   garbage collected). | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | \begin{funcdesc}{isinstance}{object, class} | 
					
						
							|  |  |  | Return true if the \var{object} argument is an instance of the | 
					
						
							|  |  |  | \var{class} argument, or of a (direct or indirect) subclass thereof. | 
					
						
							| 
									
										
										
										
											1997-12-02 19:15:01 +00:00
										 |  |  | Also return true if \var{class} is a type object and \var{object} is | 
					
						
							|  |  |  | an object of that type.  If \var{object} is not a class instance or a | 
					
						
							|  |  |  | object of the given type, the function always returns false.  If | 
					
						
							|  |  |  | \var{class} is neither a class object nor a type object, a | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | \exception{TypeError} exception is raised. | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{issubclass}{class1, class2} | 
					
						
							|  |  |  | Return true if \var{class1} is a subclass (direct or indirect) of | 
					
						
							|  |  |  | \var{class2}.  A class is considered a subclass of itself.  If either | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | argument is not a class object, a \exception{TypeError} exception is | 
					
						
							|  |  |  | raised. | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{len}{s} | 
					
						
							|  |  |  |   Return the length (the number of items) of an object.  The argument | 
					
						
							|  |  |  |   may be a sequence (string, tuple or list) or a mapping (dictionary). | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-02 17:21:20 +00:00
										 |  |  | \begin{funcdesc}{list}{sequence} | 
					
						
							|  |  |  | Return a list whose items are the same and in the same order as | 
					
						
							|  |  |  | \var{sequence}'s items.  If \var{sequence} is already a list, | 
					
						
							|  |  |  | a copy is made and returned, similar to \code{\var{sequence}[:]}.   | 
					
						
							|  |  |  | For instance, \code{list('abc')} returns | 
					
						
							|  |  |  | returns \code{['a', 'b', 'c']} and \code{list( (1, 2, 3) )} returns | 
					
						
							|  |  |  | \code{[1, 2, 3]}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-07-07 22:58:28 +00:00
										 |  |  | \begin{funcdesc}{locals}{} | 
					
						
							|  |  |  | Return a dictionary representing the current local symbol table. | 
					
						
							| 
									
										
										
										
											1998-06-18 16:45:34 +00:00
										 |  |  | \strong{Warning:} the contents of this dictionary should not be | 
					
						
							|  |  |  | modified; changes may not affect the values of local variables used by  | 
					
						
							|  |  |  | the interpreter. | 
					
						
							| 
									
										
										
										
											1995-07-07 22:58:28 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{long}{x} | 
					
						
							| 
									
										
										
										
											1997-04-02 06:04:02 +00:00
										 |  |  |   Convert a string or number to a long integer.  If the argument is a | 
					
						
							| 
									
										
										
										
											1998-12-07 17:13:18 +00:00
										 |  |  |   string, it must contain a possibly signed decimal number of | 
					
						
							| 
									
										
										
										
											1997-04-02 06:04:02 +00:00
										 |  |  |   arbitrary size, possibly embedded in whitespace; | 
					
						
							|  |  |  |   this behaves identical to \code{string.atol(\var{x})}. | 
					
						
							|  |  |  |   Otherwise, the argument may be a plain or | 
					
						
							| 
									
										
										
										
											1997-12-30 20:38:16 +00:00
										 |  |  |   long integer or a floating point number, and a long integer with | 
					
						
							| 
									
										
										
										
											1997-04-02 06:04:02 +00:00
										 |  |  |   the same value is returned.    Conversion of floating | 
					
						
							|  |  |  |   point numbers to integers is defined by the C semantics; | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   see the description of \function{int()}. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{map}{function, list, ...} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | Apply \var{function} to every item of \var{list} and return a list | 
					
						
							|  |  |  | of the results.  If additional \var{list} arguments are passed,  | 
					
						
							|  |  |  | \var{function} must take that many arguments and is applied to | 
					
						
							|  |  |  | the items of all lists in parallel; if a list is shorter than another | 
					
						
							|  |  |  | it is assumed to be extended with \code{None} items.  If | 
					
						
							|  |  |  | \var{function} is \code{None}, the identity function is assumed; if | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | there are multiple list arguments, \function{map()} returns a list | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | consisting of tuples containing the corresponding items from all lists | 
					
						
							|  |  |  | (i.e. a kind of transpose operation).  The \var{list} arguments may be | 
					
						
							|  |  |  | any kind of sequence; the result is always a list. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-11-25 18:53:05 +00:00
										 |  |  | \begin{funcdesc}{max}{s\optional{, args...}} | 
					
						
							|  |  |  | With a single argument \var{s}, return the largest item of a | 
					
						
							|  |  |  | non-empty sequence (e.g., a string, tuple or list).  With more than | 
					
						
							|  |  |  | one argument, return the largest of the arguments. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-11-25 18:53:05 +00:00
										 |  |  | \begin{funcdesc}{min}{s\optional{, args...}} | 
					
						
							|  |  |  | With a single argument \var{s}, return the smallest item of a | 
					
						
							|  |  |  | non-empty sequence (e.g., a string, tuple or list).  With more than | 
					
						
							|  |  |  | one argument, return the smallest of the arguments. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{oct}{x} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  |   Convert an integer number (of any size) to an octal string.  The | 
					
						
							| 
									
										
										
										
											1997-01-14 18:44:23 +00:00
										 |  |  |   result is a valid Python expression.  Note: this always yields | 
					
						
							|  |  |  |   an unsigned literal, e.g. on a 32-bit machine, \code{oct(-1)} yields | 
					
						
							|  |  |  |   \code{'037777777777'}.  When evaluated on a machine with the same | 
					
						
							|  |  |  |   word size, this literal is evaluated as -1; at a different word | 
					
						
							|  |  |  |   size, it may turn up as a large positive number or raise an | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   \exception{OverflowError} exception. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{open}{filename\optional{, mode\optional{, bufsize}}} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Return a new file object (described earlier under Built-in Types). | 
					
						
							| 
									
										
										
										
											1994-05-03 14:46:50 +00:00
										 |  |  |   The first two arguments are the same as for \code{stdio}'s | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   \cfunction{fopen()}: \var{filename} is the file name to be opened, | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   \var{mode} indicates how the file is to be opened: \code{'r'} for | 
					
						
							|  |  |  |   reading, \code{'w'} for writing (truncating an existing file), and | 
					
						
							| 
									
										
										
										
											1998-01-14 14:51:31 +00:00
										 |  |  |   \code{'a'} opens it for appending (which on \emph{some} \UNIX{} | 
					
						
							|  |  |  |   systems means that \emph{all} writes append to the end of the file, | 
					
						
							| 
									
										
										
										
											1996-05-02 15:16:59 +00:00
										 |  |  |   regardless of the current seek position). | 
					
						
							| 
									
										
										
										
											1998-07-29 21:05:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Modes \code{'r+'}, \code{'w+'} and \code{'a+'} open the file for | 
					
						
							|  |  |  |   updating (note that \code{'w+'} truncates the file).  Append | 
					
						
							|  |  |  |   \code{'b'} to the mode to open the file in binary mode, on systems | 
					
						
							|  |  |  |   that differentiate between binary and text files (else it is | 
					
						
							|  |  |  |   ignored).  If the file cannot be opened, \exception{IOError} is | 
					
						
							|  |  |  |   raised. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-05 21:22:41 +00:00
										 |  |  |   If \var{mode} is omitted, it defaults to \code{'r'}.  When opening a  | 
					
						
							|  |  |  |   binary file, you should append \code{'b'} to the \var{mode} value | 
					
						
							|  |  |  |   for improved portability.  (It's useful even on systems which don't | 
					
						
							|  |  |  |   treat binary and text files differently, where it serves as | 
					
						
							| 
									
										
										
										
											1999-08-05 13:43:08 +00:00
										 |  |  |   documentation.) | 
					
						
							|  |  |  |   \index{line-buffered I/O}\index{unbuffered I/O}\index{buffer size, I/O} | 
					
						
							|  |  |  |   \index{I/O control!buffering} | 
					
						
							|  |  |  |   The optional \var{bufsize} argument specifies the | 
					
						
							| 
									
										
										
										
											1999-04-05 21:22:41 +00:00
										 |  |  |   file's desired buffer size: 0 means unbuffered, 1 means line | 
					
						
							|  |  |  |   buffered, any other positive value means use a buffer of | 
					
						
							|  |  |  |   (approximately) that size.  A negative \var{bufsize} means to use | 
					
						
							|  |  |  |   the system default, which is usually line buffered for for tty | 
					
						
							|  |  |  |   devices and fully buffered for other files.  If omitted, the system | 
					
						
							|  |  |  |   default is used.\footnote{ | 
					
						
							|  |  |  |     Specifying a buffer size currently has no effect on systems that | 
					
						
							|  |  |  |     don't have \cfunction{setvbuf()}.  The interface to specify the | 
					
						
							|  |  |  |     buffer size is not done using a method that calls | 
					
						
							|  |  |  |     \cfunction{setvbuf()}, because that may dump core when called | 
					
						
							|  |  |  |     after any I/O has been performed, and there's no reliable way to | 
					
						
							|  |  |  |     determine whether this is the case.} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{ord}{c} | 
					
						
							| 
									
										
										
										
											2000-04-06 14:43:12 +00:00
										 |  |  |   Return the \ASCII{} value of a string of one character or a Unicode | 
					
						
							|  |  |  |   character.  E.g., \code{ord('a')} returns the integer \code{97}, | 
					
						
							|  |  |  |   \code{ord(u'\\u2020')} returns \code{8224}.  This is the inverse of | 
					
						
							|  |  |  |   \function{chr()} for strings and of \function{unichr()} for Unicode | 
					
						
							|  |  |  |   characters. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{pow}{x, y\optional{, z}} | 
					
						
							| 
									
										
										
										
											1994-08-12 13:13:50 +00:00
										 |  |  |   Return \var{x} to the power \var{y}; if \var{z} is present, return | 
					
						
							|  |  |  |   \var{x} to the power \var{y}, modulo \var{z} (computed more | 
					
						
							| 
									
										
										
										
											1998-04-03 07:15:54 +00:00
										 |  |  |   efficiently than \code{pow(\var{x}, \var{y}) \%\ \var{z}}). | 
					
						
							| 
									
										
										
										
											1994-08-12 13:13:50 +00:00
										 |  |  |   The arguments must have | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   numeric types.  With mixed operand types, the rules for binary | 
					
						
							|  |  |  |   arithmetic operators apply.  The effective operand type is also the | 
					
						
							|  |  |  |   type of the result; if the result is not expressible in this type, the | 
					
						
							| 
									
										
										
										
											1994-08-08 12:30:22 +00:00
										 |  |  |   function raises an exception; e.g., \code{pow(2, -1)} or \code{pow(2, | 
					
						
							|  |  |  |   35000)} is not allowed. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{range}{\optional{start,} stop\optional{, step}} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   This is a versatile function to create lists containing arithmetic | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   progressions.  It is most often used in \keyword{for} loops.  The | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   arguments must be plain integers.  If the \var{step} argument is | 
					
						
							|  |  |  |   omitted, it defaults to \code{1}.  If the \var{start} argument is | 
					
						
							|  |  |  |   omitted, it defaults to \code{0}.  The full form returns a list of | 
					
						
							|  |  |  |   plain integers \code{[\var{start}, \var{start} + \var{step}, | 
					
						
							|  |  |  |   \var{start} + 2 * \var{step}, \ldots]}.  If \var{step} is positive, | 
					
						
							|  |  |  |   the last element is the largest \code{\var{start} + \var{i} * | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  |   \var{step}} less than \var{stop}; if \var{step} is negative, the last | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   element is the largest \code{\var{start} + \var{i} * \var{step}} | 
					
						
							| 
									
										
										
										
											1998-04-03 07:15:54 +00:00
										 |  |  |   greater than \var{stop}.  \var{step} must not be zero (or else | 
					
						
							|  |  |  |   \exception{ValueError} is raised).  Example: | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | >>> range(10) | 
					
						
							|  |  |  | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | 
					
						
							|  |  |  | >>> range(1, 11) | 
					
						
							|  |  |  | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | 
					
						
							|  |  |  | >>> range(0, 30, 5) | 
					
						
							|  |  |  | [0, 5, 10, 15, 20, 25] | 
					
						
							|  |  |  | >>> range(0, 10, 3) | 
					
						
							|  |  |  | [0, 3, 6, 9] | 
					
						
							|  |  |  | >>> range(0, -10, -1) | 
					
						
							|  |  |  | [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] | 
					
						
							|  |  |  | >>> range(0) | 
					
						
							|  |  |  | [] | 
					
						
							|  |  |  | >>> range(1, 0) | 
					
						
							|  |  |  | [] | 
					
						
							|  |  |  | >>>  | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-08 12:30:22 +00:00
										 |  |  | \begin{funcdesc}{raw_input}{\optional{prompt}} | 
					
						
							|  |  |  |   If the \var{prompt} argument is present, it is written to standard output | 
					
						
							|  |  |  |   without a trailing newline.  The function then reads a line from input, | 
					
						
							|  |  |  |   converts it to a string (stripping a trailing newline), and returns that. | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   When \EOF{} is read, \exception{EOFError} is raised. Example: | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | >>> s = raw_input('--> ') | 
					
						
							|  |  |  | --> Monty Python's Flying Circus | 
					
						
							|  |  |  | >>> s | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | "Monty Python's Flying Circus" | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | >>>  | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											1997-06-02 17:21:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | If the \module{readline} module was loaded, then | 
					
						
							|  |  |  | \function{raw_input()} will use it to provide elaborate | 
					
						
							| 
									
										
										
										
											1997-06-02 17:21:20 +00:00
										 |  |  | line editing and history features. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-06 23:10:51 +00:00
										 |  |  | \begin{funcdesc}{reduce}{function, sequence\optional{, initializer}} | 
					
						
							|  |  |  | Apply \var{function} of two arguments cumulatively to the items of | 
					
						
							|  |  |  | \var{sequence}, from left to right, so as to reduce the sequence to | 
					
						
							|  |  |  | a single value.  For example, | 
					
						
							|  |  |  | \code{reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])} calculates | 
					
						
							|  |  |  | \code{((((1+2)+3)+4)+5)}. | 
					
						
							|  |  |  | If the optional \var{initializer} is present, it is placed before the | 
					
						
							|  |  |  | items of the sequence in the calculation, and serves as a default when | 
					
						
							|  |  |  | the sequence is empty. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{reload}{module} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | Re-parse and re-initialize an already imported \var{module}.  The | 
					
						
							|  |  |  | argument must be a module object, so it must have been successfully | 
					
						
							|  |  |  | imported before.  This is useful if you have edited the module source | 
					
						
							|  |  |  | file using an external editor and want to try out the new version | 
					
						
							|  |  |  | without leaving the Python interpreter.  The return value is the | 
					
						
							|  |  |  | module object (i.e.\ the same as the \var{module} argument). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | There are a number of caveats: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If a module is syntactically correct but its initialization fails, the | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | first \keyword{import} statement for it does not bind its name locally, | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | but does store a (partially initialized) module object in | 
					
						
							|  |  |  | \code{sys.modules}.  To reload the module you must first | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | \keyword{import} it again (this will bind the name to the partially | 
					
						
							|  |  |  | initialized module object) before you can \function{reload()} it. | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | When a module is reloaded, its dictionary (containing the module's | 
					
						
							|  |  |  | global variables) is retained.  Redefinitions of names will override | 
					
						
							|  |  |  | the old definitions, so this is generally not a problem.  If the new | 
					
						
							|  |  |  | version of a module does not define a name that was defined by the old | 
					
						
							|  |  |  | version, the old definition remains.  This feature can be used to the | 
					
						
							|  |  |  | module's advantage if it maintains a global table or cache of objects | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | --- with a \keyword{try} statement it can test for the table's presence | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | and skip its initialization if desired. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | It is legal though generally not very useful to reload built-in or | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | dynamically loaded modules, except for \module{sys}, \module{__main__} | 
					
						
							|  |  |  | and \module{__builtin__}.  In certain cases, however, extension | 
					
						
							|  |  |  | modules are not designed to be initialized more than once, and may | 
					
						
							|  |  |  | fail in arbitrary ways when reloaded. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If a module imports objects from another module using \keyword{from} | 
					
						
							|  |  |  | \ldots{} \keyword{import} \ldots{}, calling \function{reload()} for | 
					
						
							|  |  |  | the other module does not redefine the objects imported from it --- | 
					
						
							|  |  |  | one way around this is to re-execute the \keyword{from} statement, | 
					
						
							|  |  |  | another is to use \keyword{import} and qualified names | 
					
						
							|  |  |  | (\var{module}.\var{name}) instead. | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | If a module instantiates instances of a class, reloading the module | 
					
						
							|  |  |  | that defines the class does not affect the method definitions of the | 
					
						
							|  |  |  | instances --- they continue to use the old class definition.  The same | 
					
						
							|  |  |  | is true for derived classes. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{repr}{object} | 
					
						
							|  |  |  | Return a string containing a printable representation of an object. | 
					
						
							|  |  |  | This is the same value yielded by conversions (reverse quotes). | 
					
						
							|  |  |  | It is sometimes useful to be able to access this operation as an | 
					
						
							|  |  |  | ordinary function.  For many types, this function makes an attempt | 
					
						
							|  |  |  | to return a string that would yield an object with the same value | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | when passed to \function{eval()}. | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-08-24 20:30:07 +00:00
										 |  |  | \begin{funcdesc}{round}{x\optional{, n}} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   Return the floating point value \var{x} rounded to \var{n} digits | 
					
						
							|  |  |  |   after the decimal point.  If \var{n} is omitted, it defaults to zero. | 
					
						
							|  |  |  |   The result is a floating point number.  Values are rounded to the | 
					
						
							|  |  |  |   closest multiple of 10 to the power minus \var{n}; if two multiples | 
					
						
							|  |  |  |   are equally close, rounding is done away from 0 (so e.g. | 
					
						
							|  |  |  |   \code{round(0.5)} is \code{1.0} and \code{round(-0.5)} is \code{-1.0}). | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{setattr}{object, name, value} | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  |   This is the counterpart of \function{getattr()}.  The arguments are an | 
					
						
							| 
									
										
										
										
											1998-08-24 20:30:07 +00:00
										 |  |  |   object, a string and an arbitrary value.  The string may name an | 
					
						
							|  |  |  |   existing attribute or a new attribute.  The function assigns the | 
					
						
							|  |  |  |   value to the attribute, provided the object allows it.  For example, | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  |   \code{setattr(\var{x}, '\var{foobar}', 123)} is equivalent to | 
					
						
							|  |  |  |   \code{\var{x}.\var{foobar} = 123}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{slice}{\optional{start,} stop\optional{, step}} | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | Return a slice object representing the set of indices specified by | 
					
						
							|  |  |  | \code{range(\var{start}, \var{stop}, \var{step})}.  The \var{start} | 
					
						
							|  |  |  | and \var{step} arguments default to None.  Slice objects have | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | read-only data attributes \member{start}, \member{stop} and \member{step} | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | which merely return the argument values (or their default).  They have | 
					
						
							|  |  |  | no other explicit functionality; however they are used by Numerical | 
					
						
							| 
									
										
										
										
											1998-04-03 07:15:54 +00:00
										 |  |  | Python\index{Numerical Python} and other third party extensions. | 
					
						
							|  |  |  | Slice objects are also generated when extended indexing syntax is | 
					
						
							|  |  |  | used, e.g. for \samp{a[start:stop:step]} or \samp{a[start:stop, i]}. | 
					
						
							| 
									
										
										
										
											1997-10-05 18:53:00 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{str}{object} | 
					
						
							|  |  |  | Return a string containing a nicely printable representation of an | 
					
						
							|  |  |  | object.  For strings, this returns the string itself.  The difference | 
					
						
							| 
									
										
										
										
											1995-03-07 10:14:09 +00:00
										 |  |  | with \code{repr(\var{object})} is that \code{str(\var{object})} does not | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | always attempt to return a string that is acceptable to \function{eval()}; | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | its goal is to return a printable string. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | \begin{funcdesc}{tuple}{sequence} | 
					
						
							| 
									
										
										
										
											1994-08-12 13:13:50 +00:00
										 |  |  | Return a tuple whose items are the same and in the same order as | 
					
						
							| 
									
										
										
										
											1997-06-02 17:21:20 +00:00
										 |  |  | \var{sequence}'s items.  If \var{sequence} is already a tuple, it | 
					
						
							| 
									
										
										
										
											1994-08-12 13:13:50 +00:00
										 |  |  | is returned unchanged.  For instance, \code{tuple('abc')} returns | 
					
						
							|  |  |  | returns \code{('a', 'b', 'c')} and \code{tuple([1, 2, 3])} returns | 
					
						
							|  |  |  | \code{(1, 2, 3)}. | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \begin{funcdesc}{type}{object} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | Return the type of an \var{object}.  The return value is a type | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | object.  The standard module \module{types} defines names for all | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | built-in types. | 
					
						
							| 
									
										
										
										
											1997-12-15 22:28:38 +00:00
										 |  |  | \refstmodindex{types} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | \obindex{type} | 
					
						
							|  |  |  | For instance: | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | >>> import types | 
					
						
							| 
									
										
										
										
											1998-06-22 14:07:36 +00:00
										 |  |  | >>> if type(x) == types.StringType: print "It's a string" | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											1994-01-02 01:22:07 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							| 
									
										
										
										
											1994-02-24 11:28:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-06 14:43:12 +00:00
										 |  |  | \begin{funcdesc}{unichr}{i} | 
					
						
							|  |  |  | Return the Unicode string of one character whose Unicode code is the | 
					
						
							|  |  |  | integer \var{i}, e.g., \code{unichr(97)} returns the string | 
					
						
							|  |  |  | \code{u'a'}.  This is the inverse of \function{ord()} for Unicode | 
					
						
							|  |  |  | strings.  The argument must be in the range [0..65535], inclusive. | 
					
						
							|  |  |  | \exception{ValueError} is raised otherwise. | 
					
						
							| 
									
										
										
										
											2000-06-30 16:06:19 +00:00
										 |  |  | \versionadded{2.0} | 
					
						
							| 
									
										
										
										
											2000-04-06 14:43:12 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{unicode}{string\optional{, encoding='utf-8'\optional{, errors='strict'}}} | 
					
						
							|  |  |  | Decodes \var{string} using the codec for \var{encoding}.  Error | 
					
						
							|  |  |  | handling is done according to \var{errors}.  The default behavior is | 
					
						
							|  |  |  | to decode UTF-8 in strict mode, meaning that encoding errors raise | 
					
						
							|  |  |  | \exception{ValueError}. | 
					
						
							| 
									
										
										
										
											2000-06-30 16:06:19 +00:00
										 |  |  | \versionadded{2.0} | 
					
						
							| 
									
										
										
										
											2000-04-06 14:43:12 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-03-13 10:03:32 +00:00
										 |  |  | \begin{funcdesc}{vars}{\optional{object}} | 
					
						
							| 
									
										
										
										
											1994-04-21 10:32:28 +00:00
										 |  |  | Without arguments, return a dictionary corresponding to the current | 
					
						
							|  |  |  | local symbol table.  With a module, class or class instance object as | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | argument (or anything else that has a \member{__dict__} attribute), | 
					
						
							| 
									
										
										
										
											1994-04-21 10:32:28 +00:00
										 |  |  | returns a dictionary corresponding to the object's symbol table. | 
					
						
							|  |  |  | The returned dictionary should not be modified: the effects on the | 
					
						
							| 
									
										
										
										
											1999-04-05 21:22:41 +00:00
										 |  |  | corresponding symbol table are undefined.\footnote{ | 
					
						
							|  |  |  |   In the current implementation, local variable bindings cannot | 
					
						
							|  |  |  |   normally be affected this way, but variables retrieved from  | 
					
						
							|  |  |  |   other scopes (e.g. modules) can be.  This may change.} | 
					
						
							| 
									
										
										
										
											1994-04-21 10:32:28 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-17 06:33:25 +00:00
										 |  |  | \begin{funcdesc}{xrange}{\optional{start,} stop\optional{, step}} | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | This function is very similar to \function{range()}, but returns an | 
					
						
							| 
									
										
										
										
											1994-02-24 11:28:27 +00:00
										 |  |  | ``xrange object'' instead of a list.  This is an opaque sequence type | 
					
						
							|  |  |  | which yields the same values as the corresponding list, without | 
					
						
							|  |  |  | actually storing them all simultaneously.  The advantage of | 
					
						
							| 
									
										
										
										
											1998-03-03 21:56:15 +00:00
										 |  |  | \function{xrange()} over \function{range()} is minimal (since | 
					
						
							|  |  |  | \function{xrange()} still has to create the values when asked for | 
					
						
							|  |  |  | them) except when a very large range is used on a memory-starved | 
					
						
							|  |  |  | machine (e.g. MS-DOS) or when all of the range's elements are never | 
					
						
							|  |  |  | used (e.g. when the loop is usually terminated with \keyword{break}). | 
					
						
							| 
									
										
										
										
											1994-02-24 11:28:27 +00:00
										 |  |  | \end{funcdesc} |