3.2 Why am I getting strange results with simple arithmetic operations? . . . . . . . . . . . . . . . . . 37 3.3 Why are floating-point calculations so inaccurate? . . . . . . . . . . . . . . . . . . . . . . . . . . 37 3.4 Why are Python strings immutable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.5 Why must ‘self’ be used explicitly in method definitions and calls? . . . . . . . . . . . . . . . . . . 38 3.6 Why can’t I use an assignment in an expression? . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.7 Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.8 Why is join() a string method instead of a list or tuple method? . . . . . . . . . . . . . . . . . . . 39 3.9 How fast are exceptions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.10 Why isn’t there a switch or case statement in Python? . . . . . . . . . . . . . . . . . . . . . . . . 40 3.11 Can’t you emulate threads in the interpreter instead of relying on an OS-specific thread implementation? 41 3.12 Why can’t lambda expressions contain statements? . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.13 Can Python be compiled to machine code, C or some other language? . . . . . . . . . . . . . . . 41 3.14 How does Python manage memory? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.15 Why doesn’t CPython use a more traditional garbage collection scheme? . . . . . . . . . . . . . . 42 3.16 Why isn’t all memory freed when CPython exits? . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.17 Why are there separate tuple and list data types? . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.18 How are lists implemented in CPython? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.19 How are dictionaries implemented in CPython? . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.20 Why must dictionary keys be immutable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.21 Why doesn’t list.sort() return the sorted list? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.22 How do you specify and enforce an interface spec in Python? . . . . . . . . . . . . . . . . . . . . 44 3.23 Why is there no goto? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.24 Why can’t raw strings (r-strings) end with a backslash? . . . . . . . . . . . . . . . . . . . . . . . 45 3.25 Why doesn’t Python have a “with” statement for attribute assignments? . . . . . . . . . . . . . . . 46 3.26 Why are colons required for the if/while/def/class statements? . . . . . . . . . . . . . . . . . . . . 47 3.27 Why does Python allow commas at the end of lists and tuples? . . . . . . . . . . . . . . . . . . . 47 4.1 General Library Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 4.1.1 How do I find a module or application to perform task X? . . . . . . . . . . . . . . . . . 49 4.1.2 Where is the math.py (socket.py, regex.py, etc.) source file? . . . . . . . . . . . . . . . . 49 4.1.3 How do I make a Python script executable on Unix? . . . . . . . . . . . . . . . . . . . . 49 4.1.4 Is there a curses/termcap package for Python? . . . . . . . . . . . . . . . . . . . . . . . 50 4.1.5 Is there an equivalent to C’s onexit() in Python? . . . . . . . . . . . . . . . . . . . . . . . 50 4.1.6 Why don’t my signal handlers work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4.2 Common tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4.2.1 How do I test a Python program or component? . . . . . . . . . . . . . . . . . . . . . . 50 4.2.2 How do I create documentation from doc strings? . . . . . . . . . . . . . . . . . . . . . 51 4.2.3 How do I get a single keypress at a time? . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.3 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.3.1 How do I program using threads? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.3.2 None of my threads seem to run: why? . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.3.3 How do I parcel out work among a bunch of worker threads? . . . . . . . . . . . . . . . . 52 4.3.4 What kinds of global value mutation are thread-safe? . . . . . . . . . . . . . . . . . . . . 53 4.3.5 Can’t we get rid of the Global Interpreter Lock? . . . . . . . . . . . . . . . . . . . . . . 54 4.4 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.4.1 How do I delete a file? (And other file questions…) . . . . . . . . . . . . . . . . . . . . . 54 4.4.2 How do I copy a file? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.4.3 How do I read (or write) binary data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.4.4 I can’t seem to use os.read() on a pipe created with os.popen(); why? . . . . . . . . . . . 55 4.4.5 How do I access the serial (RS232) port? . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.4.6 Why doesn’t closing sys.stdout (stdin, stderr) really close it? . . . . . . . . . . . . . . . . 55 4.5 Network/Internet Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.5.1 What WWW tools are there for Python? . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.5.2 How can I mimic CGI form submission (METHOD=POST)? . . . . . . . . . . . . . . . 56 4.5.3 What module should I use to help with generating HTML? . . . . . . . . . . . . . . . . . 57 4.5.4 How do I send mail from a Python script? . . . . . . . . . . . . . . . . . . . . . . . . . 57 4 Library and Extension FAQ 49 iii