blob: 0eb68574fd82e5e3f6a1a1f6c58406a889667991 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Functions}
2
3The Python interpreter has a number of functions built into it that
4are always available. They are listed here in alphabetical order.
5
6
7\renewcommand{\indexsubitem}{(built-in function)}
8\begin{funcdesc}{abs}{x}
9 Return the absolute value of a number. The argument may be a plain
10 or long integer or a floating point number.
11\end{funcdesc}
12
13\begin{funcdesc}{apply}{function\, args}
14The \var{function} argument must be a callable object (a user-defined or
15built-in function or method, or a class object) and the \var{args}
16argument must be a tuple. The \var{function} is called with
17\var{args} as argument list; the number of arguments is the the length
18of the tuple. (This is different from just calling
19\code{\var{func}(\var{args})}, since in that case there is always
20exactly one argument.)
21\end{funcdesc}
22
23\begin{funcdesc}{chr}{i}
24 Return a string of one character whose \ASCII{} code is the integer
25 \var{i}, e.g., \code{chr(97)} returns the string \code{'a'}. This is the
26 inverse of \code{ord()}. The argument must be in the range [0..255],
27 inclusive.
28\end{funcdesc}
29
30\begin{funcdesc}{cmp}{x\, y}
31 Compare the two objects \var{x} and \var{y} and return an integer
32 according to the outcome. The return value is negative if \code{\var{x}
33 < \var{y}}, zero if \code{\var{x} == \var{y}} and strictly positive if
34 \code{\var{x} > \var{y}}.
35\end{funcdesc}
36
37\begin{funcdesc}{coerce}{x\, y}
38 Return a tuple consisting of the two numeric arguments converted to
39 a common type, using the same rules as used by arithmetic
40 operations.
41\end{funcdesc}
42
43\begin{funcdesc}{compile}{string\, filename\, kind}
44 Compile the \var{string} into a code object. Code objects can be
Guido van Rossum6c4f0031995-03-07 10:14:09 +000045 executed by an \code{exec} statement or evaluated by a call to
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046 \code{eval()}. The \var{filename} argument should
47 give the file from which the code was read; pass e.g. \code{'<string>'}
48 if it wasn't read from a file. The \var{kind} argument specifies
49 what kind of code must be compiled; it can be \code{'exec'} if
50 \var{string} consists of a sequence of statements, or \code{'eval'}
51 if it consists of a single expression.
52\end{funcdesc}
53
Guido van Rossum1efbb0f1994-08-16 22:15:11 +000054\begin{funcdesc}{delattr}{object\, name}
55 This is a relative of \code{setattr}. The arguments are an
56 object and a string. The string must be the name
57 of one of the object's attributes. The function deletes
58 the named attribute, provided the object allows it. For example,
Guido van Rossum6c4f0031995-03-07 10:14:09 +000059 \code{delattr(\var{x}, '\var{foobar}')} is equivalent to
Guido van Rossum1efbb0f1994-08-16 22:15:11 +000060 \code{del \var{x}.\var{foobar}}.
61\end{funcdesc}
62
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063\begin{funcdesc}{dir}{}
64 Without arguments, return the list of names in the current local
65 symbol table. With a module, class or class instance object as
66 argument (or anything else that has a \code{__dict__} attribute),
67 returns the list of names in that object's attribute dictionary.
68 The resulting list is sorted. For example:
69
70\bcode\begin{verbatim}
71>>> import sys
72>>> dir()
73['sys']
74>>> dir(sys)
75['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout']
76>>>
77\end{verbatim}\ecode
78\end{funcdesc}
79
80\begin{funcdesc}{divmod}{a\, b}
81 Take two numbers as arguments and return a pair of integers
82 consisting of their integer quotient and remainder. With mixed
83 operand types, the rules for binary arithmetic operators apply. For
84 plain and long integers, the result is the same as
85 \code{(\var{a} / \var{b}, \var{a} \%{} \var{b})}.
86 For floating point numbers the result is the same as
87 \code{(math.floor(\var{a} / \var{b}), \var{a} \%{} \var{b})}.
88\end{funcdesc}
89
Guido van Rossumf8601621995-01-10 10:50:24 +000090\begin{funcdesc}{eval}{expression\optional{\, globals\optional{\, locals}}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091 The arguments are a string and two optional dictionaries. The
Guido van Rossumf8601621995-01-10 10:50:24 +000092 \var{expression} argument is parsed and evaluated as a Python
93 expression (technically speaking, a condition list) using the
94 \var{globals} and \var{locals} dictionaries as global and local name
95 space. If the \var{globals} dictionary is omitted it defaults to
96 the \var{locals} dictionary. If both dictionaries are omitted, the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000097 expression is executed in the environment where \code{eval} is
Guido van Rossumf8601621995-01-10 10:50:24 +000098 called. The return value is the result of the evaluated expression.
99 Syntax errors are reported as exceptions. Example:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000100
101\bcode\begin{verbatim}
102>>> x = 1
103>>> print eval('x+1')
1042
105>>>
106\end{verbatim}\ecode
107
108 This function can also be used to execute arbitrary code objects
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000109 (e.g.\ created by \code{compile()}). In this case pass a code
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000110 object instead of a string. The code object must have been compiled
111 passing \code{'eval'} to the \var{kind} argument.
112
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000113 Hints: dynamic execution of statements is supported by the
Guido van Rossumf8601621995-01-10 10:50:24 +0000114 \code{exec} statement. Execution of statements from a file is
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000115 supported by the \code{execfile()} function. The \code{vars()}
116 function returns the current local dictionary, which may be useful
117 to pass around for use by \code{eval()} or \code{execfile()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000118
119\end{funcdesc}
120
Guido van Rossumf8601621995-01-10 10:50:24 +0000121\begin{funcdesc}{execfile}{file\optional{\, globals\optional{\, locals}}}
122 This function is similar to the \code{eval()} function or the
123 \code{exec} statement, but parses a file instead of a string. It is
124 different from the \code{import} statement in that it does not use
Guido van Rossum86751151995-02-28 17:14:32 +0000125 the module administration --- it reads the file unconditionally and
Guido van Rossumf8601621995-01-10 10:50:24 +0000126 does not create a new module.
127
128 The arguments are a file name and two optional dictionaries. The
129 file is parsed and evaluated as a sequence of Python statements
130 (similarly to a module) using the \var{globals} and \var{locals}
131 dictionaries as global and local name space. If the \var{globals}
132 dictionary is omitted it defaults to the \var{locals} dictionary.
133 If both dictionaries are omitted, the expression is executed in the
134 environment where \code{execfile} is called. The return value is
135 None.
136\end{funcdesc}
137
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000138\begin{funcdesc}{filter}{function\, list}
139Construct a list from those elements of \var{list} for which
140\var{function} returns true. If \var{list} is a string or a tuple,
141the result also has that type; otherwise it is always a list. If
142\var{function} is \code{None}, the identity function is assumed,
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000143i.e.\ all elements of \var{list} that are false (zero or empty) are
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144removed.
145\end{funcdesc}
146
147\begin{funcdesc}{float}{x}
148 Convert a number to floating point. The argument may be a plain or
149 long integer or a floating point number.
150\end{funcdesc}
151
152\begin{funcdesc}{getattr}{object\, name}
153 The arguments are an object and a string. The string must be the
154 name
155 of one of the object's attributes. The result is the value of that
156 attribute. For example, \code{getattr(\var{x}, '\var{foobar}')} is equivalent to
157 \code{\var{x}.\var{foobar}}.
158\end{funcdesc}
159
160\begin{funcdesc}{hasattr}{object\, name}
161 The arguments are an object and a string. The result is 1 if the
162 string is the name of one of the object's attributes, 0 if not.
163 (This is implemented by calling \code{getattr(object, name)} and
164 seeing whether it raises an exception or not.)
165\end{funcdesc}
166
167\begin{funcdesc}{hash}{object}
168 Return the hash value of the object (if it has one). Hash values
169 are 32-bit integers. They are used to quickly compare dictionary
170 keys during a dictionary lookup. Numeric values that compare equal
171 have the same hash value (even if they are of different types, e.g.
172 1 and 1.0).
173\end{funcdesc}
174
175\begin{funcdesc}{hex}{x}
176 Convert a number to a hexadecimal string. The result is a valid
177 Python expression.
178\end{funcdesc}
179
180\begin{funcdesc}{id}{object}
181 Return the `identity' of an object. This is an integer which is
182 guaranteed to be unique and constant for this object during its
183 lifetime. (Two objects whose lifetimes are disjunct may have the
184 same id() value.) (Implementation note: this is the address of the
185 object.)
186\end{funcdesc}
187
Guido van Rossum16d6e711994-08-08 12:30:22 +0000188\begin{funcdesc}{input}{\optional{prompt}}
189 Almost equivalent to \code{eval(raw_input(\var{prompt}))}. Like
190 \code{raw_input()}, the \var{prompt} argument is optional. The difference
191 is that a long input expression may be broken over multiple lines using
192 the backslash convention.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000193\end{funcdesc}
194
195\begin{funcdesc}{int}{x}
196 Convert a number to a plain integer. The argument may be a plain or
197 long integer or a floating point number.
198\end{funcdesc}
199
200\begin{funcdesc}{len}{s}
201 Return the length (the number of items) of an object. The argument
202 may be a sequence (string, tuple or list) or a mapping (dictionary).
203\end{funcdesc}
204
205\begin{funcdesc}{long}{x}
206 Convert a number to a long integer. The argument may be a plain or
207 long integer or a floating point number.
208\end{funcdesc}
209
210\begin{funcdesc}{map}{function\, list\, ...}
211Apply \var{function} to every item of \var{list} and return a list
212of the results. If additional \var{list} arguments are passed,
213\var{function} must take that many arguments and is applied to
214the items of all lists in parallel; if a list is shorter than another
215it is assumed to be extended with \code{None} items. If
216\var{function} is \code{None}, the identity function is assumed; if
217there are multiple list arguments, \code{map} returns a list
218consisting of tuples containing the corresponding items from all lists
219(i.e. a kind of transpose operation). The \var{list} arguments may be
220any kind of sequence; the result is always a list.
221\end{funcdesc}
222
223\begin{funcdesc}{max}{s}
224 Return the largest item of a non-empty sequence (string, tuple or
225 list).
226\end{funcdesc}
227
228\begin{funcdesc}{min}{s}
229 Return the smallest item of a non-empty sequence (string, tuple or
230 list).
231\end{funcdesc}
232
233\begin{funcdesc}{oct}{x}
234 Convert a number to an octal string. The result is a valid Python
235 expression.
236\end{funcdesc}
237
Guido van Rossum7f49b7a1995-01-12 12:38:46 +0000238\begin{funcdesc}{open}{filename\optional{\, mode\optional{\, bufsize}}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000239 Return a new file object (described earlier under Built-in Types).
Guido van Rossum041be051994-05-03 14:46:50 +0000240 The first two arguments are the same as for \code{stdio}'s
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000241 \code{fopen()}: \var{filename} is the file name to be opened,
242 \var{mode} indicates how the file is to be opened: \code{'r'} for
243 reading, \code{'w'} for writing (truncating an existing file), and
244 \code{'a'} opens it for appending. Modes \code{'r+'}, \code{'w+'} and
245 \code{'a+'} open the file for updating, provided the underlying
246 \code{stdio} library understands this. On systems that differentiate
247 between binary and text files, \code{'b'} appended to the mode opens
248 the file in binary mode. If the file cannot be opened, \code{IOError}
249 is raised.
Guido van Rossum041be051994-05-03 14:46:50 +0000250If \var{mode} is omitted, it defaults to \code{'r'}.
251The optional \var{bufsize} argument specifies the file's desired
252buffer size: 0 means unbuffered, 1 means line buffered, any other
253positive value means use a buffer of (approximately) that size. A
254negative \var{bufsize} means to use the system default, which is
255usually line buffered for for tty devices and fully buffered for other
256files.%
257\footnote{Specifying a buffer size currently has no effect on systems
258that don't have \code{setvbuf()}. The interface to specify the buffer
259size is not done using a method that calls \code{setvbuf()}, because
260that may dump core when called after any I/O has been performed, and
261there's no reliable way to determine whether this is the case.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000262\end{funcdesc}
263
264\begin{funcdesc}{ord}{c}
265 Return the \ASCII{} value of a string of one character. E.g.,
266 \code{ord('a')} returns the integer \code{97}. This is the inverse of
267 \code{chr()}.
268\end{funcdesc}
269
Guido van Rossum16d6e711994-08-08 12:30:22 +0000270\begin{funcdesc}{pow}{x\, y\optional{\, z}}
Guido van Rossumb8b264b1994-08-12 13:13:50 +0000271 Return \var{x} to the power \var{y}; if \var{z} is present, return
272 \var{x} to the power \var{y}, modulo \var{z} (computed more
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000273 efficiently than \code{pow(\var{x}, \var{y}) \% \var{z}}).
Guido van Rossumb8b264b1994-08-12 13:13:50 +0000274 The arguments must have
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000275 numeric types. With mixed operand types, the rules for binary
276 arithmetic operators apply. The effective operand type is also the
277 type of the result; if the result is not expressible in this type, the
Guido van Rossum16d6e711994-08-08 12:30:22 +0000278 function raises an exception; e.g., \code{pow(2, -1)} or \code{pow(2,
279 35000)} is not allowed.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000280\end{funcdesc}
281
Guido van Rossum16d6e711994-08-08 12:30:22 +0000282\begin{funcdesc}{range}{\optional{start\,} end\optional{\, step}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000283 This is a versatile function to create lists containing arithmetic
284 progressions. It is most often used in \code{for} loops. The
285 arguments must be plain integers. If the \var{step} argument is
286 omitted, it defaults to \code{1}. If the \var{start} argument is
287 omitted, it defaults to \code{0}. The full form returns a list of
288 plain integers \code{[\var{start}, \var{start} + \var{step},
289 \var{start} + 2 * \var{step}, \ldots]}. If \var{step} is positive,
290 the last element is the largest \code{\var{start} + \var{i} *
291 \var{step}} less than \var{end}; if \var{step} is negative, the last
292 element is the largest \code{\var{start} + \var{i} * \var{step}}
293 greater than \var{end}. \var{step} must not be zero. Example:
294
295\bcode\begin{verbatim}
296>>> range(10)
297[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
298>>> range(1, 11)
299[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
300>>> range(0, 30, 5)
301[0, 5, 10, 15, 20, 25]
302>>> range(0, 10, 3)
303[0, 3, 6, 9]
304>>> range(0, -10, -1)
305[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
306>>> range(0)
307[]
308>>> range(1, 0)
309[]
310>>>
311\end{verbatim}\ecode
312\end{funcdesc}
313
Guido van Rossum16d6e711994-08-08 12:30:22 +0000314\begin{funcdesc}{raw_input}{\optional{prompt}}
315 If the \var{prompt} argument is present, it is written to standard output
316 without a trailing newline. The function then reads a line from input,
317 converts it to a string (stripping a trailing newline), and returns that.
318 When \EOF{} is read, \code{EOFError} is raised. Example:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000319
320\bcode\begin{verbatim}
321>>> s = raw_input('--> ')
322--> Monty Python's Flying Circus
323>>> s
324'Monty Python\'s Flying Circus'
325>>>
326\end{verbatim}\ecode
327\end{funcdesc}
328
Guido van Rossum16d6e711994-08-08 12:30:22 +0000329\begin{funcdesc}{reduce}{function\, list\optional{\, initializer}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000330Apply the binary \var{function} to the items of \var{list} so as to
331reduce the list to a single value. E.g.,
332\code{reduce(lambda x, y: x*y, \var{list}, 1)} returns the product of
333the elements of \var{list}. The optional \var{initializer} can be
334thought of as being prepended to \var{list} so as to allow reduction
335of an empty \var{list}. The \var{list} arguments may be any kind of
336sequence.
337\end{funcdesc}
338
339\begin{funcdesc}{reload}{module}
340 Re-parse and re-initialize an already imported \var{module}. The
341 argument must be a module object, so it must have been successfully
342 imported before. This is useful if you have edited the module source
343 file using an external editor and want to try out the new version
344 without leaving the Python interpreter. Note that if a module is
345 syntactically correct but its initialization fails, the first
346 \code{import} statement for it does not import the name, but does
347 create a (partially initialized) module object; to reload the module
348 you must first \code{import} it again (this will just make the
349 partially initialized module object available) before you can
350 \code{reload()} it.
351\end{funcdesc}
352
353\begin{funcdesc}{repr}{object}
354Return a string containing a printable representation of an object.
355This is the same value yielded by conversions (reverse quotes).
356It is sometimes useful to be able to access this operation as an
357ordinary function. For many types, this function makes an attempt
358to return a string that would yield an object with the same value
359when passed to \code{eval()}.
360\end{funcdesc}
361
362\begin{funcdesc}{round}{x\, n}
363 Return the floating point value \var{x} rounded to \var{n} digits
364 after the decimal point. If \var{n} is omitted, it defaults to zero.
365 The result is a floating point number. Values are rounded to the
366 closest multiple of 10 to the power minus \var{n}; if two multiples
367 are equally close, rounding is done away from 0 (so e.g.
368 \code{round(0.5)} is \code{1.0} and \code{round(-0.5)} is \code{-1.0}).
369\end{funcdesc}
370
371\begin{funcdesc}{setattr}{object\, name\, value}
372 This is the counterpart of \code{getattr}. The arguments are an
373 object, a string and an arbitrary value. The string must be the name
374 of one of the object's attributes. The function assigns the value to
375 the attribute, provided the object allows it. For example,
376 \code{setattr(\var{x}, '\var{foobar}', 123)} is equivalent to
377 \code{\var{x}.\var{foobar} = 123}.
378\end{funcdesc}
379
380\begin{funcdesc}{str}{object}
381Return a string containing a nicely printable representation of an
382object. For strings, this returns the string itself. The difference
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000383with \code{repr(\var{object})} is that \code{str(\var{object})} does not
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000384always attempt to return a string that is acceptable to \code{eval()};
385its goal is to return a printable string.
386\end{funcdesc}
387
Guido van Rossumb8b264b1994-08-12 13:13:50 +0000388\begin{funcdesc}{tuple}{object}
389Return a tuple whose items are the same and in the same order as
390\var{object}'s items. If \var{object} is alread a tuple, it
391is returned unchanged. For instance, \code{tuple('abc')} returns
392returns \code{('a', 'b', 'c')} and \code{tuple([1, 2, 3])} returns
393\code{(1, 2, 3)}.
394\end{funcdesc}
395
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000396\begin{funcdesc}{type}{object}
397% XXXJH xref to buil-in objects here?
398 Return the type of an \var{object}. The return value is a type
399 object. There is not much you can do with type objects except compare
400 them to other type objects; e.g., the following checks if a variable
401 is a string:
402
403\bcode\begin{verbatim}
404>>> if type(x) == type(''): print 'It is a string'
405\end{verbatim}\ecode
406\end{funcdesc}
Guido van Rossum68cfbe71994-02-24 11:28:27 +0000407
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000408\begin{funcdesc}{vars}{\optional{object}}
Guido van Rossum17383111994-04-21 10:32:28 +0000409Without arguments, return a dictionary corresponding to the current
410local symbol table. With a module, class or class instance object as
411argument (or anything else that has a \code{__dict__} attribute),
412returns a dictionary corresponding to the object's symbol table.
413The returned dictionary should not be modified: the effects on the
414corresponding symbol table are undefined.%
415\footnote{In the current implementation, local variable bindings
416cannot normally be affected this way, but variables retrieved from
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000417other scopes (e.g. modules) can be. This may change.}
Guido van Rossum17383111994-04-21 10:32:28 +0000418\end{funcdesc}
419
Guido van Rossum16d6e711994-08-08 12:30:22 +0000420\begin{funcdesc}{xrange}{\optional{start\,} end\optional{\, step}}
Guido van Rossum68cfbe71994-02-24 11:28:27 +0000421This function is very similar to \code{range()}, but returns an
422``xrange object'' instead of a list. This is an opaque sequence type
423which yields the same values as the corresponding list, without
424actually storing them all simultaneously. The advantage of
425\code{xrange()} over \code{range()} is minimal (since \code{xrange()}
426still has to create the values when asked for them) except when a very
427large range is used on a memory-starved machine (e.g. DOS) or when all
428of the range's elements are never used (e.g. when the loop is usually
429terminated with \code{break}).
430\end{funcdesc}