Built-in Functions

All built-in names (functions and exceptions) exist in the implied outer built-in scope, which corresponds to the __builtin__ module. They are always available in programs without imports (but are not reserved words).

abs(N)

Return the absolute value of a number N.

apply(func, args [, keys])

Call object func (a function, method, or class), passing the positional arguments in tuple args, and keyword arguments in dictionary keys. Returns func result.

callable(object)

Returns 1 if object is callable, else returns 0.

chr(I)

Returns a one-character string whose ASCII code is integer I.

cmp(X, Y)

Returns a negative, zero, or positive integer to designate (X < Y), (X == Y), or (X > Y), respectively.

coerce(X, Y)

Returns a tuple containing the two numeric arguments X and Y converted to a common type.

compile(string, filename, kind)

Compiles string into a code object. string is Python string containing Python program code. filename is a string used in error messages (and is usually the file from which the code was read, or ‘<string>’ if typed interactively). kind can be ‘exec’ if string is statements, ‘eval’ if string is an expression, or ‘single’ which prints the output of expression statements that evaluate to something other than None. The resulting code object can be executed with exec statements or eval calls.

complex(real [, imag])

Builds a complex number object (can also be done using J or j suffix: real+imagJ). imag defaults to 0.

delattr(object, name)

Deletes the attribute named name (a string) from object object. Similar to del obj.name (but name is a string).

dir( [object])

If no args, returns the list of names in the current local scope (namespace). With any object with attributes as an argument, returns the list of attribute names associated with that object. In 1.5, works on modules, classes, and class instances, as well as built-in objects with attributes (lists, dictionaries, etc.).

divmod(X, Y)

Returns a tuple of (X / Y, X % Y)

eval(expr [, globals [, locals]])

Evaluates expr, which is assumed to be a Python string containing a Python expression, or a compiled code object. expr is evaluated in the namespaces of the eval call unless the globals and/or locals namespace dictionary arguments are passed. locals defaults to globals if only globals is passed. Returns expr result. Also see: compile function and exec statement.

execfile(file [, globals [, locals]])

Like eval, but runs all the code in a file whose string name is passed in (instead of an expression). Unlike imports, does not create a new module object for the file. Returns None.

filter(function, sequence)

Constructs a list from those elements of sequence for which function returns true. function takes one parameter. If function is None, returns all true items.

float(X)

Converts a number or a string X to a floating-point number.

getattr(object, name)

Returns the value of attribute name (a string) from object object. Similar to object.name, but name is a string.

globals( )

Returns a dictionary containing the caller’s global variables (e.g., the enclosing module’s names).

hasattr(object, name)

Returns true if object object has an attribute called name (a string).

hash(object)

Returns the hash value of the object (if it has one).

hex(N)

Converts a number N to a hexadecimal string.

id(object)

Returns the unique identity integer of an object (its address in memory).

__import__(name [,globals [,locals]])

Imports and returns a module, given its name as a string. globals and locals have advanced roles (see library reference manual for details).

input( [prompt])

Prints prompt, if given. Then reads an input line from the stdin stream, evaluates it as Python code, and returns the result. Like eval(raw_input(prompt)).

int(X)

Converts a number or a string X to a plain integer.

intern(string)

Enters string in the table of “interned strings” and return the string. Interned strings are ‘immortals.’

isinstance(object, class)

Returns true if object is an instance of class.

issubclass(class1, class2)

Returns true if class1 is derived from class2.

len(object)

Returns the number of items (length) in a collection object. Works on sequences and mappings.

list(sequence)

Converts any sequence object into a list, and returns the result. If sequence is already a list, return a copy of it.

locals( )

Returns a dictionary containing the local variables of the caller (with one key:value entry per local).

long(X)

Converts a number or a string X to a long integer.

map(function, list, . . . )

Applies function to every item of list and returns a list of the collected results. If additional list arguments are passed, function must take that many arguments and it is passed one item from each list on every call.

max(S)

Returns the largest item of a non-empty sequence S.

min(S)

Returns the smallest item of a non-empty sequence S.

oct(N)

Converts a number N to an octal string.

open(filename [, mode, [bufsize]])

Returns a new stdio file object, connected to the external file named filename (a string). The first two arguments are the same as those for C’s stdio “fopen” function.

mode defaults to ‘r’ if omitted, but can be ‘r’ for input, ‘w’ for output, ‘a’ for append, and ‘rb,’ or ‘wb’ for binary files. On most systems, most of these can also have a ‘+’ appended to open in input/output mode.

bufsize defaults to an implementation-dependent value, but can be for unbuffered, 1 for line-buffered, negative for system-default, or a given specific size.

ord(C)

Returns integer ASCII value of a 1-character string C.

pow(X, Y [, Z])

Returns X to power Y [modulo Z ]. Functions like the ** operator.

range( [start,] stop [, step])

Returns a list of successive integers between start and stop. With 1 argument, returns integers from zero through stop-1. With 2 arguments, returns integers from start through stop-1. With 3 arguments, returns integers from start through stop-1, adding step to each predecessor in the result. start, step default to 0, 1.

raw_input( [prompt])

Print prompt if given, then read a string (line) from the stdin input stream (sys.stdin). Strips ‘ ’ at end, and raises EOFError at the end of the stdin stream.

reduce(func, list [, init])

Applies the two-argument function func to successive items from list, so as to reduce the list to a single value. If init is given, it is “prepended” to list.

reload(module)

Reloads, re-parses, and re-executes an already imported module in the module’s current namespace. Replaces prior values of the module’s attributes in-place. module is an existing module object, not a name. Useful in interactive mode if you want to reload a module after fixing it. Returns the module object.

repr(object)

Returns a string containing a printable representation of any object. Equivalent to `object` (backquotes).

round(X [, N])

Returns the floating-point value X rounded to N digits after the decimal point. N defaults to zero.

setattr(object, name, value)

Assigns value to attribute name (a string) in object object. Like object.name = value.

slice([start ,] stop [, step])

Returns a slice object representing a range, with read-only attributes: start, stop, step. Arguments are the same as for range.

str(object)

Returns a string containing a nicely printable representation of an object.

tuple(sequence)

Creates a tuple with the same elements as any sequence. If sequence is already a tuple, it is returned directly (not a copy).

type(object)

Returns a type object representing the type of object object. See module types for objects to compare to.

vars([object])

Without arguments, returns a dictionary containing the current local scope’s names. With a module, class or class instance object as argument, returns a dictionary corresponding to the object’s attribute namespace (its __dict__). Useful for “%” string formatting.

xrange([start,] stop [, step])

Similar to range, but doesn’t actually store entire list all at once. Good to use in “for” loops when there is a big range and little memory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.231.97