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 >
Encoding:
Text File  |  1996-11-14  |  23.6 KB  |  450 lines

  1. <TITLE>Built-in Functions -- Python library reference</TITLE>
  2. Prev: <A HREF="../e/exceptions" TYPE="Prev">Exceptions</A>  
  3. Up: <A HREF="../b/built-in_objects" TYPE="Up">Built-in Objects</A>  
  4. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  5. <H1>2.3. Built-in Functions</H1>
  6. The Python interpreter has a number of functions built into it that
  7. are always available.  They are listed here in alphabetical order.
  8. <P>
  9. <DL><DT><B>abs</B> (<VAR>x</VAR>) -- built-in function<DD>
  10. Return the absolute value of a number.  The argument may be a plain
  11. or long integer or a floating point number.
  12. </DL>
  13. <DL><DT><B>apply</B> (<VAR>function</VAR>, <VAR>args</VAR>[, <VAR>keywords</VAR>]) -- built-in function<DD>
  14. The <VAR>function</VAR> argument must be a callable object (a user-defined or
  15. built-in function or method, or a class object) and the <VAR>args</VAR>
  16. argument must be a tuple.  The <VAR>function</VAR> is called with
  17. <VAR>args</VAR> as argument list; the number of arguments is the the length
  18. of the tuple.  (This is different from just calling
  19. <CODE><VAR>func</VAR>(<VAR>args</VAR>)</CODE>, since in that case there is always
  20. exactly one argument.)
  21. If the optional <VAR>keywords</VAR> argument is present, it must be a
  22. dictionary whose keys are strings.  It specifies keyword arguments to
  23. be added to the end of the the argument list.
  24. </DL>
  25. <DL><DT><B>chr</B> (<VAR>i</VAR>) -- built-in function<DD>
  26. Return a string of one character whose ASCII code is the integer
  27. <VAR>i</VAR>, e.g., <CODE>chr(97)</CODE> returns the string <CODE>'a'</CODE>.  This is the
  28. inverse of <CODE>ord()</CODE>.  The argument must be in the range [0..255],
  29. inclusive.
  30. </DL>
  31. <DL><DT><B>cmp</B> (<VAR>x</VAR>, <VAR>y</VAR>) -- built-in function<DD>
  32. Compare the two objects <VAR>x</VAR> and <VAR>y</VAR> and return an integer
  33. according to the outcome.  The return value is negative if <CODE><VAR>x</VAR>
  34. < <VAR>y</VAR></CODE>, zero if <CODE><VAR>x</VAR> == <VAR>y</VAR></CODE> and strictly positive if
  35. <CODE><VAR>x</VAR> > <VAR>y</VAR></CODE>.
  36. </DL>
  37. <DL><DT><B>coerce</B> (<VAR>x</VAR>, <VAR>y</VAR>) -- built-in function<DD>
  38. Return a tuple consisting of the two numeric arguments converted to
  39. a common type, using the same rules as used by arithmetic
  40. operations.
  41. </DL>
  42. <DL><DT><B>compile</B> (<VAR>string</VAR>, <VAR>filename</VAR>, <VAR>kind</VAR>) -- built-in function<DD>
  43. Compile the <VAR>string</VAR> into a code object.  Code objects can be
  44. executed by an <CODE>exec</CODE> statement or evaluated by a call to
  45. <CODE>eval()</CODE>.  The <VAR>filename</VAR> argument should
  46. give the file from which the code was read; pass e.g. <CODE>'<string>'</CODE>
  47. if it wasn't read from a file.  The <VAR>kind</VAR> argument specifies
  48. what kind of code must be compiled; it can be <CODE>'exec'</CODE> if
  49. <VAR>string</VAR> consists of a sequence of statements, <CODE>'eval'</CODE>
  50. if it consists of a single expression, or <CODE>'single'</CODE> if
  51. it consists of a single interactive statement (in the latter case,
  52. expression statements that evaluate to something else than
  53. <CODE>None</CODE> will printed).
  54. </DL>
  55. <DL><DT><B>delattr</B> (<VAR>object</VAR>, <VAR>name</VAR>) -- built-in function<DD>
  56. This is a relative of <CODE>setattr</CODE>.  The arguments are an
  57. object and a string.  The string must be the name
  58. of one of the object's attributes.  The function deletes
  59. the named attribute, provided the object allows it.  For example,
  60. <CODE>delattr(<VAR>x</VAR>, '<VAR>foobar</VAR>')</CODE> is equivalent to
  61. <CODE>del <VAR>x</VAR>.<VAR>foobar</VAR></CODE>.
  62. </DL>
  63. <DL><DT><B>dir</B> () -- built-in function<DD>
  64. Without arguments, return the list of names in the current local
  65. symbol table.  With a module, class or class instance object as
  66. argument (or anything else that has a <CODE>__dict__</CODE> attribute),
  67. returns the list of names in that object's attribute dictionary.
  68. The resulting list is sorted.  For example:
  69. <P>
  70. <UL COMPACT><CODE>>>> import sys<P>
  71. >>> dir()<P>
  72. ['sys']<P>
  73. >>> dir(sys)<P>
  74. ['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout']<P>
  75. >>> <P>
  76. </CODE></UL>
  77. </DL>
  78. <DL><DT><B>divmod</B> (<VAR>a</VAR>, <VAR>b</VAR>) -- built-in function<DD>
  79. Take two numbers as arguments and return a pair of integers
  80. consisting of their integer quotient and remainder.  With mixed
  81. operand types, the rules for binary arithmetic operators apply.  For
  82. plain and long integers, the result is the same as
  83. <CODE>(<VAR>a</VAR> / <VAR>b</VAR>, <VAR>a</VAR> % <VAR>b</VAR>)</CODE>.
  84. For floating point numbers the result is the same as
  85. <CODE>(math.floor(<VAR>a</VAR> / <VAR>b</VAR>), <VAR>a</VAR> % <VAR>b</VAR>)</CODE>.
  86. </DL>
  87. <DL><DT><B>eval</B> (<VAR>expression</VAR>[, <VAR>globals</VAR>[, <VAR>locals</VAR>]]) -- built-in function<DD>
  88. The arguments are a string and two optional dictionaries.  The
  89. <VAR>expression</VAR> argument is parsed and evaluated as a Python
  90. expression (technically speaking, a condition list) using the
  91. <VAR>globals</VAR> and <VAR>locals</VAR> dictionaries as global and local name
  92. space.  If the <VAR>locals</VAR> dictionary is omitted it defaults to
  93. the <VAR>globals</VAR> dictionary.  If both dictionaries are omitted, the
  94. expression is executed in the environment where <CODE>eval</CODE> is
  95. called.  The return value is the result of the evaluated expression.
  96. Syntax errors are reported as exceptions.  Example:
  97. <P>
  98. <UL COMPACT><CODE>>>> x = 1<P>
  99. >>> print eval('x+1')<P>
  100. 2<P>
  101. >>> <P>
  102. </CODE></UL>
  103. This function can also be used to execute arbitrary code objects
  104. (e.g. created by <CODE>compile()</CODE>).  In this case pass a code
  105. object instead of a string.  The code object must have been compiled
  106. passing <CODE>'eval'</CODE> to the <VAR>kind</VAR> argument.
  107. <P>
  108. Hints: dynamic execution of statements is supported by the
  109. <CODE>exec</CODE> statement.  Execution of statements from a file is
  110. supported by the <CODE>execfile()</CODE> function.  The <CODE>globals()</CODE>
  111. and <CODE>locals()</CODE> functions returns the current global and local
  112. dictionary, respectively, which may be useful
  113. to pass around for use by <CODE>eval()</CODE> or <CODE>execfile()</CODE>.
  114. <P>
  115. </DL>
  116. <DL><DT><B>execfile</B> (<VAR>file</VAR>[, <VAR>globals</VAR>[, <VAR>locals</VAR>]]) -- built-in function<DD>
  117. This function is similar to the
  118. <CODE>exec</CODE> statement, but parses a file instead of a string.  It is
  119. different from the <CODE>import</CODE> statement in that it does not use
  120. the module administration --- it reads the file unconditionally and
  121. does not create a new module.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
  122. <P>
  123. The arguments are a file name and two optional dictionaries.  The
  124. file is parsed and evaluated as a sequence of Python statements
  125. (similarly to a module) using the <VAR>globals</VAR> and <VAR>locals</VAR>
  126. dictionaries as global and local name space.  If the <VAR>locals</VAR>
  127. dictionary is omitted it defaults to the <VAR>globals</VAR> dictionary.
  128. If both dictionaries are omitted, the expression is executed in the
  129. environment where <CODE>execfile()</CODE> is called.  The return value is
  130. <CODE>None</CODE>.
  131. </DL>
  132. <DL><DT><B>filter</B> (<VAR>function</VAR>, <VAR>list</VAR>) -- built-in function<DD>
  133. Construct a list from those elements of <VAR>list</VAR> for which
  134. <VAR>function</VAR> returns true.  If <VAR>list</VAR> is a string or a tuple,
  135. the result also has that type; otherwise it is always a list.  If
  136. <VAR>function</VAR> is <CODE>None</CODE>, the identity function is assumed,
  137. i.e. all elements of <VAR>list</VAR> that are false (zero or empty) are
  138. removed.
  139. </DL>
  140. <DL><DT><B>float</B> (<VAR>x</VAR>) -- built-in function<DD>
  141. Convert a number to floating point.  The argument may be a plain or
  142. long integer or a floating point number.
  143. </DL>
  144. <DL><DT><B>getattr</B> (<VAR>object</VAR>, <VAR>name</VAR>) -- built-in function<DD>
  145. The arguments are an object and a string.  The string must be the
  146. name
  147. of one of the object's attributes.  The result is the value of that
  148. attribute.  For example, <CODE>getattr(<VAR>x</VAR>, '<VAR>foobar</VAR>')</CODE> is equivalent to
  149. <CODE><VAR>x</VAR>.<VAR>foobar</VAR></CODE>.
  150. </DL>
  151. <DL><DT><B>globals</B> () -- built-in function<DD>
  152. Return a dictionary representing the current global symbol table.
  153. This is always the dictionary of the current module (inside a
  154. function or method, this is the module where it is defined, not the
  155. module from which it is called).
  156. </DL>
  157. <DL><DT><B>hasattr</B> (<VAR>object</VAR>, <VAR>name</VAR>) -- built-in function<DD>
  158. The arguments are an object and a string.  The result is 1 if the
  159. string is the name of one of the object's attributes, 0 if not.
  160. (This is implemented by calling <CODE>getattr(object, name)</CODE> and
  161. seeing whether it raises an exception or not.)
  162. </DL>
  163. <DL><DT><B>hash</B> (<VAR>object</VAR>) -- built-in function<DD>
  164. Return the hash value of the object (if it has one).  Hash values
  165. are 32-bit integers.  They are used to quickly compare dictionary
  166. keys during a dictionary lookup.  Numeric values that compare equal
  167. have the same hash value (even if they are of different types, e.g.
  168. 1 and 1.0).
  169. </DL>
  170. <DL><DT><B>hex</B> (<VAR>x</VAR>) -- built-in function<DD>
  171. Convert an integer number (of any size) to a hexadecimal string.
  172. The result is a valid Python expression.
  173. </DL>
  174. <DL><DT><B>id</B> (<VAR>object</VAR>) -- built-in function<DD>
  175. Return the `identity' of an object.  This is an integer which is
  176. guaranteed to be unique and constant for this object during its
  177. lifetime.  (Two objects whose lifetimes are disjunct may have the
  178. same id() value.)  (Implementation note: this is the address of the
  179. object.)
  180. </DL>
  181. <DL><DT><B>input</B> ([<VAR>prompt</VAR>]) -- built-in function<DD>
  182. Almost equivalent to <CODE>eval(raw_input(<VAR>prompt</VAR>))</CODE>.  Like
  183. <CODE>raw_input()</CODE>, the <VAR>prompt</VAR> argument is optional.  The difference
  184. is that a long input expression may be broken over multiple lines using
  185. the backslash convention.
  186. </DL>
  187. <DL><DT><B>int</B> (<VAR>x</VAR>) -- built-in function<DD>
  188. Convert a number to a plain integer.  The argument may be a plain or
  189. long integer or a floating point number.  Conversion of floating
  190. point numbers to integers is defined by the C semantics; normally
  191. the conversion truncates towards zero.<A NAME="footnoteref2" HREF="#footnotetext2">(2)</A>
  192. </DL>
  193. <DL><DT><B>len</B> (<VAR>s</VAR>) -- built-in function<DD>
  194. Return the length (the number of items) of an object.  The argument
  195. may be a sequence (string, tuple or list) or a mapping (dictionary).
  196. </DL>
  197. <DL><DT><B>locals</B> () -- built-in function<DD>
  198. Return a dictionary representing the current local symbol table.
  199. Inside a function, modifying this dictionary does not always have the
  200. desired effect.
  201. </DL>
  202. <DL><DT><B>long</B> (<VAR>x</VAR>) -- built-in function<DD>
  203. Convert a number to a long integer.  The argument may be a plain or
  204. long integer or a floating point number.
  205. </DL>
  206. <DL><DT><B>map</B> (<VAR>function</VAR>, <VAR>list</VAR>, ...) -- built-in function<DD>
  207. Apply <VAR>function</VAR> to every item of <VAR>list</VAR> and return a list
  208. of the results.  If additional <VAR>list</VAR> arguments are passed, 
  209. <VAR>function</VAR> must take that many arguments and is applied to
  210. the items of all lists in parallel; if a list is shorter than another
  211. it is assumed to be extended with <CODE>None</CODE> items.  If
  212. <VAR>function</VAR> is <CODE>None</CODE>, the identity function is assumed; if
  213. there are multiple list arguments, <CODE>map</CODE> returns a list
  214. consisting of tuples containing the corresponding items from all lists
  215. (i.e. a kind of transpose operation).  The <VAR>list</VAR> arguments may be
  216. any kind of sequence; the result is always a list.
  217. </DL>
  218. <DL><DT><B>max</B> (<VAR>s</VAR>) -- built-in function<DD>
  219. Return the largest item of a non-empty sequence (string, tuple or
  220. list).
  221. </DL>
  222. <DL><DT><B>min</B> (<VAR>s</VAR>) -- built-in function<DD>
  223. Return the smallest item of a non-empty sequence (string, tuple or
  224. list).
  225. </DL>
  226. <DL><DT><B>oct</B> (<VAR>x</VAR>) -- built-in function<DD>
  227. Convert an integer number (of any size) to an octal string.  The
  228. result is a valid Python expression.
  229. </DL>
  230. <DL><DT><B>open</B> (<VAR>filename</VAR>[, <VAR>mode</VAR>[, <VAR>bufsize</VAR>]]) -- built-in function<DD>
  231. Return a new file object (described earlier under Built-in Types).
  232. The first two arguments are the same as for <CODE>stdio</CODE>'s
  233. <CODE>fopen()</CODE>: <VAR>filename</VAR> is the file name to be opened,
  234. <VAR>mode</VAR> indicates how the file is to be opened: <CODE>'r'</CODE> for
  235. reading, <CODE>'w'</CODE> for writing (truncating an existing file), and
  236. <CODE>'a'</CODE> opens it for appending (which on <I>some</I> UNIX
  237. systems means that <I>all</I> writes append to the end of the file,
  238. regardless of the current seek position).
  239. Modes <CODE>'r+'</CODE>, <CODE>'w+'</CODE> and
  240. <CODE>'a+'</CODE> open the file for updating, provided the underlying
  241. <CODE>stdio</CODE> library understands this.  On systems that differentiate
  242. between binary and text files, <CODE>'b'</CODE> appended to the mode opens
  243. the file in binary mode.  If the file cannot be opened, <CODE>IOError</CODE>
  244. is raised.
  245. If <VAR>mode</VAR> is omitted, it defaults to <CODE>'r'</CODE>.
  246. The optional <VAR>bufsize</VAR> argument specifies the file's desired
  247. buffer size: 0 means unbuffered, 1 means line buffered, any other
  248. positive value means use a buffer of (approximately) that size.  A
  249. negative <VAR>bufsize</VAR> means to use the system default, which is
  250. usually line buffered for for tty devices and fully buffered for other
  251. files.<A NAME="footnoteref3" HREF="#footnotetext3">(3)</A>
  252. </DL>
  253. <DL><DT><B>ord</B> (<VAR>c</VAR>) -- built-in function<DD>
  254. Return the ASCII value of a string of one character.  E.g.,
  255. <CODE>ord('a')</CODE> returns the integer <CODE>97</CODE>.  This is the inverse of
  256. <CODE>chr()</CODE>.
  257. </DL>
  258. <DL><DT><B>pow</B> (<VAR>x</VAR>, <VAR>y</VAR>[, <VAR>z</VAR>]) -- built-in function<DD>
  259. Return <VAR>x</VAR> to the power <VAR>y</VAR>; if <VAR>z</VAR> is present, return
  260. <VAR>x</VAR> to the power <VAR>y</VAR>, modulo <VAR>z</VAR> (computed more
  261. efficiently than <CODE>pow(<VAR>x</VAR>, <VAR>y</VAR>) % <VAR>z</VAR></CODE>).
  262. The arguments must have
  263. numeric types.  With mixed operand types, the rules for binary
  264. arithmetic operators apply.  The effective operand type is also the
  265. type of the result; if the result is not expressible in this type, the
  266. function raises an exception; e.g., <CODE>pow(2, -1)</CODE> or <CODE>pow(2,
  267. 35000)</CODE> is not allowed.
  268. </DL>
  269. <DL><DT><B>range</B> ([<VAR>start</VAR>,] <VAR>end</VAR>[, <VAR>step</VAR>]) -- built-in function<DD>
  270. This is a versatile function to create lists containing arithmetic
  271. progressions.  It is most often used in <CODE>for</CODE> loops.  The
  272. arguments must be plain integers.  If the <VAR>step</VAR> argument is
  273. omitted, it defaults to <CODE>1</CODE>.  If the <VAR>start</VAR> argument is
  274. omitted, it defaults to <CODE>0</CODE>.  The full form returns a list of
  275. plain integers <CODE>[<VAR>start</VAR>, <VAR>start</VAR> + <VAR>step</VAR>,
  276. <VAR>start</VAR> + 2 * <VAR>step</VAR>, ...]</CODE>.  If <VAR>step</VAR> is positive,
  277. the last element is the largest <CODE><VAR>start</VAR> + <VAR>i</VAR> *
  278. <VAR>step</VAR></CODE> less than <VAR>end</VAR>; if <VAR>step</VAR> is negative, the last
  279. element is the largest <CODE><VAR>start</VAR> + <VAR>i</VAR> * <VAR>step</VAR></CODE>
  280. greater than <VAR>end</VAR>.  <VAR>step</VAR> must not be zero (or else an
  281. exception is raised).  Example:
  282. <P>
  283. <UL COMPACT><CODE>>>> range(10)<P>
  284. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<P>
  285. >>> range(1, 11)<P>
  286. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<P>
  287. >>> range(0, 30, 5)<P>
  288. [0, 5, 10, 15, 20, 25]<P>
  289. >>> range(0, 10, 3)<P>
  290. [0, 3, 6, 9]<P>
  291. >>> range(0, -10, -1)<P>
  292. [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]<P>
  293. >>> range(0)<P>
  294. []<P>
  295. >>> range(1, 0)<P>
  296. []<P>
  297. >>> <P>
  298. </CODE></UL>
  299. </DL>
  300. <DL><DT><B>raw_input</B> ([<VAR>prompt</VAR>]) -- built-in function<DD>
  301. If the <VAR>prompt</VAR> argument is present, it is written to standard output
  302. without a trailing newline.  The function then reads a line from input,
  303. converts it to a string (stripping a trailing newline), and returns that.
  304. When EOF is read, <CODE>EOFError</CODE> is raised. Example:
  305. <P>
  306. <UL COMPACT><CODE>>>> s = raw_input('--> ')<P>
  307. --> Monty Python's Flying Circus<P>
  308. >>> s<P>
  309. "Monty Python's Flying Circus"<P>
  310. >>> <P>
  311. </CODE></UL>
  312. </DL>
  313. <DL><DT><B>reduce</B> (<VAR>function</VAR>, <VAR>list</VAR>[, <VAR>initializer</VAR>]) -- built-in function<DD>
  314. Apply the binary <VAR>function</VAR> to the items of <VAR>list</VAR> so as to
  315. reduce the list to a single value.  E.g.,
  316. <CODE>reduce(lambda x, y: x*y, <VAR>list</VAR>, 1)</CODE> returns the product of
  317. the elements of <VAR>list</VAR>.  The optional <VAR>initializer</VAR> can be
  318. thought of as being prepended to <VAR>list</VAR> so as to allow reduction
  319. of an empty <VAR>list</VAR>.  The <VAR>list</VAR> arguments may be any kind of
  320. sequence.
  321. </DL>
  322. <DL><DT><B>reload</B> (<VAR>module</VAR>) -- built-in function<DD>
  323. Re-parse and re-initialize an already imported <VAR>module</VAR>.  The
  324. argument must be a module object, so it must have been successfully
  325. imported before.  This is useful if you have edited the module source
  326. file using an external editor and want to try out the new version
  327. without leaving the Python interpreter.  The return value is the
  328. module object (i.e. the same as the <VAR>module</VAR> argument).
  329. <P>
  330. There are a number of caveats:
  331. <P>
  332. If a module is syntactically correct but its initialization fails, the
  333. first <CODE>import</CODE> statement for it does not bind its name locally,
  334. but does store a (partially initialized) module object in
  335. <CODE>sys.modules</CODE>.  To reload the module you must first
  336. <CODE>import</CODE> it again (this will bind the name to the partially
  337. initialized module object) before you can <CODE>reload()</CODE> it.
  338. <P>
  339. When a module is reloaded, its dictionary (containing the module's
  340. global variables) is retained.  Redefinitions of names will override
  341. the old definitions, so this is generally not a problem.  If the new
  342. version of a module does not define a name that was defined by the old
  343. version, the old definition remains.  This feature can be used to the
  344. module's advantage if it maintains a global table or cache of objects
  345. --- with a <CODE>try</CODE> statement it can test for the table's presence
  346. and skip its initialization if desired.
  347. <P>
  348. It is legal though generally not very useful to reload built-in or
  349. dynamically loaded modules, except for <CODE>sys</CODE>, <CODE>__main__</CODE> and
  350. <CODE>__builtin__</CODE>.  In certain cases, however, extension modules are
  351. not designed to be initialized more than once, and may fail in
  352. arbitrary ways when reloaded.
  353. <P>
  354. If a module imports objects from another module using <CODE>from</CODE>
  355. ... <CODE>import</CODE> ..., calling <CODE>reload()</CODE> for the other
  356. module does not redefine the objects imported from it --- one way
  357. around this is to re-execute the <CODE>from</CODE> statement, another is to
  358. use <CODE>import</CODE> and qualified names (<VAR>module</VAR>.<VAR>name</VAR>)
  359. instead.
  360. <P>
  361. If a module instantiates instances of a class, reloading the module
  362. that defines the class does not affect the method definitions of the
  363. instances --- they continue to use the old class definition.  The same
  364. is true for derived classes.
  365. </DL>
  366. <DL><DT><B>repr</B> (<VAR>object</VAR>) -- built-in function<DD>
  367. Return a string containing a printable representation of an object.
  368. This is the same value yielded by conversions (reverse quotes).
  369. It is sometimes useful to be able to access this operation as an
  370. ordinary function.  For many types, this function makes an attempt
  371. to return a string that would yield an object with the same value
  372. when passed to <CODE>eval()</CODE>.
  373. </DL>
  374. <DL><DT><B>round</B> (<VAR>x</VAR>, <VAR>n</VAR>) -- built-in function<DD>
  375. Return the floating point value <VAR>x</VAR> rounded to <VAR>n</VAR> digits
  376. after the decimal point.  If <VAR>n</VAR> is omitted, it defaults to zero.
  377. The result is a floating point number.  Values are rounded to the
  378. closest multiple of 10 to the power minus <VAR>n</VAR>; if two multiples
  379. are equally close, rounding is done away from 0 (so e.g.
  380. <CODE>round(0.5)</CODE> is <CODE>1.0</CODE> and <CODE>round(-0.5)</CODE> is <CODE>-1.0</CODE>).
  381. </DL>
  382. <DL><DT><B>setattr</B> (<VAR>object</VAR>, <VAR>name</VAR>, <VAR>value</VAR>) -- built-in function<DD>
  383. This is the counterpart of <CODE>getattr</CODE>.  The arguments are an
  384. object, a string and an arbitrary value.  The string must be the name
  385. of one of the object's attributes.  The function assigns the value to
  386. the attribute, provided the object allows it.  For example,
  387. <CODE>setattr(<VAR>x</VAR>, '<VAR>foobar</VAR>', 123)</CODE> is equivalent to
  388. <CODE><VAR>x</VAR>.<VAR>foobar</VAR> = 123</CODE>.
  389. </DL>
  390. <DL><DT><B>str</B> (<VAR>object</VAR>) -- built-in function<DD>
  391. Return a string containing a nicely printable representation of an
  392. object.  For strings, this returns the string itself.  The difference
  393. with <CODE>repr(<VAR>object</VAR>)</CODE> is that <CODE>str(<VAR>object</VAR>)</CODE> does not
  394. always attempt to return a string that is acceptable to <CODE>eval()</CODE>;
  395. its goal is to return a printable string.
  396. </DL>
  397. <DL><DT><B>tuple</B> (<VAR>sequence</VAR>) -- built-in function<DD>
  398. Return a tuple whose items are the same and in the same order as
  399. <VAR>sequence</VAR>'s items.  If <VAR>sequence</VAR> is alread a tuple, it
  400. is returned unchanged.  For instance, <CODE>tuple('abc')</CODE> returns
  401. returns <CODE>('a', 'b', 'c')</CODE> and <CODE>tuple([1, 2, 3])</CODE> returns
  402. <CODE>(1, 2, 3)</CODE>.
  403. </DL>
  404. <DL><DT><B>type</B> (<VAR>object</VAR>) -- built-in function<DD>
  405. Return the type of an <VAR>object</VAR>.  The return value is a type
  406. object.  The standard module <CODE>types</CODE> defines names for all
  407. built-in types.
  408. For instance:
  409. <P>
  410. <UL COMPACT><CODE>>>> import types<P>
  411. >>> if type(x) == types.StringType: print "It's a string"<P>
  412. </CODE></UL>
  413. </DL>
  414. <DL><DT><B>vars</B> ([<VAR>object</VAR>]) -- built-in function<DD>
  415. Without arguments, return a dictionary corresponding to the current
  416. local symbol table.  With a module, class or class instance object as
  417. argument (or anything else that has a <CODE>__dict__</CODE> attribute),
  418. returns a dictionary corresponding to the object's symbol table.
  419. The returned dictionary should not be modified: the effects on the
  420. corresponding symbol table are undefined.<A NAME="footnoteref4" HREF="#footnotetext4">(4)</A>
  421. </DL>
  422. <DL><DT><B>xrange</B> ([<VAR>start</VAR>,] <VAR>end</VAR>[, <VAR>step</VAR>]) -- built-in function<DD>
  423. This function is very similar to <CODE>range()</CODE>, but returns an
  424. ``xrange object'' instead of a list.  This is an opaque sequence type
  425. which yields the same values as the corresponding list, without
  426. actually storing them all simultaneously.  The advantage of
  427. <CODE>xrange()</CODE> over <CODE>range()</CODE> is minimal (since <CODE>xrange()</CODE>
  428. still has to create the values when asked for them) except when a very
  429. large range is used on a memory-starved machine (e.g. MS-DOS) or when all
  430. of the range's elements are never used (e.g. when the loop is usually
  431. terminated with <CODE>break</CODE>).
  432. </DL>
  433. <H2>---------- Footnotes ----------</H2>
  434. <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
  435. It is used relatively rarely
  436. so does not warrant being made into a statement.<P>
  437. <A NAME="footnotetext2" HREF="#footnoteref2">(2)</A>
  438. This is ugly --- the
  439. language definition should require truncation towards zero.<P>
  440. <A NAME="footnotetext3" HREF="#footnoteref3">(3)</A>
  441. Specifying a buffer size currently has no effect on systems
  442. that don't have <CODE>setvbuf()</CODE>.  The interface to specify the buffer
  443. size is not done using a method that calls <CODE>setvbuf()</CODE>, because
  444. that may dump core when called after any I/O has been performed, and
  445. there's no reliable way to determine whether this is the case.<P>
  446. <A NAME="footnotetext4" HREF="#footnoteref4">(4)</A>
  447. In the current implementation, local variable bindings
  448. cannot normally be affected this way, but variables retrieved from
  449. other scopes (e.g. modules) can be.  This may change.<P>
  450.