The Python Library Reference, Release 2.6
cmp(x, y)
Compare the two objects x and y and return an integer according to the outcome. The return value is negative
if x < y, zero if x == y and strictly positive if x > y.
compile(source, filename, mode, [flags, [dont_inherit]])
Compile the source into a code or AST object. Code objects can be executed by an exec statement or
evaluated by a call to eval(). source can either be a string or an AST object. Refer to the _ast module
documentation for information on how to compile into and from AST objects.
When compiling a string with multi-line statements, two caveats apply: line endings must be represented
by a single newline character (’\n’), and the input must be terminated by at least one newline character. If
line endings are represented by ’\r\n’, use the string replace() method to change them into ’\n’.
The filename argument should give the file from which the code was read; pass some recognizable value if
it wasn’t read from a file (’<string>’ is commonly used).
The mode argument specifies what kind of code must be compiled; it can be ’exec’ if source consists of a
sequence of statements, ’eval’ if it consists of a single expression, or ’single’ if it consists of a single
interactive statement (in the latter case, expression statements that evaluate to something else than None
will be printed).
The optional arguments flags and dont_inherit (which are new in Python 2.2) control which future state-
ments (see PEP 236) affect the compilation of source. If neither is present (or both are zero) the code is
compiled with those future statements that are in effect in the code that is calling compile. If the flags argu-
ment is given and dont_inherit is not (or is zero) then the future statements specified by the flags argument
are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags
argument is it – the future statements in effect around the call to compile are ignored.
Future statements are specified by bits which can be bitwise ORed together to specify multiple statements.
The bitfield required to specify a given feature can be found as the compiler_flag attribute on the
_Feature instance in the __future__ module.
This function raises SyntaxError if the compiled source is invalid, and TypeError if the source con-
tains null bytes. New in version 2.6: Support for compiling AST objects.
complex([real, [imag]])
Create a complex number with the value real + imag*j or convert a string or number to a complex number.
If the first parameter is a string, it will be interpreted as a complex number and the function must be called
without a second parameter. The second parameter can never be a string. Each argument may be any
numeric type (including complex). If imag is omitted, it defaults to zero and the function serves as a
numeric conversion function like int(), long() and float(). If both arguments are omitted, returns
0j.
The complex type is described in Numeric Types — int, float, long, complex.
delattr(object, name)
This is a relative of setattr(). The arguments are an object and a string. The string must be the name
of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For
example, delattr(x, ’foobar’) is equivalent to del x.foobar.
dict([arg])
Create a new data dictionary, optionally with items taken from arg. The dictionary type is described in
Mapping Types — dict.
For other containers see the built in list, set, and tuple classes, and the collections module.
dir([object])
Without arguments, return the list of names in the current local scope. With an argument, attempt to return
a list of valid attributes for that object.
If the object has a method named __dir__(), this method will be called and must return the list of
attributes. This allows objects that implement a custom __getattr__() or __getattribute__()
function to customize the way dir() reports their attributes.
If the object does not provide __dir__(), the function tries its best to gather information from the object’s
__dict__ attribute, if defined, and from its type object. The resulting list is not necessarily complete, and
may be inaccurate when the object has a custom __getattr__().
7