home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 5
/
DATAFILE_PDCD5.iso
/
utilities
/
p
/
python
/
pyhtmldoc
/
b
/
built-in_f
< prev
next >
Wrap
Text File
|
1996-11-14
|
24KB
|
450 lines
<TITLE>Built-in Functions -- Python library reference</TITLE>
Prev: <A HREF="../e/exceptions" TYPE="Prev">Exceptions</A>
Up: <A HREF="../b/built-in_objects" TYPE="Up">Built-in Objects</A>
Top: <A HREF="../t/top" TYPE="Top">Top</A>
<H1>2.3. Built-in Functions</H1>
The Python interpreter has a number of functions built into it that
are always available. They are listed here in alphabetical order.
<P>
<DL><DT><B>abs</B> (<VAR>x</VAR>) -- built-in function<DD>
Return the absolute value of a number. The argument may be a plain
or long integer or a floating point number.
</DL>
<DL><DT><B>apply</B> (<VAR>function</VAR>, <VAR>args</VAR>[, <VAR>keywords</VAR>]) -- built-in function<DD>
The <VAR>function</VAR> argument must be a callable object (a user-defined or
built-in function or method, or a class object) and the <VAR>args</VAR>
argument must be a tuple. The <VAR>function</VAR> is called with
<VAR>args</VAR> as argument list; the number of arguments is the the length
of the tuple. (This is different from just calling
<CODE><VAR>func</VAR>(<VAR>args</VAR>)</CODE>, since in that case there is always
exactly one argument.)
If the optional <VAR>keywords</VAR> argument is present, it must be a
dictionary whose keys are strings. It specifies keyword arguments to
be added to the end of the the argument list.
</DL>
<DL><DT><B>chr</B> (<VAR>i</VAR>) -- built-in function<DD>
Return a string of one character whose ASCII code is the integer
<VAR>i</VAR>, e.g., <CODE>chr(97)</CODE> returns the string <CODE>'a'</CODE>. This is the
inverse of <CODE>ord()</CODE>. The argument must be in the range [0..255],
inclusive.
</DL>
<DL><DT><B>cmp</B> (<VAR>x</VAR>, <VAR>y</VAR>) -- built-in function<DD>
Compare the two objects <VAR>x</VAR> and <VAR>y</VAR> and return an integer
according to the outcome. The return value is negative if <CODE><VAR>x</VAR>
< <VAR>y</VAR></CODE>, zero if <CODE><VAR>x</VAR> == <VAR>y</VAR></CODE> and strictly positive if
<CODE><VAR>x</VAR> > <VAR>y</VAR></CODE>.
</DL>
<DL><DT><B>coerce</B> (<VAR>x</VAR>, <VAR>y</VAR>) -- built-in function<DD>
Return a tuple consisting of the two numeric arguments converted to
a common type, using the same rules as used by arithmetic
operations.
</DL>
<DL><DT><B>compile</B> (<VAR>string</VAR>, <VAR>filename</VAR>, <VAR>kind</VAR>) -- built-in function<DD>
Compile the <VAR>string</VAR> into a code object. Code objects can be
executed by an <CODE>exec</CODE> statement or evaluated by a call to
<CODE>eval()</CODE>. The <VAR>filename</VAR> argument should
give the file from which the code was read; pass e.g. <CODE>'<string>'</CODE>
if it wasn't read from a file. The <VAR>kind</VAR> argument specifies
what kind of code must be compiled; it can be <CODE>'exec'</CODE> if
<VAR>string</VAR> consists of a sequence of statements, <CODE>'eval'</CODE>
if it consists of a single expression, or <CODE>'single'</CODE> if
it consists of a single interactive statement (in the latter case,
expression statements that evaluate to something else than
<CODE>None</CODE> will printed).
</DL>
<DL><DT><B>delattr</B> (<VAR>object</VAR>, <VAR>name</VAR>) -- built-in function<DD>
This is a relative of <CODE>setattr</CODE>. 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,
<CODE>delattr(<VAR>x</VAR>, '<VAR>foobar</VAR>')</CODE> is equivalent to
<CODE>del <VAR>x</VAR>.<VAR>foobar</VAR></CODE>.
</DL>
<DL><DT><B>dir</B> () -- built-in function<DD>
Without arguments, return the list of names in the current local
symbol table. With a module, class or class instance object as
argument (or anything else that has a <CODE>__dict__</CODE> attribute),
returns the list of names in that object's attribute dictionary.
The resulting list is sorted. For example:
<P>
<UL COMPACT><CODE>>>> import sys<P>
>>> dir()<P>
['sys']<P>
>>> dir(sys)<P>
['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout']<P>
>>> <P>
</CODE></UL>
</DL>
<DL><DT><B>divmod</B> (<VAR>a</VAR>, <VAR>b</VAR>) -- built-in function<DD>
Take two numbers as arguments and return a pair of integers
consisting of their integer quotient and remainder. With mixed
operand types, the rules for binary arithmetic operators apply. For
plain and long integers, the result is the same as
<CODE>(<VAR>a</VAR> / <VAR>b</VAR>, <VAR>a</VAR> % <VAR>b</VAR>)</CODE>.
For floating point numbers the result is the same as
<CODE>(math.floor(<VAR>a</VAR> / <VAR>b</VAR>), <VAR>a</VAR> % <VAR>b</VAR>)</CODE>.
</DL>
<DL><DT><B>eval</B> (<VAR>expression</VAR>[, <VAR>globals</VAR>[, <VAR>locals</VAR>]]) -- built-in function<DD>
The arguments are a string and two optional dictionaries. The
<VAR>expression</VAR> argument is parsed and evaluated as a Python
expression (technically speaking, a condition list) using the
<VAR>globals</VAR> and <VAR>locals</VAR> dictionaries as global and local name
space. If the <VAR>locals</VAR> dictionary is omitted it defaults to
the <VAR>globals</VAR> dictionary. If both dictionaries are omitted, the
expression is executed in the environment where <CODE>eval</CODE> is
called. The return value is the result of the evaluated expression.
Syntax errors are reported as exceptions. Example:
<P>
<UL COMPACT><CODE>>>> x = 1<P>
>>> print eval('x+1')<P>
2<P>
>>> <P>
</CODE></UL>
This function can also be used to execute arbitrary code objects
(e.g. created by <CODE>compile()</CODE>). In this case pass a code
object instead of a string. The code object must have been compiled
passing <CODE>'eval'</CODE> to the <VAR>kind</VAR> argument.
<P>
Hints: dynamic execution of statements is supported by the
<CODE>exec</CODE> statement. Execution of statements from a file is
supported by the <CODE>execfile()</CODE> function. The <CODE>globals()</CODE>
and <CODE>locals()</CODE> functions returns the current global and local
dictionary, respectively, which may be useful
to pass around for use by <CODE>eval()</CODE> or <CODE>execfile()</CODE>.
<P>
</DL>
<DL><DT><B>execfile</B> (<VAR>file</VAR>[, <VAR>globals</VAR>[, <VAR>locals</VAR>]]) -- built-in function<DD>
This function is similar to the
<CODE>exec</CODE> statement, but parses a file instead of a string. It is
different from the <CODE>import</CODE> statement in that it does not use
the module administration --- it reads the file unconditionally and
does not create a new module.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
<P>
The arguments are a file name and two optional dictionaries. The
file is parsed and evaluated as a sequence of Python statements
(similarly to a module) using the <VAR>globals</VAR> and <VAR>locals</VAR>
dictionaries as global and local name space. If the <VAR>locals</VAR>
dictionary is omitted it defaults to the <VAR>globals</VAR> dictionary.
If both dictionaries are omitted, the expression is executed in the
environment where <CODE>execfile()</CODE> is called. The return value is
<CODE>None</CODE>.
</DL>
<DL><DT><B>filter</B> (<VAR>function</VAR>, <VAR>list</VAR>) -- built-in function<DD>
Construct a list from those elements of <VAR>list</VAR> for which
<VAR>function</VAR> returns true. If <VAR>list</VAR> is a string or a tuple,
the result also has that type; otherwise it is always a list. If
<VAR>function</VAR> is <CODE>None</CODE>, the identity function is assumed,
i.e. all elements of <VAR>list</VAR> that are false (zero or empty) are
removed.
</DL>
<DL><DT><B>float</B> (<VAR>x</VAR>) -- built-in function<DD>
Convert a number to floating point. The argument may be a plain or
long integer or a floating point number.
</DL>
<DL><DT><B>getattr</B> (<VAR>object</VAR>, <VAR>name</VAR>) -- built-in function<DD>
The arguments are an object and a string. The string must be the
name
of one of the object's attributes. The result is the value of that
attribute. For example, <CODE>getattr(<VAR>x</VAR>, '<VAR>foobar</VAR>')</CODE> is equivalent to
<CODE><VAR>x</VAR>.<VAR>foobar</VAR></CODE>.
</DL>
<DL><DT><B>globals</B> () -- built-in function<DD>
Return a dictionary represen