Next / Previous / Contents / TCC Help System / NM Tech homepage

11. Built-in functions for multiple types

Here are some useful functions that operate on objects of many types:

chr(n)

Returns a string containing the character whose ASCII code is the integer n.

cmp(x,y)

Compare two objects. The value returned is zero if x and y are equal; negative if x precedes y; and positive if x follows y.

This may not work for all types, or when x and y have different types. It will certainly work for numbers and strings.

dir(x)

Returns a list of all the names defined in x. If x is a module, the list shows the names of its members and functions. If the argument is omitted, it returns a list of the names defined in the local name space.

filter(f,S)

S must be a sequence, and f a function that takes one argument and returns true or false (that is, a predicate). The result is a new sequence consisting of only those elements of S for which f(S) is true. If f is None, the result is a sequence consisting of the true elements of S; see the definition of true-false values below.

globals()

Returns a dictionary whose keys are the names defined in the current module's global scope, and whose values are the values bound to those names.

locals()

Returns a dictionary whose keys are the names defined in the current local scope, and whose values are the values bound to those names.

map(f,s0[,s1,...])

Applies a function f to each member of a sequence s0 and returns a list of the results [f(s0[0]), f(s0[1]), ...]. If there are additional sequence arguments, the function must take as many arguments as there are sequences, and the [ith] element of the result is equal to f(s0[i],s1[i], ...).

ord(c)

The argument c must be a string containing exactly one character. The return value is the character code of that character as an integer.

reduce(f,s[,i])

Apply the two-argument function f pairwise to the elements of sequence s. Starts by computing t0=f(s[0],s[1]), then t1=f(t0,s[1]), and so forth over the elements of s, then returns the last ti.

The optional third argument i is a starting value. If it is given, the function starts by computing t0=f(i,s[0]), and then continues as before.

repr(x)

Return a string containing a printable representation of x. You can abbreviate this function as `x`.

type(x)

Returns the type of x, or more precisely, the type object for the type of x.

There is one type object for each built-in type; the type objects are named int, long, and so forth, and are summarized in the table of basic types.

You can compare type objects using expressions like “if type(x) is list...”.

unichr(n)

Returns a Unicode string containing the character whose code is the integer n.