2.2.11 How do you make a higher order function in Python? . . . . . . . . . . . . . . . . . . . . 15 2.2.12 How do I copy an object in Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.2.13 How can I find the methods or attributes of an object? . . . . . . . . . . . . . . . . . . . 16 2.2.14 How can my code discover the name of an object? . . . . . . . . . . . . . . . . . . . . . 16 2.2.15 What’s up with the comma operator’s precedence? . . . . . . . . . . . . . . . . . . . . . 17 2.2.16 Is there an equivalent of C’s “?:” ternary operator? . . . . . . . . . . . . . . . . . . . . . 17 2.2.17 Is it possible to write obfuscated one-liners in Python? . . . . . . . . . . . . . . . . . . . 17 2.2.18 What does the slash(/) in the parameter list of a function mean? . . . . . . . . . . . . . . 18 2.3 Numbers and strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.3.1 How do I specify hexadecimal and octal integers? . . . . . . . . . . . . . . . . . . . . . 18 2.3.2 Why does -22 // 10 return -3? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3.3 How do I convert a string to a number? . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3.4 How do I convert a number to a string? . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3.5 How do I modify a string in place? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3.6 How do I use strings to call functions/methods? . . . . . . . . . . . . . . . . . . . . . . . 20 2.3.7 Is there an equivalent to Perl’s chomp() for removing trailing newlines from strings? . . . . 21 2.3.8 Is there a scanf() or sscanf() equivalent? . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.3.9 What does ‘UnicodeDecodeError’ or ‘UnicodeEncodeError’ error mean? . . . . . . . . . . 21 2.4 Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.4.1 My program is too slow. How do I speed it up? . . . . . . . . . . . . . . . . . . . . . . . 21 2.4.2 What is the most efficient way to concatenate many strings together? . . . . . . . . . . . . 22 2.5 Sequences (Tuples/Lists) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.5.1 How do I convert between tuples and lists? . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.5.2 What’s a negative index? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.5.3 How do I iterate over a sequence in reverse order? . . . . . . . . . . . . . . . . . . . . . 23 2.5.4 How do you remove duplicates from a list? . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.5.5 How do you remove multiple items from a list . . . . . . . . . . . . . . . . . . . . . . . 23 2.5.6 How do you make an array in Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.5.7 How do I create a multidimensional list? . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.5.8 How do I apply a method to a sequence of objects? . . . . . . . . . . . . . . . . . . . . . 25 2.5.9 Why does a_tuple[i] += [‘item’] raise an exception when the addition works? . . . . . . . 25 2.5.10 I want to do a complicated sort: can you do a Schwartzian Transform in Python? . . . . . 26 2.5.11 How can I sort one list by values from another list? . . . . . . . . . . . . . . . . . . . . . 26 2.6 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.6.1 What is a class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.6.2 What is a method? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.6.3 What is self? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.6.4 How do I check if an object is an instance of a given class or of a subclass of it? . . . . . . 27 2.6.5 What is delegation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.6.6 How do I call a method defined in a base class from a derived class that overrides it? . . . 29 2.6.7 How can I organize my code to make it easier to change the base class? . . . . . . . . . . 29 2.6.8 How do I create static class data and static class methods? . . . . . . . . . . . . . . . . . 29 2.6.9 How can I overload constructors (or methods) in Python? . . . . . . . . . . . . . . . . . 30 2.6.10 I try to use __spam and I get an error about _SomeClassName__spam. . . . . . . . . . . 30 2.6.11 My class defines __del__ but it is not called when I delete the object. . . . . . . . . . . . 30 2.6.12 How do I get a list of all instances of a given class? . . . . . . . . . . . . . . . . . . . . . 31 2.6.13 Why does the result of$_{id()}$ appear to be not unique? . . . . . . . . . . . . . . . . . . . 31 2.6.14 When can I rely on identity tests with the is operator? . . . . . . . . . . . . . . . . . . . 31 2.7 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.7.1 How do I create a .pyc file? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.7.2 How do I find the current module name? . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.7.3 How can I have modules that mutually import each other? . . . . . . . . . . . . . . . . . 33 2.7.4 __import__(‘x.y.z’) returns <module ‘x’>; how do I get z? . . . . . . . . . . . . . . . . . 34 2.7.5 When I edit an imported module and reimport it, the changes don’t show up. Why does this happen? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 3 Design and History FAQ 37 ii 3.1 Why does Python use indentation for grouping of statements? . . . . . . . . . . . . . . . . . . . . 37