учебник Python для начинающих
17 книг для начинающих, опытных и продвинутых Python ...
Книги для начинающих Python-разработчиков ... «Изучаем Python» Марка Лутца — культовый учебник программирования, рассчитанный на новичков.
Python. Книга рецептов
« <anchor>Python. Книга рецептов</anchor>» — это учебник программирования, заточенный на практику. Скучной теории здесь мало, зато на каждой странице — задача с решением и обсуждением. Все задания основаны на реальных ситуациях в разработке, поэтому их можно воспринимать как готовые инструкции — не зря же это книга рецептов.
Python Cookbook, 3rd Edition[Book]
Python Cookbook, 3rd Edition\[Book\]

[Skip to main content](#maincontent)

[![O'Reilly home](https://cdn.oreillystatic.com/images/sitewide-headers/oreilly_logo_mark_red.svg)](https://www.oreilly.com "home page")

- [Sign In](/member/login/?next=/library/view/python-cookbook-3rd/9781449357337/)
- [Try Now](https://learning.oreilly.com/start-trial/)
- [Teams](https://www.oreilly.com/online-learning/teams.html)
  - [For business](https://www.oreilly.com/online-learning/business.html)
  - [For government](https://www.oreilly.com/online-learning/government.html)
  - [For higher ed](https://www.oreilly.com/online-learning/academic.html)
- [Individuals](https://www.oreilly.com/online-learning/individuals.html)
- [Features](https://www.oreilly.com/online-learning/features.html)
  - [All features](https://www.oreilly.com/online-learning/features.html)
  - [Courses](https://www.oreilly.com/online-learning/courses.html)
  - [Certifications](https://www.oreilly.com/online-learning/feature-certification.html)
  - [Interactive learning](https://www.oreilly.com/online-learning/intro-interactive-learning.html)
  - [Live events](https://www.oreilly.com/online-learning/live-events.html)
  - [Answers](https://www.oreilly.com/online-learning/feature-answers.html)
  - [Insights reporting](https://www.oreilly.com/online-learning/insights-dashboard.html)
- [Blog](https://www.oreilly.com/radar/)
- [Content sponsorship](https://www.oreilly.com/content-marketing-solutions.html)

Get full access to _Python Cookbook, 3rd Edition_ and 60K+ other titles, with a free 10-day trial of O'Reilly.

There are also live events, courses curated by job role, and more.

[Start your free trial](https://learning.oreilly.com/start-trial/)

![Python Cookbook, 3rd Edition](https://learning.oreilly.com/library/cover/9781449357337/250w/)

# Python Cookbook, 3rd Edition

by

[David Beazley](/search?q=author:"David Beazley"),

[Brian K. Jones](/search?q=author:"Brian K. Jones")

Released May 2013

Publisher(s): O'Reilly Media, Inc.

ISBN: 9781449357351


Read it
now on the O’Reilly learning platform with a 10-day free trial.

O’Reilly members get unlimited access to books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

[Start your free trial](https://learning.oreilly.com/start-trial/)

## Book  description

If you need help writing programs in Python 3, or want to update older Python 2 code, this book is just the ticket. Packed with practical recipes written and tested with Python 3.3, this unique cookbook is for experienced Python programmers who want to focus on modern tools and idioms.

Inside, you’ll find complete recipes for more than a dozen topics, covering the core Python language as well as tasks common to a wide variety of application domains. Each recipe contains code samples you can use in your projects right away, along with a discussion about how and why the solution works.

Topics include:

- Data Structures and Algorithms
- Strings and Text
- Numbers, Dates, and Times
- Iterators and Generators
- Files and I/O
- Data Encoding and Processing
- Functions
- Classes and Objects
- Metaprogramming
- Modules and Packages
- Network and Web Programming
- Concurrency
- Utility Scripting and System Administration
- Testing, Debugging, and Exceptions
- C Extensions

Show and hide more

## Publisher resources

[View/Submit Errata](http://oreilly.com/catalog/0636920027072/errata)

Table of contentsProduct information

## Table of contents

01. [Preface](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html)    1. [Who This Book Is For](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html#_who_this_book_is_for)
    2. [Who This Book Is Not For](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html#_who_this_book_is_not_for)
    3. [Conventions Used in This Book](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html#_conventions_used_in_this_book)
    4. [Online Code Examples](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html#_online_code_examples)
    5. [Using Code Examples](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html#_using_code_examples)
    6. [SafariÂ® Books Online](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html#_safari_books_online)
    7. [How to Contact Us](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html#_how_to_contact_us)
    8. [Acknowledgments](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/pr01.html#_acknowledgments)
02. [1\. Data Structures and Algorithms](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html)    01. [1.1. Unpacking a Sequence into Separate Variables](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#iterableunpack)
    02. [1.2. Unpacking Elements from Iterables of Arbitrary Length](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_unpacking_elements_from_iterables_of_arbitrary_length)
    03. [1.3. Keeping the Last N Items](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_keeping_the_last_n_items)
    04. [1.4. Finding the Largest or Smallest N Items](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#findingthelargestorsmallest)
    05. [1.5. Implementing a Priority Queue](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#priorityqueue)
    06. [1.6. Mapping Keys to Multiple Values in a Dictionary](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#multidict)
    07. [1.7. Keeping Dictionaries in Order](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#ordereddict)
    08. [1.8. Calculating with Dictionaries](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#dictcalc)
    09. [1.9. Finding Commonalities in Two Dictionaries](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_finding_commonalities_in_two_dictionaries)
    10. [1.10. Removing Duplicates from a Sequence while Maintaining Order](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_removing_duplicates_from_a_sequence_while_maintaining_order)
    11. [1.11. Naming a Slice](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_naming_a_slice)
    12. [1.12. Determining the Most Frequently Occurring Items in a Sequence](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_determining_the_most_frequently_occurring_items_in_a_sequence)
    13. [1.13. Sorting a List of Dictionaries by a Common Key](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#itemgetter)
    14. [1.14. Sorting Objects Without Native Comparison Support](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_sorting_objects_without_native_comparison_support)
    15. [1.15. Grouping Records Together Based on a Field](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#groupby)
    16. [1.16. Filtering Sequence Elements](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_filtering_sequence_elements)
    17. [1.17. Extracting a Subset of a Dictionary](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_extracting_a_subset_of_a_dictionary)
    18. [1.18. Mapping Names to Sequence Elements](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_mapping_names_to_sequence_elements)
    19. [1.19. Transforming and Reducing Data at the Same Time](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#generatorargs)
    20. [1.20. Combining Multiple Mappings into a Single Mapping](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch01.html#_combining_multiple_mappings_into_a_single_mapping)
03. [2\. Strings and Text](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html)    01. [2.1. Splitting Strings on Any of Multiple Delimiters](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_splitting_strings_on_any_of_multiple_delimiters)
    02. [2.2. Matching Text at the Start or End of a String](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_matching_text_at_the_start_or_end_of_a_string)
    03. [2.3. Matching Strings Using Shell Wildcard Patterns](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_matching_strings_using_shell_wildcard_patterns)
    04. [2.4. Matching and Searching for Text Patterns](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_matching_and_searching_for_text_patterns)
    05. [2.5. Searching and Replacing Text](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_searching_and_replacing_text)
    06. [2.6. Searching and Replacing Case-Insensitive Text](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_searching_and_replacing_case_insensitive_text)
    07. [2.7. Specifying a Regular Expression for the Shortest Match](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_specifying_a_regular_expression_for_the_shortest_match)
    08. [2.8. Writing a Regular Expression for Multiline Patterns](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_writing_a_regular_expression_for_multiline_patterns)
    09. [2.9. Normalizing Unicode Text to a Standard Representation](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#normalizingunicodetext)
    10. [2.10. Working with Unicode Characters in Regular Expressions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#unicodere)
    11. [2.11. Stripping Unwanted Characters from Strings](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_stripping_unwanted_characters_from_strings)
    12. [2.12. Sanitizing and Cleaning Up Text](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_sanitizing_and_cleaning_up_text)
    13. [2.13. Aligning Text Strings](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_aligning_text_strings)
    14. [2.14. Combining and Concatenating Strings](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_combining_and_concatenating_strings)
    15. [2.15. Interpolating Variables in Strings](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#interpolation)
    16. [2.16. Reformatting Text to a Fixed Number of Columns](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_reformatting_text_to_a_fixed_number_of_columns)
    17. [2.17. Handling HTML and XML Entities in Text](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_handling_html_and_xml_entities_in_text)
    18. [2.18. Tokenizing Text](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#tokenizing)
    19. [2.19. Writing a Simple Recursive Descent Parser](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_writing_a_simple_recursive_descent_parser)
    20. [2.20. Performing Text Operations on Byte Strings](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch02.html#_performing_text_operations_on_byte_strings)
04. [3\. Numbers, Dates, and Times](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html)    01. [3.1. Rounding Numerical Values](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_rounding_numerical_values)
    02. [3.2. Performing Accurate Decimal Calculations](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_performing_accurate_decimal_calculations)
    03. [3.3. Formatting Numbers for Output](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_formatting_numbers_for_output)
    04. [3.4. Working with Binary, Octal, and Hexadecimal Integers](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_working_with_binary_octal_and_hexadecimal_integers)
    05. [3.5. Packing and Unpacking Large Integers from Bytes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_packing_and_unpacking_large_integers_from_bytes)
    06. [3.6. Performing Complex-Valued Math](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_performing_complex_valued_math)
    07. [3.7. Working with Infinity and NaNs](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_working_with_infinity_and_nans)
    08. [3.8. Calculating with Fractions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_calculating_with_fractions)
    09. [3.9. Calculating with Large Numerical Arrays](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#calculatingwithlargenumerical)
    10. [3.10. Performing Matrix and Linear Algebra Calculations](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_performing_matrix_and_linear_algebra_calculations)
    11. [3.11. Picking Things at Random](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_picking_things_at_random)
    12. [3.12. Converting Days to Seconds, and Other Basic Time Conversions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_converting_days_to_seconds_and_other_basic_time_conversions)
    13. [3.13. Determining Last Fridayâs Date](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_determining_last_friday_8217_s_date)
    14. [3.14. Finding the Date Range for the Current Month](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_finding_the_date_range_for_the_current_month)
    15. [3.15. Converting Strings into Datetimes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_converting_strings_into_datetimes)
    16. [3.16. Manipulating Dates Involving Time Zones](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch03.html#_manipulating_dates_involving_time_zones)
05. [4\. Iterators and Generators](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html)    01. [4.1. Manually Consuming an Iterator](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_manually_consuming_an_iterator)
    02. [4.2. Delegating Iteration](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#delegate_iteration)
    03. [4.3. Creating New Iteration Patterns with Generators](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#generators)
    04. [4.4. Implementing the Iterator Protocol](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_implementing_the_iterator_protocol)
    05. [4.5. Iterating in Reverse](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_iterating_in_reverse)
    06. [4.6. Defining Generator Functions with Extra State](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_defining_generator_functions_with_extra_state)
    07. [4.7. Taking a Slice of an Iterator](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#takingasliceofaniterator)
    08. [4.8. Skipping the First Part of an Iterable](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_skipping_the_first_part_of_an_iterable)
    09. [4.9. Iterating Over All Possible Combinations or Permutations](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_iterating_over_all_possible_combinations_or_permutations)
    10. [4.10. Iterating Over the Index-Value Pairs of a Sequence](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_iterating_over_the_index_value_pairs_of_a_sequence)
    11. [4.11. Iterating Over Multiple Sequences Simultaneously](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_iterating_over_multiple_sequences_simultaneously)
    12. [4.12. Iterating on Items in Separate Containers](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_iterating_on_items_in_separate_containers)
    13. [4.13. Creating Data Processing Pipelines](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_creating_data_processing_pipelines)
    14. [4.14. Flattening a Nested Sequence](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#subgenerators)
    15. [4.15. Iterating in Sorted Order Over Merged Sorted Iterables](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_iterating_in_sorted_order_over_merged_sorted_iterables)
    16. [4.16. Replacing Infinite while Loops with an Iterator](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch04.html#_replacing_infinite_while_loops_with_an_iterator)
06. [5\. Files and I/O](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html)    01. [5.1. Reading and Writing Text Data](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_reading_and_writing_text_data)
    02. [5.2. Printing to a File](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_printing_to_a_file)
    03. [5.3. Printing with a Different Separator or Line Ending](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_printing_with_a_different_separator_or_line_ending)
    04. [5.4. Reading and Writing Binary Data](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_reading_and_writing_binary_data)
    05. [5.5. Writing to a File That Doesnât Already Exist](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_writing_to_a_file_that_doesn_8217_t_already_exist)
    06. [5.6. Performing I/O Operations on a String](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#stringio)
    07. [5.7. Reading and Writing Compressed Datafiles](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_reading_and_writing_compressed_datafiles)
    08. [5.8. Iterating Over Fixed-Sized Records](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#iterrecords)
    09. [5.9. Reading Binary Data into a Mutable Buffer](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#mutablebuffer)
    10. [5.10. Memory Mapping Binary Files](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_memory_mapping_binary_files)
    11. [5.11. Manipulating Pathnames](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#pathnames)
    12. [5.12. Testing for the Existence of a File](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_testing_for_the_existence_of_a_file)
    13. [5.13. Getting a Directory Listing](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#dirlisting)
    14. [5.14. Bypassing Filename Encoding](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#rawfilenames)
    15. [5.15. Printing Bad Filenames](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#bad_filenames)
    16. [5.16. Adding or Changing the Encoding of an Already Open File](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_adding_or_changing_the_encoding_of_an_already_open_file)
    17. [5.17. Writing Bytes to a Text File](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_writing_bytes_to_a_text_file)
    18. [5.18. Wrapping an Existing File Descriptor As a File Object](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_wrapping_an_existing_file_descriptor_as_a_file_object)
    19. [5.19. Making Temporary Files and Directories](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_making_temporary_files_and_directories)
    20. [5.20. Communicating with Serial Ports](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_communicating_with_serial_ports)
    21. [5.21. Serializing Python Objects](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch05.html#_serializing_python_objects)
07. [6\. Data Encoding and Processing](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html)    01. [6.1. Reading and Writing CSV Data](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_reading_and_writing_csv_data)
    02. [6.2. Reading and Writing JSON Data](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_reading_and_writing_json_data)
    03. [6.3. Parsing Simple XML Data](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_parsing_simple_xml_data)
    04. [6.4. Parsing Huge XML Files Incrementally](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_parsing_huge_xml_files_incrementally)
    05. [6.5. Turning a Dictionary into XML](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#turningadictionaryintoxml)
    06. [6.6. Parsing, Modifying, and Rewriting XML](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_parsing_modifying_and_rewriting_xml)
    07. [6.7. Parsing XML Documents with Namespaces](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_parsing_xml_documents_with_namespaces)
    08. [6.8. Interacting with a Relational Database](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_interacting_with_a_relational_database)
    09. [6.9. Decoding and Encoding Hexadecimal Digits](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_decoding_and_encoding_hexadecimal_digits)
    10. [6.10. Decoding and Encoding Base64](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#_decoding_and_encoding_base64)
    11. [6.11. Reading and Writing Binary Arrays of Structures](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#binstructarrays)
    12. [6.12. Reading Nested and Variable-Sized Binary Structures](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#nestedbinaryrecords)
    13. [6.13. Summarizing Data and Performing Statistics](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch06.html#pandas)
08. [7\. Functions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html)    01. [7.1. Writing Functions That Accept Any Number of Arguments](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#_writing_functions_that_accept_any_number_of_arguments)
    02. [7.2. Writing Functions That Only Accept Keyword Arguments](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#keywordargs)
    03. [7.3. Attaching Informational Metadata to Function Arguments](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#_attaching_informational_metadata_to_function_arguments)
    04. [7.4. Returning Multiple Values from a Function](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#_returning_multiple_values_from_a_function)
    05. [7.5. Defining Functions with Default Arguments](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#_defining_functions_with_default_arguments)
    06. [7.6. Defining Anonymous or Inline Functions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#lambda)
    07. [7.7. Capturing Variables in Anonymous Functions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#_capturing_variables_in_anonymous_functions)
    08. [7.8. Making an N-Argument Callable Work As a Callable with Fewer Arguments](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#partial)
    09. [7.9. Replacing Single Method Classes with Functions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#_replacing_single_method_classes_with_functions)
    10. [7.10. Carrying Extra State with Callback Functions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#carryingextrastatewithcbfunctions)
    11. [7.11. Inlining Callback Functions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#_inlining_callback_functions)
    12. [7.12. Accessing Variables Defined Inside a Closure](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch07.html#_accessing_variables_defined_inside_a_closure)
09. [8\. Classes and Objects](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html)    01. [8.1. Changing the String Representation of Instances](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_changing_the_string_representation_of_instances)
    02. [8.2. Customizing String Formatting](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_customizing_string_formatting)
    03. [8.3. Making Objects Support the Context-Management Protocol](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#context_manager)
    04. [8.4. Saving Memory When Creating a Large Number of Instances](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#class_with_slots)
    05. [8.5. Encapsulating Names in a Class](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_encapsulating_names_in_a_class)
    06. [8.6. Creating Managed Attributes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#property)
    07. [8.7. Calling a Method on a Parent Class](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#super)
    08. [8.8. Extending a Property in a Subclass](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_extending_a_property_in_a_subclass)
    09. [8.9. Creating a New Kind of Class or Instance Attribute](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#descriptors)
    10. [8.10. Using Lazily Computed Properties](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#lazy_property)
    11. [8.11. Simplifying the Initialization of Data Structures](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#simpledatastructure)
    12. [8.12. Defining an Interface or Abstract Base Class](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#abc)
    13. [8.13. Implementing a Data Model or Type System](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#datamodel)
    14. [8.14. Implementing Custom Containers](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_implementing_custom_containers)
    15. [8.15. Delegating Attribute Access](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_delegating_attribute_access)
    16. [8.16. Defining More Than One Constructor in a Class](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_defining_more_than_one_constructor_in_a_class)
    17. [8.17. Creating an Instance Without Invoking init](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#createnoinit)
    18. [8.18. Extending Classes with Mixins](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#mixins)
    19. [8.19. Implementing Stateful Objects or State Machines](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_implementing_stateful_objects_or_state_machines)
    20. [8.20. Calling a Method on an Object Given the Name As a String](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_calling_a_method_on_an_object_given_the_name_as_a_string)
    21. [8.21. Implementing the Visitor Pattern](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#visitor)
    22. [8.22. Implementing the Visitor Pattern Without Recursion](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#genvisitor)
    23. [8.23. Managing Memory in Cyclic Data Structures](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#weakref)
    24. [8.24. Making Classes Support Comparison Operations](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#_making_classes_support_comparison_operations)
    25. [8.25. Creating Cached Instances](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch08.html#caching)
10. [9\. Metaprogramming](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html)    01. [9.1. Putting a Wrapper Around a Function](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_putting_a_wrapper_around_a_function)
    02. [9.2. Preserving Function Metadata When Writing Decorators](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#decoratormeta)
    03. [9.3. Unwrapping a Decorator](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_unwrapping_a_decorator)
    04. [9.4. Defining a Decorator That Takes Arguments](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_defining_a_decorator_that_takes_arguments)
    05. [9.5. Defining a Decorator with User Adjustable Attributes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#decorator_parms)
    06. [9.6. Defining a Decorator That Takes an Optional Argument](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_defining_a_decorator_that_takes_an_optional_argument)
    07. [9.7. Enforcing Type Checking on a Function Using a Decorator](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#typeassertdecorator)
    08. [9.8. Defining Decorators As Part of a Class](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_defining_decorators_as_part_of_a_class)
    09. [9.9. Defining Decorators As Classes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#decoratorasclass)
    10. [9.10. Applying Decorators to Class and Static Methods](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_applying_decorators_to_class_and_static_methods)
    11. [9.11. Writing Decorators That Add Arguments to Wrapped Functions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#decoratoraddarg)
    12. [9.12. Using Decorators to Patch Class Definitions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#classdecorator)
    13. [9.13. Using a Metaclass to Control Instance Creation](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#metacreational)
    14. [9.14. Capturing Class Attribute Definition Order](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#definitionorder)
    15. [9.15. Defining a Metaclass That Takes Optional Arguments](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_defining_a_metaclass_that_takes_optional_arguments)
    16. [9.16. Enforcing an Argument Signature on \*args and \*\*kwargs](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#signatures)
    17. [9.17. Enforcing Coding Conventions in Classes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_enforcing_coding_conventions_in_classes)
    18. [9.18. Defining Classes Programmatically](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_defining_classes_programmatically)
    19. [9.19. Initializing Class Members at Definition Time](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#metaclassinit)
    20. [9.20. Implementing Multiple Dispatch with Function Annotations](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#multipledispatch)
    21. [9.21. Avoiding Repetitive Property Methods](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#propertyclosures)
    22. [9.22. Defining Context Managers the Easy Way](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#contextmanager_decorator)
    23. [9.23. Executing Code with Local Side Effects](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_executing_code_with_local_side_effects)
    24. [9.24. Parsing and Analyzing Python Source](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#ast)
    25. [9.25. Disassembling Python Byte Code](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch09.html#_disassembling_python_byte_code)
11. [10\. Modules and Packages](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html)    01. [10.1. Making a Hierarchical Package of Modules](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_making_a_hierarchical_package_of_modules)
    02. [10.2. Controlling the Import of Everything](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_controlling_the_import_of_everything)
    03. [10.3. Importing Package Submodules Using Relative Names](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#importingpackagesubmodulesusingrelative)
    04. [10.4. Splitting a Module into Multiple Files](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#modulesplitting)
    05. [10.5. Making Separate Directories of Code Import Under a Common Namespace](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#namespacepackage)
    06. [10.6. Reloading Modules](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_reloading_modules)
    07. [10.7. Making a Directory or Zip File Runnable As a Main Script](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_making_a_directory_or_zip_file_runnable_as_a_main_script)
    08. [10.8. Reading Datafiles Within a Package](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_reading_datafiles_within_a_package)
    09. [10.9. Adding Directories to sys.path](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_adding_directories_to_sys_path)
    10. [10.10. Importing Modules Using a Name Given in a String](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_importing_modules_using_a_name_given_in_a_string)
    11. [10.11. Loading Modules from a Remote Machine Using Import Hooks](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#importhooks)
    12. [10.12. Patching Modules on Import](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_patching_modules_on_import)
    13. [10.13. Installing Packages Just for Yourself](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_installing_packages_just_for_yourself)
    14. [10.14. Creating a New Python Environment](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_creating_a_new_python_environment)
    15. [10.15. Distributing Packages](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch10.html#_distributing_packages)
12. [11\. Network and Web Programming](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html)    01. [11.1. Interacting with HTTP Services As a Client](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#_interacting_with_http_services_as_a_client)
    02. [11.2. Creating a TCP Server](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#tcpserver)
    03. [11.3. Creating a UDP Server](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#_creating_a_udp_server)
    04. [11.4. Generating a Range of IP Addresses from a CIDR Address](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#_generating_a_range_of_ip_addresses_from_a_cidr_address)
    05. [11.5. Creating a Simple REST-Based Interface](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#_creating_a_simple_rest_based_interface)
    06. [11.6. Implementing a Simple Remote Procedure Call with XML-RPC](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#xmlrpc)
    07. [11.7. Communicating Simply Between Interpreters](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#multiconn)
    08. [11.8. Implementing Remote Procedure Calls](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#rpc)
    09. [11.9. Authenticating Clients Simply](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#_authenticating_clients_simply)
    10. [11.10. Adding SSL to Network Services](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#ssl)
    11. [11.11. Passing a Socket File Descriptor Between Processes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#_passing_a_socket_file_descriptor_between_processes)
    12. [11.12. Understanding Event-Driven I/O](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#_understanding_event_driven_i_o)
    13. [11.13. Sending and Receiving Large Arrays](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch11.html#_sending_and_receiving_large_arrays)
13. [12\. Concurrency](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html)    01. [12.1. Starting and Stopping Threads](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#thread_start)
    02. [12.2. Determining If a Thread Has Started](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#_determining_if_a_thread_has_started)
    03. [12.3. Communicating Between Threads](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#thread_communication)
    04. [12.4. Locking Critical Sections](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#_locking_critical_sections)
    05. [12.5. Locking with Deadlock Avoidance](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#deadlock)
    06. [12.6. Storing Thread-Specific State](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#thread_local)
    07. [12.7. Creating a Thread Pool](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#_creating_a_thread_pool)
    08. [12.8. Performing Simple Parallel Programming](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#_performing_simple_parallel_programming)
    09. [12.9. Dealing with the GIL (and How to Stop Worrying About It)](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#_dealing_with_the_gil_and_how_to_stop_worrying_about_it)
    10. [12.10. Defining an Actor Task](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#actors)
    11. [12.11. Implementing Publish/Subscribe Messaging](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#_implementing_publish_subscribe_messaging)
    12. [12.12. Using Generators As an Alternative to Threads](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#coroutine_concurrency)
    13. [12.13. Polling Multiple Thread Queues](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#_polling_multiple_thread_queues)
    14. [12.14. Launching a Daemon Process on Unix](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch12.html#_launching_a_daemon_process_on_unix)
14. [13\. Utility Scripting and System Administration](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html)    01. [13.1. Accepting Script Input via Redirection, Pipes, or Input Files](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_accepting_script_input_via_redirection_pipes_or_input_files)
    02. [13.2. Terminating a Program with an Error Message](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_terminating_a_program_with_an_error_message)
    03. [13.3. Parsing Command-Line Options](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_parsing_command_line_options)
    04. [13.4. Prompting for a Password at Runtime](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_prompting_for_a_password_at_runtime)
    05. [13.5. Getting the Terminal Size](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_getting_the_terminal_size)
    06. [13.6. Executing an External Command and Getting Its Output](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_executing_an_external_command_and_getting_its_output)
    07. [13.7. Copying or Moving Files and Directories](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_copying_or_moving_files_and_directories)
    08. [13.8. Creating and Unpacking Archives](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_creating_and_unpacking_archives)
    09. [13.9. Finding Files by Name](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_finding_files_by_name)
    10. [13.10. Reading Configuration Files](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_reading_configuration_files)
    11. [13.11. Adding Logging to Simple Scripts](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_adding_logging_to_simple_scripts)
    12. [13.12. Adding Logging to Libraries](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_adding_logging_to_libraries)
    13. [13.13. Making a Stopwatch Timer](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#stopwatch)
    14. [13.14. Putting Limits on Memory and CPU Usage](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_putting_limits_on_memory_and_cpu_usage)
    15. [13.15. Launching a Web Browser](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch13.html#_launching_a_web_browser)
15. [14\. Testing, Debugging, and Exceptions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html)    01. [14.1. Testing Output Sent to stdout](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_testing_output_sent_to_stdout)
    02. [14.2. Patching Objects in Unit Tests](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_patching_objects_in_unit_tests)
    03. [14.3. Testing for Exceptional Conditions in Unit Tests](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_testing_for_exceptional_conditions_in_unit_tests)
    04. [14.4. Logging Test Output to a File](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_logging_test_output_to_a_file)
    05. [14.5. Skipping or Anticipating Test Failures](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_skipping_or_anticipating_test_failures)
    06. [14.6. Handling Multiple Exceptions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_handling_multiple_exceptions)
    07. [14.7. Catching All Exceptions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_catching_all_exceptions)
    08. [14.8. Creating Custom Exceptions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_creating_custom_exceptions)
    09. [14.9. Raising an Exception in Response to Another Exception](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_raising_an_exception_in_response_to_another_exception)
    10. [14.10. Reraising the Last Exception](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_reraising_the_last_exception)
    11. [14.11. Issuing Warning Messages](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_issuing_warning_messages)
    12. [14.12. Debugging Basic Program Crashes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_debugging_basic_program_crashes)
    13. [14.13. Profiling and Timing Your Program](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#profiling)
    14. [14.14. Making Your Programs Run Faster](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch14.html#_making_your_programs_run_faster)
16. [15\. C Extensions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html)    01. [15.1. Accessing C Code Using ctypes](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_accessing_c_code_using_ctypes)
    02. [15.2. Writing a Simple C Extension Module](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#simpleextension)
    03. [15.3. Writing an Extension Function That Operates on Arrays](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#carray)
    04. [15.4. Managing Opaque Pointers in C Extension Modules](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#capsules)
    05. [15.5. Defining and Exporting C APIs from Extension Modules](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_defining_and_exporting_c_apis_from_extension_modules)
    06. [15.6. Calling Python from C](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_calling_python_from_c)
    07. [15.7. Releasing the GIL in C Extensions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#releasegil)
    08. [15.8. Mixing Threads from C and Python](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_mixing_threads_from_c_and_python)
    09. [15.9. Wrapping C Code with Swig](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#swig)
    10. [15.10. Wrapping Existing C Code with Cython](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#cython_existing)
    11. [15.11. Using Cython to Write High-Performance Array Operations](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#cythonarray)
    12. [15.12. Turning a Function Pointer into a Callable](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_turning_a_function_pointer_into_a_callable)
    13. [15.13. Passing NULL-Terminated Strings to C Libraries](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_passing_null_terminated_strings_to_c_libraries)
    14. [15.14. Passing Unicode Strings to C Libraries](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_passing_unicode_strings_to_c_libraries)
    15. [15.15. Converting C Strings to Python](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_converting_c_strings_to_python)
    16. [15.16. Working with C Strings of Dubious Encoding](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_working_with_c_strings_of_dubious_encoding)
    17. [15.17. Passing Filenames to C Extensions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_passing_filenames_to_c_extensions)
    18. [15.18. Passing Open Files to C Extensions](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_passing_open_files_to_c_extensions)
    19. [15.19. Reading File-Like Objects from C](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_reading_file_like_objects_from_c)
    20. [15.20. Consuming an Iterable from C](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_consuming_an_iterable_from_c)
    21. [15.21. Diagnosing Segmentation Faults](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ch15.html#_diagnosing_segmentation_faults)
17. [A. Further Reading](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/apa.html)    1. [Online Resources](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/apa.html#_online_resources)
    2. [Books for Learning Python](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/apa.html#_books_for_learning_python)
    3. [Advanced Books](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/apa.html#_advanced_books)
18. [Index](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/ix01.html)
19. [Colophon](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/co01.html)
20. [Copyright](https://learning.oreilly.com/library/view/python-cookbook-3rd/9781449357337/copyright.html)

Show and hide more

## Product information

- Title:Python Cookbook, 3rd Edition
- Author(s):David Beazley, Brian K. Jones
- Release date:May 2013
- Publisher(s):O'Reilly Media, Inc.
- ISBN:9781449357351

## You might also like

[book\
**Head First Python, 3rd Edition**\
\
by\
\
Paul Barry\
\
\
\
What will you learn from this book? Want to learn the Python language without slogging your …](https://learning.oreilly.com/library/view/-/9781492051282/) [book\
**Learning Python, 5th Edition**\
\
by\
\
Mark Lutz\
\
\
\
Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on …](https://learning.oreilly.com/library/view/-/9781449355722/) [book\
**Modern Python Cookbook - Second Edition**\
\
by\
\
Steven F. Lott\
\
\
\
Complete recipes spread across 15 chapters to help you overcome commonly faced issues by Python for …](https://learning.oreilly.com/library/view/-/9781800207455/) [book\
**Python Crash Course, 2nd Edition**\
\
by\
\
Eric Matthes\
\
\
\
This is the second edition of the best selling Python book in the world. Python Crash …](https://learning.oreilly.com/library/view/-/9781492071266/)

## [About O’Reilly](https://www.oreilly.com/about/)

- [Teach/write/train](https://www.oreilly.com/work-with-us.html)
- [Careers](https://www.oreilly.com/careers/)
- [Press releases](https://www.oreilly.com/press/)
- [Media coverage](https://www.oreilly.com/press/media-coverage.html)
- [Community partners](https://www.oreilly.com/partner/signup.csp)
- [Affiliate program](https://www.oreilly.com/affiliates/)
- [Submit an RFP](https://www.oreilly.com/online-learning/rfp.html)
- [Diversity](https://www.oreilly.com/diversity/)
- [O’Reilly for marketers](https://www.oreilly.com/content-marketing-solutions.html)

## [Support](https://www.oreilly.com/online-learning/support/)

- [Contact us](https://www.oreilly.com/about/contact.html)
- [Newsletters](https://www.oreilly.com/emails/newsletters/)
- [Privacy policy](https://www.oreilly.com/privacy.html)

[linkedin-logo](https://www.linkedin.com/company/oreilly-media) [youtube-logo](https://www.youtube.com/user/OreillyMedia)

## International

- [Australia & New Zealand](https://www.oreilly.com/anz/)
- [Hong Kong & Taiwan](https://oreilly.hk/)
- [India](https://oreillylearning.in/)
- [Indonesia](https://oreilly.id/)
- [Japan](https://www.oreilly.co.jp/index.shtml)

## Download the O’Reilly App

Take O’Reilly with you and learn anywhere, anytime on your phone and tablet.

[![Apple app store](https://cdn.oreillystatic.com/oreilly/images/app-store-logo.png)](https://itunes.apple.com/us/app/safari-to-go/id881697395)[![Google play store](https://cdn.oreillystatic.com/oreilly/images/google-play-logo.png)](https://play.google.com/store/apps/details?id=com.safariflow.queue)

## Watch on your big screen

View all O’Reilly videos, Superstream events, and Meet the Expert sessions on your home TV.

[![Roku Payers and TVs](https://cdn.oreillystatic.com/oreilly/images/roku-tv-logo.png)](https://channelstore.roku.com/details/c8a2d0096693eb9455f6ac165003ee06/oreilly)[![Amazon appstore](https://cdn.oreillystatic.com/oreilly/images/amazon-appstore-logo.png)](https://www.amazon.com/OReilly-Media-Inc/dp/B087YYHL5C/ref=sr_1_2?dchild=1&keywords=oreilly&qid=1604964116&s=mobile-apps&sr=1-2)

## [Do not sell my personal information](https://www.oreilly.com/privacy.html?donotsell=show)

[![O'Reilly home](https://cdn.oreillystatic.com/images/sitewide-headers/oreilly_logo_mark_red.svg)](https://www.oreilly.com "home page")

© 2024, O’Reilly Media, Inc. All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.

[Terms of service](https://www.oreilly.com/terms/) • [Privacy policy](https://www.oreilly.com/privacy.html) • [Editorial independence](https://www.oreilly.com/about/editorial_independence.html)

## Don’t leave empty-handed

Get Mark Richards’s _Software Architecture Patterns_ ebook to better understand how to design components—and how they should interact.

### It’s yours, free.

[Get it now](https://get.oreilly.com/ind_software-architecture-patterns.html)

![Cover of Software Architecture Patterns](https://cdn.oreillystatic.com/oreilly/images/report-software-architecture-patterns-553x420.jpg)

Close

## Check it out now on O’Reilly

Dive in for free with a 10-day trial of the O’Reilly learning platform—then explore all the other resources our members count on to build skills and solve problems every day.

[Start your free trial](https://www.oreilly.com/start-trial/) [Become a member now](https://learning.oreilly.com/signup/)

![](https://cdn.oreillystatic.com/oreilly/images/laptop-flat-topics-ml-1124x638.png)

Close