home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / python / emacs-info / python-lib.info-1 < prev    next >
Encoding:
GNU Info File  |  1994-04-01  |  50.2 KB  |  1,396 lines  |  [TEXT/R*ch]

  1. This is Info file python-lib.info, produced by Makeinfo-1.55 from the
  2. input file lib.texi.
  3.  
  4. This file describes the built-in types, exceptions and functions and the
  5. standard modules that come with the Python system.  It assumes basic
  6. knowledge about the Python language.  For an informal introduction to
  7. the language, see the Python Tutorial.  The Python Reference Manual
  8. gives a more formal definition of the language.  (These manuals are not
  9. yet available in INFO or Texinfo format.)
  10.  
  11. Copyright (C) 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  12. Amsterdam, The Netherlands.
  13.  
  14. All Rights Reserved
  15.  
  16. Permission to use, copy, modify, and distribute this software and its
  17. documentation for any purpose and without fee is hereby granted,
  18. provided that the above copyright notice appear in all copies and that
  19. both that copyright notice and this permission notice appear in
  20. supporting documentation, and that the names of Stichting Mathematisch
  21. Centrum or CWI not be used in advertising or publicity pertaining to
  22. distribution of the software without specific, written prior permission.
  23.  
  24. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  25. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE FOR
  27. ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  28. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  29. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  30. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  31.  
  32. 
  33. File: python-lib.info,  Node: Top,  Next: Introduction,  Prev: (dir),  Up: (dir)
  34.  
  35. The Python library
  36. ******************
  37.  
  38. This file describes the built-in types, exceptions and functions and the
  39. standard modules that come with the Python system.  It assumes basic
  40. knowledge about the Python language.  For an informal introduction to
  41. the language, see the `Python Tutorial'.  The `Python Reference Manual'
  42. gives a more formal definition of the language.  (These manuals are not
  43. yet available in INFO or Texinfo format.)
  44.  
  45. This version corresponds to Python version 1.0.0.
  46.  
  47. * Menu:
  48.  
  49. * Introduction::
  50. * Built-in Objects::
  51. * Built-in Modules::
  52. * Standard Modules::
  53. * UNIX ONLY::
  54. * MULTIMEDIA EXTENSIONS::
  55. * CRYPTOGRAPHIC EXTENSIONS::
  56. * STDWIN ONLY::
  57. * SGI IRIX ONLY::
  58. * SUNOS ONLY::
  59. * Function Index::
  60. * Variable Index::
  61. * Module Index::
  62. * Concept Index::
  63.  
  64. 
  65. File: python-lib.info,  Node: Introduction,  Next: Built-in Objects,  Prev: Top,  Up: Top
  66.  
  67. Introduction
  68. ************
  69.  
  70. The Python library consists of three parts, with different levels of
  71. integration with the interpreter.  Closest to the interpreter are
  72. built-in types, exceptions and functions.  Next are built-in modules,
  73. which are written in C and linked statically with the interpreter.
  74. Finally there are standard modules that are implemented entirely in
  75. Python, but are always available.  For efficiency, some standard
  76. modules may become built-in modules in future versions of the
  77. interpreter.
  78.  
  79. 
  80. File: python-lib.info,  Node: Built-in Objects,  Next: Built-in Modules,  Prev: Introduction,  Up: Top
  81.  
  82. Built-in Types, Exceptions and Functions
  83. ****************************************
  84.  
  85. Names for built-in exceptions and functions are found in a separate
  86. symbol table.  This table is searched last, so local and global
  87. user-defined names can override built-in names.  Built-in types have no
  88. names but are created easily by constructing an object of the desired
  89. type (e.g., using a literal) and applying the built-in function
  90. `type()' to it.  They are described together here for easy reference.(1)
  91.  
  92. * Menu:
  93.  
  94. * Types::
  95. * Exceptions::
  96. * Built-in Functions::
  97.  
  98. ---------- Footnotes ----------
  99.  
  100. (1)  Some descriptions sorely lack explanations of the exceptions that
  101. may be raised -- this will be fixed in a future version of this
  102. document.
  103.  
  104. 
  105. File: python-lib.info,  Node: Types,  Next: Exceptions,  Prev: Built-in Objects,  Up: Built-in Objects
  106.  
  107. Built-in Types
  108. ==============
  109.  
  110. The following sections describe the standard types that are built into
  111. the interpreter.  These are the numeric types, sequence types, and
  112. several others, including types themselves.  There is no explicit
  113. Boolean type; use integers instead.
  114.  
  115. Some operations are supported by several object types; in particular,
  116. all objects can be compared, tested for truth value, and converted to a
  117. string (with the ``...`' notation).  The latter conversion is
  118. implicitly used when an object is written by the `print' statement.
  119.  
  120. * Menu:
  121.  
  122. * Truth Value Testing::
  123. * Boolean Operations::
  124. * Comparisons::
  125. * Numeric Types::
  126. * Sequence Types::
  127. * Mapping Types::
  128. * Other Built-in Types::
  129. * Special Attributes::
  130.  
  131. 
  132. File: python-lib.info,  Node: Truth Value Testing,  Next: Boolean Operations,  Prev: Types,  Up: Types
  133.  
  134. Truth Value Testing
  135. -------------------
  136.  
  137. Any object can be tested for truth value, for use in an `if' or `while'
  138. condition or as operand of the Boolean operations below.  The following
  139. values are false:
  140.  
  141.    * `None'
  142.  
  143.    * zero of any numeric type, e.g., `0', `0L', `0.0'.
  144.  
  145.    * any empty sequence, e.g., `''', `()', `[]'.
  146.  
  147.    * any empty mapping, e.g., `{}'.
  148.  
  149. *All* other values are true -- so objects of many types are always true.
  150.  
  151. 
  152. File: python-lib.info,  Node: Boolean Operations,  Next: Comparisons,  Prev: Truth Value Testing,  Up: Types
  153.  
  154. Boolean Operations
  155. ------------------
  156.  
  157. These are the Boolean operations:
  158.  
  159. *Operation*
  160.      *Result*  --  *Notes*
  161.  
  162. `X or Y'
  163.      if X is false, then Y, else X  --  (1)
  164.  
  165. `X and Y'
  166.      if X is false, then X, else Y  --  (1)
  167.  
  168. `not X'
  169.      if X is false, then `1', else `0'
  170.  
  171. Notes:
  172.  
  173. (1)
  174.      These only evaluate their second argument if needed for their
  175.      outcome.
  176.  
  177. 
  178. File: python-lib.info,  Node: Comparisons,  Next: Numeric Types,  Prev: Boolean Operations,  Up: Types
  179.  
  180. Comparisons
  181. -----------
  182.  
  183. Comparison operations are supported by all objects:
  184.  
  185. *Operation*
  186.      *Meaning*  --  *Notes*
  187.  
  188. `<'
  189.      strictly less than
  190.  
  191. `<='
  192.      less than or equal
  193.  
  194. `>'
  195.      strictly greater than
  196.  
  197. `>='
  198.      greater than or equal
  199.  
  200. `=='
  201.      equal
  202.  
  203. `<>'
  204.      not equal  --  (1)
  205.  
  206. `!='
  207.      not equal  --  (1)
  208.  
  209. `is'
  210.      object identity
  211.  
  212. `is not'
  213.      negated object identity
  214.  
  215. Notes:
  216.  
  217. (1)
  218.      `<>' and `!=' are alternate spellings for the same operator.  (I
  219.      couldn't choose between ABC and C! :-)
  220.  
  221. Objects of different types, except different numeric types, never
  222. compare equal; such objects are ordered consistently but arbitrarily
  223. (so that sorting a heterogeneous array yields a consistent result).
  224. Furthermore, some types (e.g., windows) support only a degenerate
  225. notion of comparison where any two objects of that type are unequal.
  226. Again, such objects are ordered arbitrarily but consistently.
  227.  
  228. (Implementation note: objects of different types except numbers are
  229. ordered by their type names; objects of the same types that don't
  230. support proper comparison are ordered by their address.)
  231.  
  232. Two more operations with the same syntactic priority, `in' and `not
  233. in', are supported only by sequence types (below).
  234.  
  235. 
  236. File: python-lib.info,  Node: Numeric Types,  Next: Sequence Types,  Prev: Comparisons,  Up: Types
  237.  
  238. Numeric Types
  239. -------------
  240.  
  241. There are three numeric types: "plain integers", "long integers", and
  242. "floating point numbers".  Plain integers (also just called "integers")
  243. are implemented using `long' in C, which gives them at least 32 bits of
  244. precision.  Long integers have unlimited precision.  Floating point
  245. numbers are implemented using `double' in C.  All bets on their
  246. precision are off unless you happen to know the machine you are working
  247. with.
  248.  
  249. Numbers are created by numeric literals or as the result of built-in
  250. functions and operators.  Unadorned integer literals (including hex and
  251. octal numbers) yield plain integers.  Integer literals with an `L' or
  252. `l' suffix yield long integers (`L' is preferred because `1l' looks too
  253. much like eleven!).  Numeric literals containing a decimal point or an
  254. exponent sign yield floating point numbers.
  255.  
  256. Python fully supports mixed arithmetic: when a binary arithmetic
  257. operator has operands of different numeric types, the operand with the
  258. "smaller" type is converted to that of the other, where plain integer
  259. is smaller than long integer is smaller than floating point.
  260. Comparisons between numbers of mixed type use the same rule.(1) The
  261. functions `int()', `long()' and `float()' can be used to coerce numbers
  262. to a specific type.
  263.  
  264. All numeric types support the following operations:
  265.  
  266. *Operation*
  267.      *Result*  --  *Notes*
  268.  
  269. `abs(X)'
  270.      absolute value of X
  271.  
  272. `int(X)'
  273.      X converted to integer  --  (1)
  274.  
  275. `long(X)'
  276.      X converted to long integer  --  (1)
  277.  
  278. `float(X)'
  279.      X converted to floating point
  280.  
  281. `-X'
  282.      X negated
  283.  
  284. `+X'
  285.      X unchanged
  286.  
  287. `X + Y'
  288.      sum of X and Y
  289.  
  290. `X - Y'
  291.      difference of X and Y
  292.  
  293. `X * Y'
  294.      product of X and Y
  295.  
  296. `X / Y'
  297.      quotient of X and Y  --  (2)
  298.  
  299. `X % Y'
  300.      remainder of `X / Y'
  301.  
  302. `divmod(X, Y)'
  303.      the pair `(X / Y, X % Y)'  --  (3)
  304.  
  305. `pow(X, Y)'
  306.      X to the power Y
  307.  
  308. Notes:
  309. (1)
  310.      Conversion from floating point to (long or plain) integer may
  311.      round or truncate as in C; see functions `floor' and `ceil' in
  312.      module `math' for well-defined conversions.
  313.  
  314. (2)
  315.      For (plain or long) integer division, the result is an integer; it
  316.      always truncates towards zero.
  317.  
  318. (3)
  319.      See the section on built-in functions for an exact definition.
  320.  
  321. * Menu:
  322.  
  323. * Bit-string Operations on Integer Types::
  324.  
  325. ---------- Footnotes ----------
  326.  
  327. (1)  As a consequence, the list `[1, 2]' is considered equal to `[1.0,
  328. 2.0]', and similar for tuples.
  329.  
  330. 
  331. File: python-lib.info,  Node: Bit-string Operations on Integer Types,  Prev: Numeric Types,  Up: Numeric Types
  332.  
  333. Bit-string Operations on Integer Types.
  334. .......................................
  335.  
  336. Plain and long integer types support additional operations that make
  337. sense only for bit-strings.  Negative numbers are treated as their 2's
  338. complement value:
  339.  
  340. *Operation*
  341.      *Result*  --  *Notes*
  342.  
  343. `~X'
  344.      the bits of X inverted
  345.  
  346. `X ^ Y'
  347.      bitwise "exclusive or" of X and Y
  348.  
  349. `X & Y'
  350.      bitwise "and" of X and Y
  351.  
  352. `X | Y'
  353.      bitwise "or" of X and Y
  354.  
  355. `X << N'
  356.      X shifted left by N bits
  357.  
  358. `X >> N'
  359.      X shifted right by N bits
  360.  
  361. 
  362. File: python-lib.info,  Node: Sequence Types,  Next: Mapping Types,  Prev: Numeric Types,  Up: Types
  363.  
  364. Sequence Types
  365. --------------
  366.  
  367. There are three sequence types: strings, lists and tuples.  Strings
  368. literals are written in single quotes: `'xyzzy''.  Lists are
  369. constructed with square brackets, separating items with commas: `[a, b,
  370. c]'.  Tuples are constructed by the comma operator (not within square
  371. brackets), with or without enclosing parentheses, but an empty tuple
  372. must have the enclosing parentheses, e.g., `a, b, c' or `()'.  A single
  373. item tuple must have a trailing comma, e.g., `(d,)'.
  374.  
  375. Sequence types support the following operations (S and T are sequences
  376. of the same type; N, I and J are integers):
  377.  
  378. *Operation*
  379.      *Result*  --  *Notes*
  380.  
  381. `len(S)'
  382.      length of S
  383.  
  384. `min(S)'
  385.      smallest item of S
  386.  
  387. `max(S)'
  388.      largest item of S
  389.  
  390. `X in S'
  391.      `1' if an item of S is equal to X, else `0'
  392.  
  393. `X not in S'
  394.      `0' if an item of S is equal to X, else `1'
  395.  
  396. `S + T'
  397.      the concatenation of S and T
  398.  
  399. `S * N, N * S'
  400.      N copies of S concatenated
  401.  
  402. `S[I]'
  403.      I'th item of S, origin 0  --  (1)
  404.  
  405. `S[I:J]'
  406.      slice of S from I to J  --  (1), (2)
  407.  
  408. Notes:
  409.  
  410. (1)
  411.      If I or J is negative, the index is relative to the end of the
  412.      string, i.e., `len(S) + I' or `len(S) + J' is substituted.  But
  413.      note that `-0' is still `0'.
  414.  
  415. (2)
  416.      The slice of S from I to J is defined as the sequence of items
  417.      with index K such that `I <= K < J'.  If I or J is greater than
  418.      `len(S)', use `len(S)'.  If I is omitted, use `0'.  If J is
  419.      omitted, use `len(S)'.  If I is greater than or equal to J, the
  420.      slice is empty.
  421.  
  422. * Menu:
  423.  
  424. * More String Operations::
  425. * Mutable Sequence Types::
  426.  
  427. 
  428. File: python-lib.info,  Node: More String Operations,  Next: Mutable Sequence Types,  Prev: Sequence Types,  Up: Sequence Types
  429.  
  430. More String Operations.
  431. .......................
  432.  
  433. String objects have one unique built-in operation: the `%' operator
  434. (modulo) with a string left argument interprets this string as a C
  435. sprintf format string to be applied to the right argument, and returns
  436. the string resulting from this formatting operation.
  437.  
  438. Unless the format string requires exactly one argument, the right
  439. argument should be a tuple of the correct size.  The following format
  440. characters are understood: %, c, s, i, d, u, o, x, X, e, E, f, g, G.
  441. Width and precision may be a * to specify that an integer argument
  442. specifies the actual width or precision.  The flag characters -, +,
  443. blank, # and 0 are understood.  The size specifiers h, l or L may be
  444. present but are ignored.  The ANSI features `%p' and `%n' are not
  445. supported.  Since Python strings have an explicit length, `%s'
  446. conversions don't assume that `'
  447. 0'' is the end of the string.
  448.  
  449. For safety reasons, huge floating point precisions are truncated; `%f'
  450. conversions for huge numbers are replaced by `%g' conversions.  All
  451. other errors raise exceptions.
  452.  
  453. Additional string operations are defined in standard module `string'
  454. and in built-in module `regex'.
  455.  
  456. 
  457. File: python-lib.info,  Node: Mutable Sequence Types,  Prev: More String Operations,  Up: Sequence Types
  458.  
  459. Mutable Sequence Types.
  460. .......................
  461.  
  462. List objects support additional operations that allow in-place
  463. modification of the object.  These operations would be supported by
  464. other mutable sequence types (when added to the language) as well.
  465. Strings and tuples are immutable sequence types and such objects cannot
  466. be modified once created.  The following operations are defined on
  467. mutable sequence types (where X is an arbitrary object):
  468.  
  469. *Operation*
  470.      *Result*  --  *Notes*
  471.  
  472. `S[I] = X'
  473.      item I of S is replaced by X
  474.  
  475. `S[I:J] = T'
  476.      slice of S from I to J is replaced by T
  477.  
  478. `del S[I:J]'
  479.      same as `S[I:J] = []'
  480.  
  481. `S.append(X)'
  482.      same as `S[len(X):len(X)] = [X]'
  483.  
  484. `S.count(X)'
  485.      return number of I's for which `S[I] == X'
  486.  
  487. `S.index(X)'
  488.      return smallest I such that `S[I] == X'  --  (1)
  489.  
  490. `S.insert(I, X)'
  491.      same as `S[I:I] = [X]'
  492.  
  493. `S.remove(X)'
  494.      same as `del S[S.index(X)]'  --  (1)
  495.  
  496. `S.reverse()'
  497.      reverses the items of S in place
  498.  
  499. `S.sort()'
  500.      permutes the items of S to satisfy `S[I] <= S[J]', for `I < J'  --
  501.      (2)
  502.  
  503. Notes:
  504. (1)
  505.      Raises an exception when X is not found in S.
  506.  
  507. (2)
  508.      The `sort()' method takes an optional argument specifying a
  509.      comparison function of two arguments (list items) which should
  510.      return `-1', `0' or `1' depending on whether the first argument is
  511.      considered smaller than, equal to, or larger than the second
  512.      argument.  Note that this slows the sorting process down
  513.      considerably; e.g. to sort an array in reverse order it is much
  514.      faster to use calls to `sort()' and `reverse()' than to use
  515.      `sort()' with a comparison function that reverses the ordering of
  516.      the elements.
  517.  
  518. 
  519. File: python-lib.info,  Node: Mapping Types,  Next: Other Built-in Types,  Prev: Sequence Types,  Up: Types
  520.  
  521. Mapping Types
  522. -------------
  523.  
  524. A "mapping" object maps values of one type (the key type) to arbitrary
  525. objects.  Mappings are mutable objects.  There is currently only one
  526. mapping type, the "dictionary".  A dictionary's keys are almost
  527. arbitrary values.  The only types of values not acceptable as keys are
  528. values containing lists or dictionaries or other mutable types that are
  529. compared by value rather than by object identity.  Numeric types used
  530. for keys obey the normal rules for numeric comparison: if two numbers
  531. compare equal (e.g. 1 and 1.0) then they can be used interchangeably to
  532. index the same dictionary entry.
  533.  
  534. Dictionaries are created by placing a comma-separated list of `KEY:
  535. VALUE' pairs within braces, for example: `{'jack': 4098, 'sjoerd:
  536. 4127}' or `{4098: 'jack', 4127: 'sjoerd}'.
  537.  
  538. The following operations are defined on mappings (where A is a mapping,
  539. K is a key and X is an arbitrary object):
  540.  
  541. *Operation*
  542.      *Result*  --  *Notes*
  543.  
  544. `len(A)'
  545.      the number of items in A
  546.  
  547. `A[K]'
  548.      the item of A with key K  --  (1)
  549.  
  550. `A[K] = X'
  551.      set `A[K]' to X
  552.  
  553. `del A[K]'
  554.      remove `A[K]' from A  --  (1)
  555.  
  556. `A.items()'
  557.      a copy of A's list of (key, item) pairs  --  (2)
  558.  
  559. `A.keys()'
  560.      a copy of A's list of keys  --  (2)
  561.  
  562. `A.values()'
  563.      a copy of A's list of values  --  (2)
  564.  
  565. `A.has_key(K)'
  566.      `1' if A has a key K, else `0'
  567.  
  568. Notes:
  569. (1)
  570.      Raises an exception if K is not in the map.
  571.  
  572. (2)
  573.      Keys and values are listed in random order, but at any moment the
  574.      ordering of the `keys()', `values()' and `items()' lists is the
  575.      consistent with each other.
  576.  
  577. 
  578. File: python-lib.info,  Node: Other Built-in Types,  Next: Special Attributes,  Prev: Mapping Types,  Up: Types
  579.  
  580. Other Built-in Types
  581. --------------------
  582.  
  583. The interpreter supports several other kinds of objects.  Most of these
  584. support only one or two operations.
  585.  
  586. * Menu:
  587.  
  588. * Modules::
  589. * Classes and Class Instances::
  590. * Functions::
  591. * Methods::
  592. * Type Objects::
  593. * The Null Object::
  594. * File Objects::
  595. * Internal Objects::
  596.  
  597. 
  598. File: python-lib.info,  Node: Modules,  Next: Classes and Class Instances,  Prev: Other Built-in Types,  Up: Other Built-in Types
  599.  
  600. Modules.
  601. ........
  602.  
  603. The only special operation on a module is attribute access: `M.NAME',
  604. where M is a module and NAME accesses a name defined in M's symbol
  605. table.  Module attributes can be assigned to.  (Note that the `import'
  606. statement is not, strictly spoken, an operation on a module object;
  607. `import FOO' does not require a module object named FOO to exist,
  608. rather it requires an (external) *definition* for a module named FOO
  609. somewhere.)
  610.  
  611. A special member of every module is `__dict__'.  This is the dictionary
  612. containing the module's symbol table.  Modifying this dictionary will
  613. actually change the module's symbol table, but direct assignment to the
  614. `__dict__' attribute is not possible (i.e., you can write
  615. `M.__dict__['a'] = 1', which defines `M.a' to be `1', but you can't
  616. write `M.__dict__ = {}'.
  617.  
  618. Modules are written like this: `<module 'sys'>'.
  619.  
  620. 
  621. File: python-lib.info,  Node: Classes and Class Instances,  Next: Functions,  Prev: Modules,  Up: Other Built-in Types
  622.  
  623. Classes and Class Instances.
  624. ............................
  625.  
  626. (See the Python Reference Manual for these.)
  627.  
  628. 
  629. File: python-lib.info,  Node: Functions,  Next: Methods,  Prev: Classes and Class Instances,  Up: Other Built-in Types
  630.  
  631. Functions.
  632. ..........
  633.  
  634. Function objects are created by function definitions.  The only
  635. operation on a function object is to call it: `FUNC(ARGUMENT-LIST)'.
  636.  
  637. There are really two flavors of function objects: built-in functions
  638. and user-defined functions.  Both support the same operation (to call
  639. the function), but the implementation is different, hence the different
  640. object types.
  641.  
  642. The implementation adds two special read-only attributes: `F.func_code'
  643. is a function's "code object" (see below) and `F.func_globals' is the
  644. dictionary used as the function's global name space (this is the same
  645. as `M.__dict__' where M is the module in which the function F was
  646. defined).
  647.  
  648. 
  649. File: python-lib.info,  Node: Methods,  Next: Type Objects,  Prev: Functions,  Up: Other Built-in Types
  650.  
  651. Methods.
  652. ........
  653.  
  654. Methods are functions that are called using the attribute notation.
  655. There are two flavors: built-in methods (such as `append()' on lists)
  656. and class instance methods.  Built-in methods are described with the
  657. types that support them.
  658.  
  659. The implementation adds two special read-only attributes to class
  660. instance methods: `M.im_self' is the object whose method this is, and
  661. `M.im_func' is the function implementing the method.  Calling `M(ARG-1,
  662. ARG-2, ..., ARG-N)' is completely equivalent to calling
  663. `M.im_func(M.im_self, ARG-1, ARG-2, ..., ARG-N)'.
  664.  
  665. (See the Python Reference Manual for more info.)
  666.  
  667. 
  668. File: python-lib.info,  Node: Type Objects,  Next: The Null Object,  Prev: Methods,  Up: Other Built-in Types
  669.  
  670. Type Objects.
  671. .............
  672.  
  673. Type objects represent the various object types.  An object's type is
  674. accessed by the built-in function `type()'.  There are no special
  675. operations on types.
  676.  
  677. Types are written like this: `<type 'int'>'.
  678.  
  679. 
  680. File: python-lib.info,  Node: The Null Object,  Next: File Objects,  Prev: Type Objects,  Up: Other Built-in Types
  681.  
  682. The Null Object.
  683. ................
  684.  
  685. This object is returned by functions that don't explicitly return a
  686. value.  It supports no special operations.  There is exactly one null
  687. object, named `None' (a built-in name).
  688.  
  689. It is written as `None'.
  690.  
  691. 
  692. File: python-lib.info,  Node: File Objects,  Next: Internal Objects,  Prev: The Null Object,  Up: Other Built-in Types
  693.  
  694. File Objects.
  695. .............
  696.  
  697. File objects are implemented using C's `stdio' package and can be
  698. created with the built-in function `open()' described under Built-in
  699. Functions below.
  700.  
  701. When a file operation fails for an I/O-related reason, the exception
  702. `IOError' is raised.  This includes situations where the operation is
  703. not defined for some reason, like `seek()' on a tty device or writing a
  704. file opened for reading.
  705.  
  706. Files have the following methods:
  707.  
  708.  - Method on file: close ()
  709.      Close the file.  A closed file cannot be read or written anymore.
  710.  
  711.  - Method on file: flush ()
  712.      Flush the internal buffer, like `stdio''s `fflush()'.
  713.  
  714.  - Method on file: isatty ()
  715.      Return `1' if the file is connected to a tty(-like) device, else
  716.      `0'.
  717.  
  718.  - Method on file: read (SIZE)
  719.      Read at most SIZE bytes from the file (less if the read hits EOF
  720.      or no more data is immediately available on a pipe, tty or similar
  721.      device).  If the SIZE argument is omitted, read all data until EOF
  722.      is reached.  The bytes are returned as a string object.  An empty
  723.      string is returned when EOF is encountered immediately.  (For
  724.      certain files, like ttys, it makes sense to continue reading after
  725.      an EOF is hit.)
  726.  
  727.  - Method on file: readline ()
  728.      Read one entire line from the file.  A trailing newline character
  729.      is kept in the string (but may be absent when a file ends with an
  730.      incomplete line).  An empty string is returned when EOF is hit
  731.      immediately.  Note: unlike `stdio''s `fgets()', the returned
  732.      string contains null characters (`'\0'') if they occurred in the
  733.      input.
  734.  
  735.  - Method on file: readlines ()
  736.      Read until EOF using `readline()' and return a list containing the
  737.      lines thus read.
  738.  
  739.  - Method on file: seek (OFFSET, WHENCE)
  740.      Set the file's current position, like `stdio''s `fseek()'.  The
  741.      WHENCE argument is optional and defaults to `0' (absolute file
  742.      positioning); other values are `1' (seek relative to the current
  743.      position) and `2' (seek relative to the file's end).  There is no
  744.      return value.
  745.  
  746.  - Method on file: tell ()
  747.      Return the file's current position, like `stdio''s `ftell()'.
  748.  
  749.  - Method on file: write (STR)
  750.      Write a string to the file.  There is no return value.
  751.  
  752. 
  753. File: python-lib.info,  Node: Internal Objects,  Prev: File Objects,  Up: Other Built-in Types
  754.  
  755. Internal Objects.
  756. .................
  757.  
  758. (See the Python Reference Manual for these.)
  759.  
  760. 
  761. File: python-lib.info,  Node: Special Attributes,  Prev: Other Built-in Types,  Up: Types
  762.  
  763. Special Attributes
  764. ------------------
  765.  
  766. The implementation adds a few special read-only attributes to several
  767. object types, where they are relevant:
  768.  
  769.    * `X.__dict__' is a dictionary of some sort used to store an
  770.      object's (writable) attributes;
  771.  
  772.    * `X.__methods__' lists the methods of many built-in object types,
  773.      e.g., `[].__methods__' is `['append', 'count', 'index', 'insert',
  774.      'remove', 'reverse', 'sort']';
  775.  
  776.    * `X.__members__' lists data attributes;
  777.  
  778.    * `X.__class__' is the class to which a class instance belongs;
  779.  
  780.    * `X.__bases__' is the tuple of base classes of a class object.
  781.  
  782. 
  783. File: python-lib.info,  Node: Exceptions,  Next: Built-in Functions,  Prev: Types,  Up: Built-in Objects
  784.  
  785. Built-in Exceptions
  786. ===================
  787.  
  788. Exceptions are string objects.  Two distinct string objects with the
  789. same value are different exceptions.  This is done to force programmers
  790. to use exception names rather than their string value when specifying
  791. exception handlers.  The string value of all built-in exceptions is
  792. their name, but this is not a requirement for user-defined exceptions
  793. or exceptions defined by library modules.
  794.  
  795. The following exceptions can be generated by the interpreter or
  796. built-in functions.  Except where mentioned, they have an `associated
  797. value' indicating the detailed cause of the error.  This may be a
  798. string or a tuple containing several items of information (e.g., an
  799. error code and a string explaining the code).
  800.  
  801. User code can raise built-in exceptions.  This can be used to test an
  802. exception handler or to report an error condition `just like' the
  803. situation in which the interpreter raises the same exception; but
  804. beware that there is nothing to prevent user code from raising an
  805. inappropriate error.
  806.  
  807.  - built-in exception: AttributeError
  808.      Raised when an attribute reference or assignment fails.  (When an
  809.      object does not support attributes references or attribute
  810.      assignments at all, `TypeError' is raised.)
  811.  
  812.  - built-in exception: EOFError
  813.      Raised when one of the built-in functions (`input()' or
  814.      `raw_input()') hits an end-of-file condition (EOF) without reading
  815.      any data.  (N.B.: the `read()' and `readline()' methods of file
  816.      objects return an empty string when they hit EOF.)  No associated
  817.      value.
  818.  
  819.  - built-in exception: IOError
  820.      Raised when an I/O operation (such as a `print' statement, the
  821.      built-in `open()' function or a method of a file object) fails for
  822.      an I/O-related reason, e.g., `file not found', `disk full'.
  823.  
  824.  - built-in exception: ImportError
  825.      Raised when an `import' statement fails to find the module
  826.      definition or when a `from ... import' fails to find a name that
  827.      is to be imported.
  828.  
  829.  - built-in exception: IndexError
  830.      Raised when a sequence subscript is out of range.  (Slice indices
  831.      are silently truncated to fall in the allowed range; if an index
  832.      is not a plain integer, `TypeError' is raised.)
  833.  
  834.  - built-in exception: KeyError
  835.      Raised when a mapping (dictionary) key is not found in the set of
  836.      existing keys.
  837.  
  838.  - built-in exception: KeyboardInterrupt
  839.      Raised when the user hits the interrupt key (normally `Control-C'
  840.      or DEL).  During execution, a check for interrupts is made
  841.      regularly.  Interrupts typed when a built-in function `input()' or
  842.      `raw_input()') is waiting for input also raise this exception.  No
  843.      associated value.
  844.  
  845.  - built-in exception: MemoryError
  846.      Raised when an operation runs out of memory but the situation may
  847.      still be rescued (by deleting some objects).  The associated value
  848.      is a string indicating what kind of (internal) operation ran out
  849.      of memory.  Note that because of the underlying memory management
  850.      architecture (C's `malloc()' function), the interpreter may not
  851.      always be able to completely recover from this situation; it
  852.      nevertheless raises an exception so that a stack traceback can be
  853.      printed, in case a run-away program was the cause.
  854.  
  855.  - built-in exception: NameError
  856.      Raised when a local or global name is not found.  This applies only
  857.      to unqualified names.  The associated value is the name that could
  858.      not be found.
  859.  
  860.  - built-in exception: OverflowError
  861.      Raised when the result of an arithmetic operation is too large to
  862.      be represented.  This cannot occur for long integers (which would
  863.      rather raise `MemoryError' than give up).  Because of the lack of
  864.      standardization of floating point exception handling in C, most
  865.      floating point operations also aren't checked.  For plain integers,
  866.      all operations that can overflow are checked except left shift,
  867.      where typical applications prefer to drop bits than raise an
  868.      exception.
  869.  
  870.  - built-in exception: RuntimeError
  871.      Raised when an error is detected that doesn't fall in any of the
  872.      other categories.  The associated value is a string indicating what
  873.      precisely went wrong.  (This exception is a relic from a previous
  874.      version of the interpreter; it is not used any more except by some
  875.      extension modules that haven't been converted to define their own
  876.      exceptions yet.)
  877.  
  878.  - built-in exception: SyntaxError
  879.      Raised when the parser encounters a syntax error.  This may occur
  880.      in an `import' statement, in an `exec' statement, in a call to the
  881.      built-in function `eval()' or `input()', or when reading the
  882.      initial script or standard input (also interactively).
  883.  
  884.  - built-in exception: SystemError
  885.      Raised when the interpreter finds an internal error, but the
  886.      situation does not look so serious to cause it to abandon all hope.
  887.      The associated value is a string indicating what went wrong (in
  888.      low-level terms).
  889.  
  890.      You should report this to the author or maintainer of your Python
  891.      interpreter.  Be sure to report the version string of the Python
  892.      interpreter (`sys.version'; it is also printed at the start of an
  893.      interactive Python session), the exact error message (the
  894.      exception's associated value) and if possible the source of the
  895.      program that triggered the error.
  896.  
  897.  - built-in exception: SystemExit
  898.      This exception is raised by the `sys.exit()' function.  When it is
  899.      not handled, the Python interpreter exits; no stack traceback is
  900.      printed.  If the associated value is a plain integer, it specifies
  901.      the system exit status (passed to C's `exit()' function); if it is
  902.      `None', the exit status is zero; if it has another type (such as a
  903.      string), the object's value is printed and the exit status is one.
  904.  
  905.      A call to `sys.exit' is translated into an exception so that
  906.      clean-up handlers (`finally' clauses of `try' statements) can be
  907.      executed, and so that a debugger can execute a script without
  908.      running the risk of losing control.  The `posix._exit()' function
  909.      can be used if it is absolutely positively necessary to exit
  910.      immediately (e.g., after a `fork()' in the child process).
  911.  
  912.  - built-in exception: TypeError
  913.      Raised when a built-in operation or function is applied to an
  914.      object of inappropriate type.  The associated value is a string
  915.      giving details about the type mismatch.
  916.  
  917.  - built-in exception: ValueError
  918.      Raised when a built-in operation or function receives an argument
  919.      that has the right type but an inappropriate value, and the
  920.      situation is not described by a more precise exception such as
  921.      `IndexError'.
  922.  
  923.  - built-in exception: ZeroDivisionError
  924.      Raised when the second argument of a division or modulo operation
  925.      is zero.  The associated value is a string indicating the type of
  926.      the operands and the operation.
  927.  
  928. 
  929. File: python-lib.info,  Node: Built-in Functions,  Prev: Exceptions,  Up: Built-in Objects
  930.  
  931. Built-in Functions
  932. ==================
  933.  
  934. The Python interpreter has a number of functions built into it that are
  935. always available.  They are listed here in alphabetical order.
  936.  
  937.  - built-in function: abs (X)
  938.      Return the absolute value of a number.  The argument may be a plain
  939.      or long integer or a floating point number.
  940.  
  941.  - built-in function: apply (FUNCTION, ARGS)
  942.      The FUNCTION argument must be a callable object (a user-defined or
  943.      built-in function or method, or a class object) and the ARGS
  944.      argument must be a tuple.  The FUNCTION is called with ARGS as
  945.      argument list; the number of arguments is the the length of the
  946.      tuple.  (This is different from just calling `FUNC(ARGS)', since
  947.      in that case there is always exactly one argument.)
  948.  
  949.  - built-in function: chr (I)
  950.      Return a string of one character whose ASCII code is the integer
  951.      I, e.g., `chr(97)' returns the string `'a''.  This is the inverse
  952.      of `ord()'.  The argument must be in the range [0..255], inclusive.
  953.  
  954.  - built-in function: cmp (X, Y)
  955.      Compare the two objects X and Y and return an integer according to
  956.      the outcome.  The return value is negative if `X < Y', zero if `X
  957.      == Y' and strictly positive if `X > Y'.
  958.  
  959.  - built-in function: coerce (X, Y)
  960.      Return a tuple consisting of the two numeric arguments converted to
  961.      a common type, using the same rules as used by arithmetic
  962.      operations.
  963.  
  964.  - built-in function: compile (STRING, FILENAME, KIND)
  965.      Compile the STRING into a code object.  Code objects can be
  966.      executed by a `exec()' statement or evaluated by a call to
  967.      `eval()'.  The FILENAME argument should give the file from which
  968.      the code was read; pass e.g. `'<string>'' if it wasn't read from a
  969.      file.  The KIND argument specifies what kind of code must be
  970.      compiled; it can be `'exec'' if STRING consists of a sequence of
  971.      statements, or `'eval'' if it consists of a single expression.
  972.  
  973.  - built-in function: dir ()
  974.      Without arguments, return the list of names in the current local
  975.      symbol table.  With a module, class or class instance object as
  976.      argument (or anything else that has a `__dict__' attribute),
  977.      returns the list of names in that object's attribute dictionary.
  978.      The resulting list is sorted.  For example:
  979.  
  980.           >>> import sys
  981.           >>> dir()
  982.           ['sys']
  983.           >>> dir(sys)
  984.           ['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout']
  985.           >>>
  986.  
  987.  - built-in function: divmod (A, B)
  988.      Take two numbers as arguments and return a pair of integers
  989.      consisting of their integer quotient and remainder.  With mixed
  990.      operand types, the rules for binary arithmetic operators apply.
  991.      For plain and long integers, the result is the same as `(A / B, A
  992.      % B)'.  For floating point numbers the result is the same as
  993.      `(math.floor(A / B), A % B)'.
  994.  
  995.  - built-in function: eval (S, GLOBALS, LOCALS)
  996.      The arguments are a string and two optional dictionaries.  The
  997.      string argument is parsed and evaluated as a Python expression
  998.      (technically speaking, a condition list) using the dictionaries as
  999.      global and local name space.  The string must not contain null
  1000.      bytes or newline characters.  The return value is the result of
  1001.      the expression.  If the third argument is omitted it defaults to
  1002.      the second.  If both dictionaries are omitted, the expression is
  1003.      executed in the environment where `eval' is called.  Syntax errors
  1004.      are reported as exceptions.  Example:
  1005.  
  1006.           >>> x = 1
  1007.           >>> print eval('x+1')
  1008.           2
  1009.           >>>
  1010.      This function can also be used to execute arbitrary code objects
  1011.      (e.g. created by `compile()').  In this case pass a code object
  1012.      instead of a string.  The code object must have been compiled
  1013.      passing `'eval'' to the KIND argument.
  1014.  
  1015.      Note: dynamic execution of statements is supported by the `exec'
  1016.      statement.
  1017.  
  1018.  
  1019.  - built-in function: filter (FUNCTION, LIST)
  1020.      Construct a list from those elements of LIST for which FUNCTION
  1021.      returns true.  If LIST is a string or a tuple, the result also has
  1022.      that type; otherwise it is always a list.  If FUNCTION is `None',
  1023.      the identity function is assumed, i.e. all elements of LIST that
  1024.      are false (zero or empty) are removed.
  1025.  
  1026.  - built-in function: float (X)
  1027.      Convert a number to floating point.  The argument may be a plain or
  1028.      long integer or a floating point number.
  1029.  
  1030.  - built-in function: getattr (OBJECT, NAME)
  1031.      The arguments are an object and a string.  The string must be the
  1032.      name of one of the object's attributes.  The result is the value
  1033.      of that attribute.  For example, `getattr(X, 'FOOBAR')' is
  1034.      equivalent to `X.FOOBAR'.
  1035.  
  1036.  - built-in function: hasattr (OBJECT, NAME)
  1037.      The arguments are an object and a string.  The result is 1 if the
  1038.      string is the name of one of the object's attributes, 0 if not.
  1039.      (This is implemented by calling `getattr(object, name)' and seeing
  1040.      whether it raises an exception or not.)
  1041.  
  1042.  - built-in function: hash (OBJECT)
  1043.      Return the hash value of the object (if it has one).  Hash values
  1044.      are 32-bit integers.  They are used to quickly compare dictionary
  1045.      keys during a dictionary lookup.  Numeric values that compare equal
  1046.      have the same hash value (even if they are of different types, e.g.
  1047.      1 and 1.0).
  1048.  
  1049.  - built-in function: hex (X)
  1050.      Convert a number to a hexadecimal string.  The result is a valid
  1051.      Python expression.
  1052.  
  1053.  - built-in function: id (OBJECT)
  1054.      Return the `identity' of an object.  This is an integer which is
  1055.      guaranteed to be unique and constant for this object during its
  1056.      lifetime.  (Two objects whose lifetimes are disjunct may have the
  1057.      same id() value.)  (Implementation note: this is the address of the
  1058.      object.)
  1059.  
  1060.  - built-in function: input (PROMPT)
  1061.      Almost equivalent to `eval(raw_input(PROMPT))'.  As for
  1062.      `raw_input()', the prompt argument is optional.  The difference is
  1063.      that a long input expression may be broken over multiple lines
  1064.      using the backslash convention.
  1065.  
  1066.  - built-in function: int (X)
  1067.      Convert a number to a plain integer.  The argument may be a plain
  1068.      or long integer or a floating point number.
  1069.  
  1070.  - built-in function: len (S)
  1071.      Return the length (the number of items) of an object.  The argument
  1072.      may be a sequence (string, tuple or list) or a mapping
  1073.      (dictionary).
  1074.  
  1075.  - built-in function: long (X)
  1076.      Convert a number to a long integer.  The argument may be a plain or
  1077.      long integer or a floating point number.
  1078.  
  1079.  - built-in function: map (FUNCTION, LIST, ...)
  1080.      Apply FUNCTION to every item of LIST and return a list of the
  1081.      results.  If additional LIST arguments are passed, FUNCTION must
  1082.      take that many arguments and is applied to the items of all lists
  1083.      in parallel; if a list is shorter than another it is assumed to be
  1084.      extended with `None' items.  If FUNCTION is `None', the identity
  1085.      function is assumed; if there are multiple list arguments, `map'
  1086.      returns a list consisting of tuples containing the corresponding
  1087.      items from all lists (i.e. a kind of transpose operation).  The
  1088.      LIST arguments may be any kind of sequence; the result is always a
  1089.      list.
  1090.  
  1091.  - built-in function: max (S)
  1092.      Return the largest item of a non-empty sequence (string, tuple or
  1093.      list).
  1094.  
  1095.  - built-in function: min (S)
  1096.      Return the smallest item of a non-empty sequence (string, tuple or
  1097.      list).
  1098.  
  1099.  - built-in function: oct (X)
  1100.      Convert a number to an octal string.  The result is a valid Python
  1101.      expression.
  1102.  
  1103.  - built-in function: open (FILENAME, MODE)
  1104.      Return a new file object (described earlier under Built-in Types).
  1105.      The string arguments are the same as for `stdio''s `fopen()':
  1106.      FILENAME is the file name to be opened, MODE indicates how the
  1107.      file is to be opened: `'r'' for reading, `'w'' for writing
  1108.      (truncating an existing file), and `'a'' opens it for appending.
  1109.      Modes `'r+'', `'w+'' and `'a+'' open the file for updating,
  1110.      provided the underlying `stdio' library understands this.  On
  1111.      systems that differentiate between binary and text files, `'b''
  1112.      appended to the mode opens the file in binary mode.  If the file
  1113.      cannot be opened, `IOError' is raised.
  1114.  
  1115.  - built-in function: ord (C)
  1116.      Return the ASCII value of a string of one character.  E.g.,
  1117.      `ord('a')' returns the integer `97'.  This is the inverse of
  1118.      `chr()'.
  1119.  
  1120.  - built-in function: pow (X, Y)
  1121.      Return X to the power Y.  The arguments must have numeric types.
  1122.      With mixed operand types, the rules for binary arithmetic
  1123.      operators apply.  The effective operand type is also the type of
  1124.      the result; if the result is not expressible in this type, the
  1125.      function raises an exception; e.g., `pow(2, -1)' is not allowed.
  1126.  
  1127.  - built-in function: range (START, END, STEP)
  1128.      This is a versatile function to create lists containing arithmetic
  1129.      progressions.  It is most often used in `for' loops.  The
  1130.      arguments must be plain integers.  If the STEP argument is
  1131.      omitted, it defaults to `1'.  If the START argument is omitted, it
  1132.      defaults to `0'.  The full form returns a list of plain integers
  1133.      `[START, START + STEP, START + 2 * STEP, ...]'.  If STEP is
  1134.      positive, the last element is the largest `START + I * STEP' less
  1135.      than END; if STEP is negative, the last element is the largest
  1136.      `START + I * STEP' greater than END.  STEP must not be zero.
  1137.      Example:
  1138.  
  1139.           >>> range(10)
  1140.           [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1141.           >>> range(1, 11)
  1142.           [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  1143.           >>> range(0, 30, 5)
  1144.           [0, 5, 10, 15, 20, 25]
  1145.           >>> range(0, 10, 3)
  1146.           [0, 3, 6, 9]
  1147.           >>> range(0, -10, -1)
  1148.           [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  1149.           >>> range(0)
  1150.           []
  1151.           >>> range(1, 0)
  1152.           []
  1153.           >>>
  1154.  
  1155.  - built-in function: raw_input (PROMPT)
  1156.      The string argument is optional; if present, it is written to
  1157.      standard output without a trailing newline.  The function then
  1158.      reads a line from input, converts it to a string (stripping a
  1159.      trailing newline), and returns that.  When EOF is read, `EOFError'
  1160.      is raised.  Example:
  1161.  
  1162.           >>> s = raw_input('--> ')
  1163.           --> Monty Python's Flying Circus
  1164.           >>> s
  1165.           'Monty Python\'s Flying Circus'
  1166.           >>>
  1167.  
  1168.  - built-in function: reduce (FUNCTION, LIST, INITIALIZER)
  1169.      Apply the binary FUNCTION to the items of LIST so as to reduce the
  1170.      list to a single value.  E.g., `reduce(lambda x, y: x*y, LIST, 1)'
  1171.      returns the product of the elements of LIST.  The optional
  1172.      INITIALIZER can be thought of as being prepended to LIST so as to
  1173.      allow reduction of an empty LIST.  The LIST arguments may be any
  1174.      kind of sequence.
  1175.  
  1176.  - built-in function: reload (MODULE)
  1177.      Re-parse and re-initialize an already imported MODULE.  The
  1178.      argument must be a module object, so it must have been successfully
  1179.      imported before.  This is useful if you have edited the module
  1180.      source file using an external editor and want to try out the new
  1181.      version without leaving the Python interpreter.  Note that if a
  1182.      module is syntactically correct but its initialization fails, the
  1183.      first `import' statement for it does not import the name, but does
  1184.      create a (partially initialized) module object; to reload the
  1185.      module you must first `import' it again (this will just make the
  1186.      partially initialized module object available) before you can
  1187.      `reload()' it.
  1188.  
  1189.  - built-in function: repr (OBJECT)
  1190.      Return a string containing a printable representation of an object.
  1191.      This is the same value yielded by conversions (reverse quotes).
  1192.      It is sometimes useful to be able to access this operation as an
  1193.      ordinary function.  For many types, this function makes an attempt
  1194.      to return a string that would yield an object with the same value
  1195.      when passed to `eval()'.
  1196.  
  1197.  - built-in function: round (X, N)
  1198.      Return the floating point value X rounded to N digits after the
  1199.      decimal point.  If N is omitted, it defaults to zero.  The result
  1200.      is a floating point number.  Values are rounded to the closest
  1201.      multiple of 10 to the power minus N; if two multiples are equally
  1202.      close, rounding is done away from 0 (so e.g.  `round(0.5)' is
  1203.      `1.0' and `round(-0.5)' is `-1.0').
  1204.  
  1205.  - built-in function: setattr (OBJECT, NAME, VALUE)
  1206.      This is the counterpart of `getattr'.  The arguments are an
  1207.      object, a string and an arbitrary value.  The string must be the
  1208.      name of one of the object's attributes.  The function assigns the
  1209.      value to the attribute, provided the object allows it.  For
  1210.      example, `setattr(X, 'FOOBAR', 123)' is equivalent to `X.FOOBAR =
  1211.      123'.
  1212.  
  1213.  - built-in function: str (OBJECT)
  1214.      Return a string containing a nicely printable representation of an
  1215.      object.  For strings, this returns the string itself.  The
  1216.      difference with `repr(OBJECT' is that `str(OBJECT' does not always
  1217.      attempt to return a string that is acceptable to `eval()'; its
  1218.      goal is to return a printable string.
  1219.  
  1220.  - built-in function: type (OBJECT)
  1221.      Return the type of an OBJECT.  The return value is a type object.
  1222.      There is not much you can do with type objects except compare them
  1223.      to other type objects; e.g., the following checks if a variable is
  1224.      a string:
  1225.  
  1226.           >>> if type(x) == type(''): print 'It is a string'
  1227.  
  1228. 
  1229. File: python-lib.info,  Node: Built-in Modules,  Next: Standard Modules,  Prev: Built-in Objects,  Up: Top
  1230.  
  1231. Built-in Modules
  1232. ****************
  1233.  
  1234. The modules described in this chapter are built into the interpreter
  1235. and considered part of Python's standard environment: they are always
  1236. avaialble.(1)
  1237.  
  1238. * Menu:
  1239.  
  1240. * sys::
  1241. * __builtin__::
  1242. * __main__::
  1243. * array::
  1244. * math::
  1245. * time::
  1246. * regex::
  1247. * marshal::
  1248. * struct::
  1249.  
  1250. ---------- Footnotes ----------
  1251.  
  1252. (1)  at least in theory -- it is possible to specify at build time that
  1253. one or more of these modules should be excluded, but it would be
  1254. antisocial to do so.
  1255.  
  1256. 
  1257. File: python-lib.info,  Node: sys,  Next: __builtin__,  Prev: Built-in Modules,  Up: Built-in Modules
  1258.  
  1259. Built-in Module `sys'
  1260. =====================
  1261.  
  1262. This module provides access to some variables used or maintained by the
  1263. interpreter and to functions that interact strongly with the
  1264. interpreter.  It is always available.
  1265.  
  1266.  - data of module sys: argv
  1267.      The list of command line arguments passed to a Python script.
  1268.      `sys.argv[0]' is the script name.  If no script name was passed to
  1269.      the Python interpreter, `sys.argv' is empty.
  1270.  
  1271.  - data of module sys: builtin_module_names
  1272.      A list of strings giving the names of all modules that are compiled
  1273.      into this Python interpreter.  (This information is not available
  1274.      in any other way -- `sys.modules.keys()' only lists the imported
  1275.      modules.)
  1276.  
  1277.  - data of module sys: exc_type
  1278.  
  1279.  - data of module sys: exc_value
  1280.  
  1281.  - data of module sys: exc_traceback
  1282.      These three variables are not always defined; they are set when an
  1283.      exception handler (an `except' clause of a `try' statement) is
  1284.      invoked.  Their meaning is: `exc_type' gets the exception type of
  1285.      the exception being handled; `exc_value' gets the exception
  1286.      parameter (its "associated value" or the second argument to
  1287.      `raise'); `exc_traceback' gets a traceback object which
  1288.      encapsulates the call stack at the point where the exception
  1289.      originally occurred.
  1290.  
  1291.  - function of module sys: exit (N)
  1292.      Exit from Python with numeric exit status N.  This is implemented
  1293.      by raising the `SystemExit' exception, so cleanup actions
  1294.      specified by `finally' clauses of `try' statements are honored,
  1295.      and it is possible to catch the exit attempt at an outer level.
  1296.  
  1297.  - data of module sys: exitfunc
  1298.      This value is not actually defined by the module, but can be set by
  1299.      the user (or by a program) to specify a clean-up action at program
  1300.      exit.  When set, it should be a parameterless function.  This
  1301.      function will be called when the interpreter exits in any way (but
  1302.      not when a fatal error occurs: in that case the interpreter's
  1303.      internal state cannot be trusted).
  1304.  
  1305.  - data of module sys: last_type
  1306.  
  1307.  - data of module sys: last_value
  1308.  
  1309.  - data of module sys: last_traceback
  1310.      These three variables are not always defined; they are set when an
  1311.      exception is not handled and the interpreter prints an error
  1312.      message and a stack traceback.  Their intended use is to allow an
  1313.      interactive user to import a debugger module and engage in
  1314.      post-mortem debugging without having to re-execute the command
  1315.      that cause the error (which may be hard to reproduce).  The
  1316.      meaning of the variables is the same as that of `exc_type',
  1317.      `exc_value' and `exc_tracaback', respectively.
  1318.  
  1319.  - data of module sys: modules
  1320.      Gives the list of modules that have already been loaded.  This can
  1321.      be manipulated to force reloading of modules and other tricks.
  1322.  
  1323.  - data of module sys: path
  1324.      A list of strings that specifies the search path for modules.
  1325.      Initialized from the environment variable `PYTHONPATH', or an
  1326.      installation-dependent default.
  1327.  
  1328.  - data of module sys: ps1
  1329.  
  1330.  - data of module sys: ps2
  1331.      Strings specifying the primary and secondary prompt of the
  1332.      interpreter.  These are only defined if the interpreter is in
  1333.      interactive mode.  Their initial values in this case are `'>>> ''
  1334.      and `'... ''.
  1335.  
  1336.  - function of module sys: settrace (TRACEFUNC)
  1337.      Set the system's trace function, which allows you to implement a
  1338.      Python source code debugger in Python.  The standard modules `pdb'
  1339.      and `wdb' are such debuggers; the difference is that `wdb' uses
  1340.      windows and needs STDWIN, while `pdb' has a line-oriented
  1341.      interface not unlike dbx.  See the file `pdb.doc' in the Python
  1342.      library source directory for more documentation (both about `pdb'
  1343.      and `sys.trace').
  1344.  
  1345.  - function of module sys: setprofile (PROFILEFUNC)
  1346.      Set the system's profile function, which allows you to implement a
  1347.      Python source code profiler in Python.  The system's profile
  1348.      function is called similarly to the system's trace function (see
  1349.      `sys.settrace'), but it isn't called for each executed line of
  1350.      code (only on call and return and when an exception occurs).  Also,
  1351.      its return value is not used, so it can just return `None'.
  1352.  
  1353.  - data of module sys: stdin
  1354.  
  1355.  - data of module sys: stdout
  1356.  
  1357.  - data of module sys: stderr
  1358.      File objects corresponding to the interpreter's standard input,
  1359.      output and error streams.  `sys.stdin' is used for all interpreter
  1360.      input except for scripts but including calls to `input()' and
  1361.      `raw_input()'.  `sys.stdout' is used for the output of `print' and
  1362.      expression statements and for the prompts of `input()' and
  1363.      `raw_input()'.  The interpreter's own prompts and (almost all of)
  1364.      its error messages go to `sys.stderr'.  `sys.stdout' and
  1365.      `sys.stderr' needn't be built-in file objects: any object is
  1366.      acceptable as long as it has a `write' method that takes a string
  1367.      argument.
  1368.  
  1369.  - data of module sys: tracebacklimit
  1370.      When this variable is set to an integer value, it determines the
  1371.      maximum number of levels of traceback information printed when an
  1372.      unhandled exception occurs.  The default is 1000.  When set to 0 or
  1373.      less, all traceback information is suppressed and only the
  1374.      exception type and value are printed.
  1375.  
  1376. 
  1377. File: python-lib.info,  Node: __builtin__,  Next: __main__,  Prev: sys,  Up: Built-in Modules
  1378.  
  1379. Built-in Module `__builtin__'
  1380. =============================
  1381.  
  1382. This module provides direct access to all `built-in' identifier of
  1383. Python; e.g. `__builtin__.open' is the full name for the built-in
  1384. function `open'.
  1385.  
  1386. 
  1387. File: python-lib.info,  Node: __main__,  Next: array,  Prev: __builtin__,  Up: Built-in Modules
  1388.  
  1389. Built-in Module `__main__'
  1390. ==========================
  1391.  
  1392. This module represents the (otherwise anonymous) scope in which the
  1393. interpreter's main program executes -- commands read either from
  1394. standard input or from a script file.
  1395.  
  1396.