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
and
x
are equal; negative if
y precedes
x;
and positive if
y follows
x.y
This may not work for all types, or when
and
x
have different types. It will certainly work for
numbers and strings.y
dir(x)Returns a list of all the names defined in
. 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.x
filter(f,S)
must be a sequence,
and S
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
f for which
S
is true. If f(S) is
fNone, the result is a sequence
consisting of the true elements of
;
see the definition of true-false values below.S
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 to each member of a
sequence f and
returns a list of the results
s0[. If there are additional
sequence arguments, the function must take
as many arguments as there are sequences, and
the f(s0[0]), f(s0[1]), ...][
element of the result is equal to
ith].f(s0[i],s1[i], ...)
ord(c)The argument
must be a string containing exactly one character.
The return value is the character code of that character as an
integer.c
reduce(f,s[,i])Apply the two-argument function
pairwise to the elements of sequence
f.
Starts by computing
s, then
t0=f(s[0],s[1]), and so forth
over the elements of t1=f(t0,s[1])s,
then returns the last
.ti
The optional third argument
is a starting value. If it is given, the function
starts by computing
i, and then
continues as before.t0=f(i,s[0])
repr(x)Return a string containing a printable
representation of
.
You can abbreviate this function as
x`x`.
type(x)Returns the type of
, or
more precisely, the type object
for the type of x.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