blob: 204b51bc9edef25e659b0a52f7ffda17974392c7 [file] [log] [blame]
Guido van Rossumc8180cc1994-08-05 15:57:31 +00001 A Python Bestiary
2 Itemizing Python Objects and Nuances
3
4 $Revision$ $Date$ ken.manheimer@nist.gov
5
6 Notable lexical entities
7 ========================
8
9 Keywords
10
11 access and break class
12 continue def del elif
13 else except exec finally
14 for from global if
15 import in is lambda
16 not or pass print
17 raise return try while
18
19 String Literal Escapes
20
21 \newline Ignored (escape newline)
22 \\ Backslash (\) \e Escape (ESC) \v Vertical Tab (VT)
23 \' Single quote (') \f Formfeed (FF) \0XX char with
24 \" Double quote (") \n Linefeed (LF) octal value XX
25 \a Bell (BEL) \r Carriage Return (CR) \xXX char with
26 \b Backspace (BS) \t Horizontal Tab (TAB) hex value XX
27
28 Illegitimate Tokens (only valid in strings): @ $ ?
29
30 Operating environment (Unix)
31
32 Shell environment vars:
33
34 PYTHONPATH: dirs to be prepended to module search path; a la shell PATH
35 PYTHONSTARTUP: names path of file to be loaded on interactive startup
36
37 Internal (module sys) vars ('import sys' to refer to them):
38
39 argv - list of command and arguments passed to python script
40 builtin_module_names - list of names of modules compiled in to python
41 ( exc_* values are set only during handling of a raised exception)
42 exc_type - type of exception being handled
43 exc_value - exception's parameter ('raise's 2nd arg)
44 exc_traceback - exception's traceback obj
45 exit(N) - exit from python with status N, by raising SystemExit
46 exitfunc - function hook, to be called on exit if set
47 last_type - type of last unhandled exception
48 last_value - value of last unhandled exception
49 last_traceback - traceback of last unhandled exception
50 modules - list of modules already loaded
51 path - list of strings specifying module-load search path
52 ps1='>>>' - string specifying primary prompt, iff interactive
53 ps2='...' - string specifying secondary prompt, iff interactive
54 settrace - system trace function, used by debuggers, etc
55 setprofile - system profile function
56 stdin - standard input file object
57 stdout - standard output file object
58 stderr - standard error file object
59 tracebacklimit=1000 - maximum number of traceback levels to print
60
61
62 Objects - behaviors, special attributes, operations, statements, etc
63 ====================================================================
64
65 General object aspects
66
67 Boolean values and operators
68
69 False values: None, numeric zeros, empty sequences and mappings
70 True values: all other values
71
72 not X: if X is false then 1, else 0
73 ( 'or', 'and' evaluate second arg only if necessary to determine outcome)
74 X or Y: if X is false then Y, else X
75 X and Y: if X is false then X, else Y
76
77 Special methods for any type (s: self)
78
79 id(obj) unique identifier for object (currently, its address)
80 __init__(s, args) object instantiation - see Classes, below
81 __del__(s) object demise
82 __repr__(s) repr() and `...` conversions
83 __str__(s) str() and 'print' statement
84 __cmp__(s) implements <, ==, >, <=, <>, !=, >=, is [not]
85 __hash__(s) hash() and dictionary operations
86
87 Special informative state attributes for some types:
88
89 X.__dict__ dict used to store object's writeable attributes
90 X.__methods__ list of X's methods; on many built-in types.
91 X.__members__ lists of X's data attributes
92 X.__class__ class to which X belongs
93 X.__bases__ tuple of X base classes
94
95 General Name Space behavior and binding
96
97 Name space search order: local, global, builtin
98
99 "global" name space = file = module
100 "local" name space = function or method
101
102 Code Block scopes (ns = name space, cb = containing block)
103
104 ( global ns generally is containing module, unless overriden by args)
105
106 Code block type Local ns Notes
107 --------------- -------- -----
108 Module same as global ns
109 Script same as global ns global ns is __main__
110 Interactive cmd same as global ns global ns is __main__
111 Class def new ns
112 Function body new ns
113 'exec' string local ns of cb (or args)
114 'eval' string local ns of caller (or args)
115 'execfile' file local ns of caller (or args)
116 'input' expr local ns of caller
117
118 Binding operations and exceptions:
119
120 ~ SyntaxError: on attempt to bind to literals or other non-bindables
121 ~ NameError: on attempt to evaluate unbound atom
122
123 ( for object attribute functions, obj: object, nm: string, val: any value)
124 getattr(obj, nm) get value of obj.nm
125 hasattr(obj, nm) true if obj has nm attribute
126 setattr(obj, nm, val) set obj.nm to val
127
128 assignment statement: targ1, targ2, ,,, = obj1, obj2, ,,,
129 deletion statement: del obj1, obj2, ...
130 for loop target identifiers, 'except' clauses (see Statements, below)
131 formal params (see Callables, below)
132 import statement (see Modules objects, below)
133 class and func defs (see Callables, below)
134
135 Name space linkage
136
137 global statement: global var, ... # Interpret 'var's as globals
138 X access statement: access ... # control inst and class vars access
139
140 @ Built-in Exceptions
141
142 AttributeError On attribute reference or assignment failure
143 EOFError Immediate end-of-file hit by input() or raw_input()
144 IOError I/O-related I/O operation failure
145 ImportError On failure of `import' to find module or name
146 IndexError On out-of-range sequence subscript
147 KeyError On reference to a non-existent mapping (dict) key
148 KeyboardInterrupt On user entry of the interrupt key (often `Control-C')
149 MemoryError On recoverable memory exhaustion
150 NameError On failure to find a local or global (unqualified) name
151 OverflowError On excessively large arithmetic operation
152 RuntimeError Obsolete catch-all; define a suitable error instead
153 SyntaxError On parser encountering a syntax error
154 SystemError On non-fatal interpreter error - bug - report it
155 SystemExit On `sys.exit()'
156 TypeError On passing inappropriate type to built-in op or func
157 ValueError On arg error not covered by TypeError or more precise
158 ZeroDivisionError On division or modulo operation with 0 as 2nd arg
159
160 **** Numbers ****
161
162 @ Integers: 'C' long, >= 32 bits precision; OverflowError if bounds exceeded
163 @ Long Integers: unlimited precision - '2147483648L'
164 @ Floating point: machine double-precision floating point - '2147483648.0'
165
166 Numeric operations vs special methods (s = self, o = other)
167
168 s+o = __add__(s,o) s-o = __sub__(s,o)
169 s*o = __mul__(s,o) s/o = __div__(s,o)
170 s%o = __mod__(s,o) divmod(s,o) = __divmod__(s,o)
171 pow(s,o) = __pow__(s,o)
172 s&o = __and__(s,o)
173 s^o = __xor__(s,o) s|o = __xor__(s,o)
174 s<<o = __lshift__(s,o) s>>o = __rshift__(s,o)
175 nonzero(s) = __nonzero__(s) coerce(s,o) = __coerce__(s,o)
176 -s = __neg__(s) +s = __pos__(s)
177 abs(s) = __abs__(s) ~s = __invert__(s) (bitwise)
178 int(s) = __int__(s) long(s) = __long__(s)
179 float(s) = __float__(s)
180 oct(s) = __oct__(s) hex(s) = __hex__(s)
181
182 Numeric functions:
183
184 range(start=0,end,step=1) create arithmetic progression list
185 round(x, n=0) round floating point x to n decimal places
186 xrange(start=0,end,step=1) create virtual arithmetic progressions tuple
187
188 Numeric exceptions:
189
190 ~ TypeError: raised on application of arithemetic opertion to non-number
191 ~ OverflowError: numeric bounds exceeded
192 ~ ZeroDivisionError: raised when zero second argument of div or modulo op
193
194 **** Collections - Sequences and Mappings ****
195
196 Collections general operations vs methods (s: self, i: index or key)
197
198 len(s) = __len__(s) length of object, >= 0. Length 0 == false
199 s[i] = __getitem__(s,i) Element at index/key i, origin 0
200
201 Sequences
202
203 @ String (immutable sequence): 'string contents'
204
205 'this is a string'
206 "and so is this"
207 ''' and this begins a 'multi-line' string with embedded quotes...
208 and this is the end of that string.'''
209
210 `expr` = __repr__(expr), converts arbitrary expr to string
211 chr(int) string of ascii letter at ordinate in (0 <= int < 256)
212 ord(string) ascii ordinate of string (string must be length 1)
213 string % arg format operator, a la C sprintf
214 arg for single directive can be any (suitable) type
215 arg for multiple directives must be tuple or dict
216 dict (mapping) arg uses parenthesized directives that are keys into it
217 supports %, c, s, i, d, u, o, x, X, e, E, f, g, G directives
218 * can be width and precision; directs use of corresponding (int) args
219 * can not be used with dict args
220 flag characters -, +, blank, #, and 0 understood.
221 %s conversion takes any python object, converts using `str()'
222 ANSI directives %p and %n not supported
223 %s conversions do *not* take \000 as end of string
224
225 @ Tuples (immutable sequence): (oneelem, another, etc)
226
227 parens may be left off all but empty tuples
228 singletons represented by affixing a comma to an expr
229 empty tuple represented using empty parens
230
231 @ Lists (mutable sequence): [oneelem, another, etc]
232
233 assignment - must be 1-1 map of items in target and object sequences
234 deletion - similar rules as for assignment
235
236 List special methods: see mutable sequence ops vs methods, below
237
238 Sequences general ops vs methods (s: self, i,j: indices, v: val)
239
240 All collections general methods, plus:
241 s[i:j] __getslice__(s,i,j), all s[k] s.t. i <= k < j
242 min(s) smallest item of s
243 max(s) largest item of s
244 v [not] in s 1 if v [not] equal to an item in s, else 0
245 s + seq concatenation of s and seq
246 s * num num copies of s concatenated, also, `num * s'
247
248 Immutable sequence ops vs methods (s: self, i,j: indices, v: val)
249
250 All collections and sequences general methods, plus:
251 s[i:j] __getslice__(s,i,j), all s[k] s.t. i <= k < j
252 ( For s[i:j], len(self) is intrinsically added to i, j < 0)
253 ( Complex elems of immutable seqs may be mutable, see dictionaries, below)
254
255 Mutable sequence ops vs methods (s: self, i,j: indices, v: val)
256
257 All sequences' general methods, plus:
258 ( for non-slice refs, i < 0 intrinsically has len(s) added)
259 ( For slice refs, len(s) *is not* intrinsically added to i, j < 0)
260 ( for assignment/deletion, index refs must point to existing items
261 s[i]=v = __setitem__(s,i,v)
262 del s[i] = __delitem__(s,i)
263 s[i:j] = __getslice__(s,i,j)
264 s[i:j]=seq = __setslice__(s,i,j,seq)
265 del s[i:j] = __delslice__(s,i,j) == s[i:j] = []
266 s.append(seq) == `s[len(seq):len(seq)] = seq' (but faster)
267 s.count(v) number of i's for which `s[i] == v'
268 s.index(v) first i such that `s[i] == v', or IndexError if none
269 s.insert(i, v) == `s[i:i] = [v]'
270 s.remove(v) == `del s[s.index(v)]', or IndexError if v not in s
271 s.reverse() reverse the items of s in place
272 s.sort() permute s items so s[i] <= s[j], for i < j
273
274 Mappings
275
276 @ Dictionaries: {key1: val1, key2: val2, ...}
277
278 built-in types as keys must be unalterable: obj & all contents immutable
279 User-defined classes as keys must have __hash__() and __cmp__() methods
280 ~ TypeError is raised if key not acceptable
281 ~ KeyError is raised if reference made using non-existent key
282 key types may vary (contrary to ref man)
283
284 Dictionaries ops vs methods (s: self, k: key, v: val)
285
286 all collections general ops, plus:
287 hash(s) = __hash__(s) - hash value for dictionary references
288 s[k]=v = __setitem__(s,k,v)
289 del s[k] = __delitem__(s,k)
290 s.items() = a list copy of s's (key, item) pairs
291 s.keys() = a list copy of s's keys
292 s.values() = a list copy of s's values
293 s.has_keys(k) = 1 if s has key k, else 0
294 ( s.items, .keys, and .values yield random but mutually consistent order)
295
296 **** Callables ****
297
298 @ User defined functions: 'def name (param-list): suite'
299
300 suite is not evaluated at statement execution, but at function invocation
301 function parameters, comma separated on param-list:
302 func_code: special attr, code object representing compiled function body
303 func_globals: special attr, ref to global dict of funcs definition module
304 func(arg-list) invocation
305
306 @ User defined methods: like functions, with extra implicit arg
307
308 Same as functions, but passed class instance as additional first argument
309 im_self: special attr, method's class instance object
310 im_func: special attr, function object
311 mthd(args) invocation, same as mthd.im_func(mthd.im_self, args)
312
313 @ Classes: 'class name[ (inheritance)]: suite'
314
315 inheritance list is evaluated, if any, to identify base classes for name
316 suite is executed in new local name space, which goes to the class object
317 class name is bound in encompassing local name space
318 container for dictionary containing class's ns dictionary
319 __dict__: ro attr, class ns as dictionary object
320 __bases__: ro attr, class' base classes in tuple
321 __init__(self, args..): implements object instantiation
322 __del__(self): implements impending object deletion
323
324 @ Class instances
325
326 __dict__: ro attr, class' attribute dictionary
327 __class__: ro attr, instance's class object
328
329 Callables special method vs ops (f: function)
330
331 apply(f, args-tuple) call f with args-tuple as arg list
332 compile(str, flnm, kind) compile string into exectuable code object
333 eval(str, glbls=, lcls=) evaluate string as expression (cond_list)
334 filter(f, seq) => seq of seq elems for which f is true
335 map(f, lst, [lst2, ...]) => list of f applied to succesive lsts elems
336 reduce(f, lst, initlzr) => value of f applied to elems and cume result
337
338 @ **** Null object: `None' ****
339
340 @ **** Type objects, print as: <type 'int'> ****
341 ( '<...>' form is only for printing - cannot be entered that way,
342
343 accessed via built-in func 'type()'
344 ( equal only when identical (same id()),
345 so can't just use the string name, must use object with the same str val)
346
347 @ **** Modules ****
348
349 functions and methods in a module share module's ("global") namespace
350 function uses "global" statement to instantiate var in global context
351 Modules use "import" to incorp other module's names - see Name Spaces
352
353 Special attrs, methods, and operations
354
355 __dict__: attr, module's ns as dictionary; can modify vals but not sruct
356 __name__: ro attr, module's name as string
357 import Instantiate module or module components within another
358 reload(amod) Reread an imported module
359
360 @ **** Files ****
361
362 wrapper around a C stdio file pointer
363 sys.stdin, sys.stdout, sys.stderr are standard io-stream file objects
364
365 File operations:
366
367 open(nm, mode='r', bufsize=sysdef) return new file object
368 close() A closed file cannot be read or written anymore.
369 flush() Flush the internal buffer, like stdio's `fflush()'.
370 isatty() 1 if file connected to a tty(-like) device, else 0.
371 read(SIZE) Read up to SIZE bytes frm file, less on EOF or no data
372 readline() Read one entire line from the file.
373 readlines() `readline()' til EOF and return list of lines read.
374 seek(OFFSET,WHENCE) Set file's current position, like stdio's `fseek()'
375 tell() Return file's current position, like stdio's `ftell()'.
376 write(STR) Write a string to the file. There is no return value.
377
378 File functions:
379
380 input(prompt='') like raw input but accept '\' line continuations
381 print exp, exp2, ... Write values to stdout
382 raw_input(prompt='') prompt then read single line of input
383
384 **** Internal types ****
385
386 @ Code objects - represent exectuable code - function obj sans context
387 @ Frame objects - represent executable frames - may occur in traceback objs
388 @ Traceback objects - stack trace of an exception
389
390 Control statements
391 ==================
392
393 * Calls and Evaluation *
394
395 ( See also Callables, in Objects section above, for ops and methods.)
396
397 exec: exec expr [ in expr2 [, expr3]] # Dynamic execution of python code
398 return: return [expr] # Leave current func call with expr value
399
400 * Conditionals and Loops *
401 ( See also Boolean values, above)
402
403 break: break # Terminate nearest enclosing loop
404 continue: continue # Continue next cycle of enclosing loop
405 if: if cond: suite [\n elif cond: suite \n ...] [\n else: suite]
406 for: for targs in conds: suite [ \n else: suite ]
407 while: while cond: suite [ \n else : suite ]
408
409 * Exceptions *
410
411 raise: raise expr [, expr2] # Raise exception expr, passing expr2 if any
412 try... # Handle exceptions:
413 try: suite [\n except [cond [, targ]]: suite \n ... \n] [else: suite]
414 try: suite \n finally: suite # Specify a 'cleanup' handler
415 ( two 'try' forms cannot be mixed together)
416
417 System modules
418 ==============
419 @ * Built-ins *
420
421 sys Interpreter state (see Operating Environment, above)
422 __builtin__ Access to all built-in python identifiers
423 __main__ Scope of the interpreters main program, script or stdin
424 array Obj efficiently representing arrays of basic values
425 math Math functions of C standard
426 time Time-related functions
427 regex Regular expression matching operations
428 marshal Read and write some python values in binary format
429 strop Miscellaneous string operations
430 struct Convert between python values and C structs
431
432 @ * Standard *
433
434 getopt Parse cmd line args in sys.argv. A la UNIX 'getopt'.
435 os Portable interface to OS dependent functionality
436 pdb text-oriented debugger
437 rand Pseudo-random generator, like C rand()
438 regsub Functions useful for working with regular expressions
439 string Useful string and characters functions and exceptions
440 wdb window-oriented debugger
441 whrandom Wichmann-Hill pseudo-random number generator
442
443 @ * Miscellaneous *
444
445 dis Python disassembler
446 glob Filename globbing (a la unix shell)
447 grep File string search
448 posixpath Common operations on POSIX pathnames
449 profile Python code profiler
450 repr `...` repr operator with presentation constraints
451 string All of builtin 'strop', plus
452 tb Traceback browser and printer
453 tempfile Produce a temporary-file name
454 util Miscellaneous functions that don't belong elsewhere
455
456 @ * Unix *
457
458 dbm Dict/file-like interface to Unix ndbm database library
459 grp Interface to Unix group database
460 posix Standardized UNIX OS functionality - see 'os', above
461 posixpath POSIX pathname functions - see 'os', above
462 pwd Access to the Unix password database
463 select Access to Unix select multiplex file synchronization
464 socket Access to BSD socket interface
465 thread Low-level primitives for working with process threads
466
467 @ * Multimedia *
468
469 audioop Useful operations on sound fragments
470 imageop Useful operations on images
471 jpeg Access to jpeg image compressor and decompressor
472 rgbimg Access SGI imglib image files
473
474 @ * Cryptographic Extensions *
475
476 md5 Interface to RSA's MD5 message digest algorithm
477 mpz Interface to int part of GNU multiple precision library
478 rotor Implementation of a rotor-based encryption algorithm
479
480 @ * Stdwin * Standard Window System
481
482 stdwin Standard Window System interface
483 stdwinevents Stdwin event, command, and selection constants
484 rect Rectangle manipulation operations
485
486 @ * SGI IRIX * (4 & 5)
487
488 al SGI audio facilities
489 AL al constants
490 fl Interface to FORMS library
491 FL fl constants
492 flp Functions for form designer
493 fm Access to font manager library
494 gl Access to graphics library
495 GL Constants for gl
496 DEVICE More constants for gl
497 imgfile Imglib image file interface
498
499 @ * Suns *
500
501 sunaudiodev Access to sun audio interface
502
503 @ * Contrib *
504
505 syslog
506
507 metalbase
508
509 Idioms and hints
510 ================
511
512 Invoke main if running as script: if __name__ == '__main__': main()
513
514 General object-type miscellany
515 What type is someobj; eg, a list: if type(someobj) == type([]):
516 Convert number to hex or octal string: hex(122) => '0x7a'
517 Convert string to number: eval('0x7a') => 122
518
519 Sequence Slice conceptualization:
520 +---+---+---+---+---+ Indices point ~between~ chars:
521 | s | t | u | f | f | - first char's left edge = 0,
522 +---+---+---+---+---+ - nth char's right edge = n.
523 0 1 2 3 4 5 (Note that -0 == 0, so it does
524 -5 -4 -3 -2 -1 not count from right.)
525
526 Sequences miscellany:
527 Create merge of lists: map(None, lst1, lst2, ...)
528 It's faster to list.append(elem) than to list[len(list):] = [elem]
529 Copy structure of seq a to b: b = a[:]
530
531 Comma (tuple vs expression grouping) nuances:
532 to specify a tuple of one element: ('element',)
533 to specify a tuple containing 1 tuple: (('contained', 'tuple'),)
534 "TypeError: call of non-function" often means a list missing a comma
535
536 Namespace and object surveillance:
537 get dictful object keys/key-vals: dir(), vars()
538 Current context globals: eval(__name__ + '.__dict__')
539 Current context locals: vars()
540 Methods supported by X (some objs): X.__methods__
541 X's data attributes (some objs): X.__members__