Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Functions} |
| 2 | |
| 3 | The Python interpreter has a number of functions built into it that |
| 4 | are 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} |
| 14 | The \var{function} argument must be a callable object (a user-defined or |
| 15 | built-in function or method, or a class object) and the \var{args} |
| 16 | argument must be a tuple. The \var{function} is called with |
| 17 | \var{args} as argument list; the number of arguments is the the length |
| 18 | of the tuple. (This is different from just calling |
| 19 | \code{\var{func}(\var{args})}, since in that case there is always |
| 20 | exactly 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 |
| 45 | executed by a \code{exec()} statement or evaluated by a call to |
| 46 | \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 | |
| 54 | \begin{funcdesc}{dir}{} |
| 55 | Without arguments, return the list of names in the current local |
| 56 | symbol table. With a module, class or class instance object as |
| 57 | argument (or anything else that has a \code{__dict__} attribute), |
| 58 | returns the list of names in that object's attribute dictionary. |
| 59 | The resulting list is sorted. For example: |
| 60 | |
| 61 | \bcode\begin{verbatim} |
| 62 | >>> import sys |
| 63 | >>> dir() |
| 64 | ['sys'] |
| 65 | >>> dir(sys) |
| 66 | ['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout'] |
| 67 | >>> |
| 68 | \end{verbatim}\ecode |
| 69 | \end{funcdesc} |
| 70 | |
| 71 | \begin{funcdesc}{divmod}{a\, b} |
| 72 | Take two numbers as arguments and return a pair of integers |
| 73 | consisting of their integer quotient and remainder. With mixed |
| 74 | operand types, the rules for binary arithmetic operators apply. For |
| 75 | plain and long integers, the result is the same as |
| 76 | \code{(\var{a} / \var{b}, \var{a} \%{} \var{b})}. |
| 77 | For floating point numbers the result is the same as |
| 78 | \code{(math.floor(\var{a} / \var{b}), \var{a} \%{} \var{b})}. |
| 79 | \end{funcdesc} |
| 80 | |
| 81 | \begin{funcdesc}{eval}{s\, globals\, locals} |
| 82 | The arguments are a string and two optional dictionaries. The |
| 83 | string argument is parsed and evaluated as a Python expression |
| 84 | (technically speaking, a condition list) using the dictionaries as |
| 85 | global and local name space. The string must not contain null bytes |
| 86 | or newline characters. The return value is the |
| 87 | result of the expression. If the third argument is omitted it |
| 88 | defaults to the second. If both dictionaries are omitted, the |
| 89 | expression is executed in the environment where \code{eval} is |
| 90 | called. Syntax errors are reported as exceptions. Example: |
| 91 | |
| 92 | \bcode\begin{verbatim} |
| 93 | >>> x = 1 |
| 94 | >>> print eval('x+1') |
| 95 | 2 |
| 96 | >>> |
| 97 | \end{verbatim}\ecode |
| 98 | |
| 99 | This function can also be used to execute arbitrary code objects |
| 100 | (e.g. created by \code{compile()}). In this case pass a code |
| 101 | object instead of a string. The code object must have been compiled |
| 102 | passing \code{'eval'} to the \var{kind} argument. |
| 103 | |
| 104 | Note: dynamic execution of statements is supported by the |
| 105 | \code{exec} statement. |
| 106 | |
| 107 | \end{funcdesc} |
| 108 | |
| 109 | \begin{funcdesc}{filter}{function\, list} |
| 110 | Construct a list from those elements of \var{list} for which |
| 111 | \var{function} returns true. If \var{list} is a string or a tuple, |
| 112 | the result also has that type; otherwise it is always a list. If |
| 113 | \var{function} is \code{None}, the identity function is assumed, |
| 114 | i.e. all elements of \var{list} that are false (zero or empty) are |
| 115 | removed. |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{float}{x} |
| 119 | Convert a number to floating point. The argument may be a plain or |
| 120 | long integer or a floating point number. |
| 121 | \end{funcdesc} |
| 122 | |
| 123 | \begin{funcdesc}{getattr}{object\, name} |
| 124 | The arguments are an object and a string. The string must be the |
| 125 | name |
| 126 | of one of the object's attributes. The result is the value of that |
| 127 | attribute. For example, \code{getattr(\var{x}, '\var{foobar}')} is equivalent to |
| 128 | \code{\var{x}.\var{foobar}}. |
| 129 | \end{funcdesc} |
| 130 | |
| 131 | \begin{funcdesc}{hasattr}{object\, name} |
| 132 | The arguments are an object and a string. The result is 1 if the |
| 133 | string is the name of one of the object's attributes, 0 if not. |
| 134 | (This is implemented by calling \code{getattr(object, name)} and |
| 135 | seeing whether it raises an exception or not.) |
| 136 | \end{funcdesc} |
| 137 | |
| 138 | \begin{funcdesc}{hash}{object} |
| 139 | Return the hash value of the object (if it has one). Hash values |
| 140 | are 32-bit integers. They are used to quickly compare dictionary |
| 141 | keys during a dictionary lookup. Numeric values that compare equal |
| 142 | have the same hash value (even if they are of different types, e.g. |
| 143 | 1 and 1.0). |
| 144 | \end{funcdesc} |
| 145 | |
| 146 | \begin{funcdesc}{hex}{x} |
| 147 | Convert a number to a hexadecimal string. The result is a valid |
| 148 | Python expression. |
| 149 | \end{funcdesc} |
| 150 | |
| 151 | \begin{funcdesc}{id}{object} |
| 152 | Return the `identity' of an object. This is an integer which is |
| 153 | guaranteed to be unique and constant for this object during its |
| 154 | lifetime. (Two objects whose lifetimes are disjunct may have the |
| 155 | same id() value.) (Implementation note: this is the address of the |
| 156 | object.) |
| 157 | \end{funcdesc} |
| 158 | |
| 159 | \begin{funcdesc}{input}{prompt} |
| 160 | Almost equivalent to \code{eval(raw_input(\var{prompt}))}. As for |
| 161 | \code{raw_input()}, the prompt argument is optional. The difference is |
| 162 | that a long input expression may be broken over multiple lines using the |
| 163 | backslash convention. |
| 164 | \end{funcdesc} |
| 165 | |
| 166 | \begin{funcdesc}{int}{x} |
| 167 | Convert a number to a plain integer. The argument may be a plain or |
| 168 | long integer or a floating point number. |
| 169 | \end{funcdesc} |
| 170 | |
| 171 | \begin{funcdesc}{len}{s} |
| 172 | Return the length (the number of items) of an object. The argument |
| 173 | may be a sequence (string, tuple or list) or a mapping (dictionary). |
| 174 | \end{funcdesc} |
| 175 | |
| 176 | \begin{funcdesc}{long}{x} |
| 177 | Convert a number to a long integer. The argument may be a plain or |
| 178 | long integer or a floating point number. |
| 179 | \end{funcdesc} |
| 180 | |
| 181 | \begin{funcdesc}{map}{function\, list\, ...} |
| 182 | Apply \var{function} to every item of \var{list} and return a list |
| 183 | of the results. If additional \var{list} arguments are passed, |
| 184 | \var{function} must take that many arguments and is applied to |
| 185 | the items of all lists in parallel; if a list is shorter than another |
| 186 | it is assumed to be extended with \code{None} items. If |
| 187 | \var{function} is \code{None}, the identity function is assumed; if |
| 188 | there are multiple list arguments, \code{map} returns a list |
| 189 | consisting of tuples containing the corresponding items from all lists |
| 190 | (i.e. a kind of transpose operation). The \var{list} arguments may be |
| 191 | any kind of sequence; the result is always a list. |
| 192 | \end{funcdesc} |
| 193 | |
| 194 | \begin{funcdesc}{max}{s} |
| 195 | Return the largest item of a non-empty sequence (string, tuple or |
| 196 | list). |
| 197 | \end{funcdesc} |
| 198 | |
| 199 | \begin{funcdesc}{min}{s} |
| 200 | Return the smallest item of a non-empty sequence (string, tuple or |
| 201 | list). |
| 202 | \end{funcdesc} |
| 203 | |
| 204 | \begin{funcdesc}{oct}{x} |
| 205 | Convert a number to an octal string. The result is a valid Python |
| 206 | expression. |
| 207 | \end{funcdesc} |
| 208 | |
Guido van Rossum | 041be05 | 1994-05-03 14:46:50 +0000 | [diff] [blame^] | 209 | \begin{funcdesc}{open}{filename\, mode\, bufsize} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 210 | Return a new file object (described earlier under Built-in Types). |
Guido van Rossum | 041be05 | 1994-05-03 14:46:50 +0000 | [diff] [blame^] | 211 | The first two arguments are the same as for \code{stdio}'s |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 212 | \code{fopen()}: \var{filename} is the file name to be opened, |
| 213 | \var{mode} indicates how the file is to be opened: \code{'r'} for |
| 214 | reading, \code{'w'} for writing (truncating an existing file), and |
| 215 | \code{'a'} opens it for appending. Modes \code{'r+'}, \code{'w+'} and |
| 216 | \code{'a+'} open the file for updating, provided the underlying |
| 217 | \code{stdio} library understands this. On systems that differentiate |
| 218 | between binary and text files, \code{'b'} appended to the mode opens |
| 219 | the file in binary mode. If the file cannot be opened, \code{IOError} |
| 220 | is raised. |
Guido van Rossum | 041be05 | 1994-05-03 14:46:50 +0000 | [diff] [blame^] | 221 | If \var{mode} is omitted, it defaults to \code{'r'}. |
| 222 | The optional \var{bufsize} argument specifies the file's desired |
| 223 | buffer size: 0 means unbuffered, 1 means line buffered, any other |
| 224 | positive value means use a buffer of (approximately) that size. A |
| 225 | negative \var{bufsize} means to use the system default, which is |
| 226 | usually line buffered for for tty devices and fully buffered for other |
| 227 | files.% |
| 228 | \footnote{Specifying a buffer size currently has no effect on systems |
| 229 | that don't have \code{setvbuf()}. The interface to specify the buffer |
| 230 | size is not done using a method that calls \code{setvbuf()}, because |
| 231 | that may dump core when called after any I/O has been performed, and |
| 232 | there's no reliable way to determine whether this is the case.} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 233 | \end{funcdesc} |
| 234 | |
| 235 | \begin{funcdesc}{ord}{c} |
| 236 | Return the \ASCII{} value of a string of one character. E.g., |
| 237 | \code{ord('a')} returns the integer \code{97}. This is the inverse of |
| 238 | \code{chr()}. |
| 239 | \end{funcdesc} |
| 240 | |
| 241 | \begin{funcdesc}{pow}{x\, y} |
| 242 | Return \var{x} to the power \var{y}. The arguments must have |
| 243 | numeric types. With mixed operand types, the rules for binary |
| 244 | arithmetic operators apply. The effective operand type is also the |
| 245 | type of the result; if the result is not expressible in this type, the |
| 246 | function raises an exception; e.g., \code{pow(2, -1)} is not allowed. |
| 247 | \end{funcdesc} |
| 248 | |
| 249 | \begin{funcdesc}{range}{start\, end\, step} |
| 250 | This is a versatile function to create lists containing arithmetic |
| 251 | progressions. It is most often used in \code{for} loops. The |
| 252 | arguments must be plain integers. If the \var{step} argument is |
| 253 | omitted, it defaults to \code{1}. If the \var{start} argument is |
| 254 | omitted, it defaults to \code{0}. The full form returns a list of |
| 255 | plain integers \code{[\var{start}, \var{start} + \var{step}, |
| 256 | \var{start} + 2 * \var{step}, \ldots]}. If \var{step} is positive, |
| 257 | the last element is the largest \code{\var{start} + \var{i} * |
| 258 | \var{step}} less than \var{end}; if \var{step} is negative, the last |
| 259 | element is the largest \code{\var{start} + \var{i} * \var{step}} |
| 260 | greater than \var{end}. \var{step} must not be zero. Example: |
| 261 | |
| 262 | \bcode\begin{verbatim} |
| 263 | >>> range(10) |
| 264 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 265 | >>> range(1, 11) |
| 266 | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 267 | >>> range(0, 30, 5) |
| 268 | [0, 5, 10, 15, 20, 25] |
| 269 | >>> range(0, 10, 3) |
| 270 | [0, 3, 6, 9] |
| 271 | >>> range(0, -10, -1) |
| 272 | [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] |
| 273 | >>> range(0) |
| 274 | [] |
| 275 | >>> range(1, 0) |
| 276 | [] |
| 277 | >>> |
| 278 | \end{verbatim}\ecode |
| 279 | \end{funcdesc} |
| 280 | |
| 281 | \begin{funcdesc}{raw_input}{prompt} |
| 282 | The string argument is optional; if present, it is written to |
| 283 | standard |
| 284 | output without a trailing newline. The function then reads a line |
| 285 | from input, converts it to a string (stripping a trailing newline), |
| 286 | and returns that. When \EOF{} is read, \code{EOFError} is raised. |
| 287 | Example: |
| 288 | |
| 289 | \bcode\begin{verbatim} |
| 290 | >>> s = raw_input('--> ') |
| 291 | --> Monty Python's Flying Circus |
| 292 | >>> s |
| 293 | 'Monty Python\'s Flying Circus' |
| 294 | >>> |
| 295 | \end{verbatim}\ecode |
| 296 | \end{funcdesc} |
| 297 | |
| 298 | \begin{funcdesc}{reduce}{function\, list\, initializer} |
| 299 | Apply the binary \var{function} to the items of \var{list} so as to |
| 300 | reduce the list to a single value. E.g., |
| 301 | \code{reduce(lambda x, y: x*y, \var{list}, 1)} returns the product of |
| 302 | the elements of \var{list}. The optional \var{initializer} can be |
| 303 | thought of as being prepended to \var{list} so as to allow reduction |
| 304 | of an empty \var{list}. The \var{list} arguments may be any kind of |
| 305 | sequence. |
| 306 | \end{funcdesc} |
| 307 | |
| 308 | \begin{funcdesc}{reload}{module} |
| 309 | Re-parse and re-initialize an already imported \var{module}. The |
| 310 | argument must be a module object, so it must have been successfully |
| 311 | imported before. This is useful if you have edited the module source |
| 312 | file using an external editor and want to try out the new version |
| 313 | without leaving the Python interpreter. Note that if a module is |
| 314 | syntactically correct but its initialization fails, the first |
| 315 | \code{import} statement for it does not import the name, but does |
| 316 | create a (partially initialized) module object; to reload the module |
| 317 | you must first \code{import} it again (this will just make the |
| 318 | partially initialized module object available) before you can |
| 319 | \code{reload()} it. |
| 320 | \end{funcdesc} |
| 321 | |
| 322 | \begin{funcdesc}{repr}{object} |
| 323 | Return a string containing a printable representation of an object. |
| 324 | This is the same value yielded by conversions (reverse quotes). |
| 325 | It is sometimes useful to be able to access this operation as an |
| 326 | ordinary function. For many types, this function makes an attempt |
| 327 | to return a string that would yield an object with the same value |
| 328 | when passed to \code{eval()}. |
| 329 | \end{funcdesc} |
| 330 | |
| 331 | \begin{funcdesc}{round}{x\, n} |
| 332 | Return the floating point value \var{x} rounded to \var{n} digits |
| 333 | after the decimal point. If \var{n} is omitted, it defaults to zero. |
| 334 | The result is a floating point number. Values are rounded to the |
| 335 | closest multiple of 10 to the power minus \var{n}; if two multiples |
| 336 | are equally close, rounding is done away from 0 (so e.g. |
| 337 | \code{round(0.5)} is \code{1.0} and \code{round(-0.5)} is \code{-1.0}). |
| 338 | \end{funcdesc} |
| 339 | |
| 340 | \begin{funcdesc}{setattr}{object\, name\, value} |
| 341 | This is the counterpart of \code{getattr}. The arguments are an |
| 342 | object, a string and an arbitrary value. The string must be the name |
| 343 | of one of the object's attributes. The function assigns the value to |
| 344 | the attribute, provided the object allows it. For example, |
| 345 | \code{setattr(\var{x}, '\var{foobar}', 123)} is equivalent to |
| 346 | \code{\var{x}.\var{foobar} = 123}. |
| 347 | \end{funcdesc} |
| 348 | |
| 349 | \begin{funcdesc}{str}{object} |
| 350 | Return a string containing a nicely printable representation of an |
| 351 | object. For strings, this returns the string itself. The difference |
| 352 | with \code{repr(\var{object}} is that \code{str(\var{object}} does not |
| 353 | always attempt to return a string that is acceptable to \code{eval()}; |
| 354 | its goal is to return a printable string. |
| 355 | \end{funcdesc} |
| 356 | |
| 357 | \begin{funcdesc}{type}{object} |
| 358 | % XXXJH xref to buil-in objects here? |
| 359 | Return the type of an \var{object}. The return value is a type |
| 360 | object. There is not much you can do with type objects except compare |
| 361 | them to other type objects; e.g., the following checks if a variable |
| 362 | is a string: |
| 363 | |
| 364 | \bcode\begin{verbatim} |
| 365 | >>> if type(x) == type(''): print 'It is a string' |
| 366 | \end{verbatim}\ecode |
| 367 | \end{funcdesc} |
Guido van Rossum | 68cfbe7 | 1994-02-24 11:28:27 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 1738311 | 1994-04-21 10:32:28 +0000 | [diff] [blame] | 369 | \begin{funcdesc}{vars}{} |
| 370 | Without arguments, return a dictionary corresponding to the current |
| 371 | local symbol table. With a module, class or class instance object as |
| 372 | argument (or anything else that has a \code{__dict__} attribute), |
| 373 | returns a dictionary corresponding to the object's symbol table. |
| 374 | The returned dictionary should not be modified: the effects on the |
| 375 | corresponding symbol table are undefined.% |
| 376 | \footnote{In the current implementation, local variable bindings |
| 377 | cannot normally be affected this way, but variables retrieved from |
| 378 | other scopes can be. This may change.} |
| 379 | \end{funcdesc} |
| 380 | |
Guido van Rossum | 68cfbe7 | 1994-02-24 11:28:27 +0000 | [diff] [blame] | 381 | \begin{funcdesc}{xrange}{start\, end\, step} |
| 382 | This function is very similar to \code{range()}, but returns an |
| 383 | ``xrange object'' instead of a list. This is an opaque sequence type |
| 384 | which yields the same values as the corresponding list, without |
| 385 | actually storing them all simultaneously. The advantage of |
| 386 | \code{xrange()} over \code{range()} is minimal (since \code{xrange()} |
| 387 | still has to create the values when asked for them) except when a very |
| 388 | large range is used on a memory-starved machine (e.g. DOS) or when all |
| 389 | of the range's elements are never used (e.g. when the loop is usually |
| 390 | terminated with \code{break}). |
| 391 | \end{funcdesc} |