0%

A comprehensive guide to exploring modern Python through data structures, design patterns, and effective object-oriented techniques

Key Features

  • Build an intuitive understanding of object-oriented design, from introductory to mature programs
  • Learn the ins and outs of Python syntax, libraries, and best practices
  • Examine a machine-learning case study at the end of each chapter

Book Description

Object-oriented programming (OOP) is a popular design paradigm in which data and behaviors are encapsulated in such a way that they can be manipulated together. Python Object-Oriented Programming, Fourth Edition dives deep into the various aspects of OOP, Python as an OOP language, common and advanced design patterns, and hands-on data manipulation and testing of more complex OOP systems. These concepts are consolidated by open-ended exercises, as well as a real-world case study at the end of every chapter, newly written for this edition. All example code is now compatible with Python 3.9+ syntax and has been updated with type hints for ease of learning.

Steven and Dusty provide a comprehensive, illustrative tour of important OOP concepts, such as inheritance, composition, and polymorphism, and explain how they work together with Python's classes and data structures to facilitate good design. In addition, the book also features an in-depth look at Python's exception handling and how functional programming intersects with OOP. Two very powerful automated testing systems, unittest and pytest, are introduced. The final chapter provides a detailed discussion of Python's concurrent programming ecosystem.

By the end of the book, you will have a thorough understanding of how to think about and apply object-oriented principles using Python syntax and be able to confidently create robust and reliable programs.

What you will learn

  • Implement objects in Python by creating classes and defining methods
  • Extend class functionality using inheritance
  • Use exceptions to handle unusual situations cleanly
  • Understand when to use object-oriented features, and more importantly, when not to use them
  • Discover several widely used design patterns and how they are implemented in Python
  • Uncover the simplicity of unit and integration testing and understand why they are so important
  • Learn to statically type check your dynamic code
  • Understand concurrency with asyncio and how it speeds up programs

Who this book is for

If you are new to object-oriented programming techniques, or if you have basic Python skills and wish to learn how and when to correctly apply OOP principles in Python, this is the book for you. Moreover, if you are an object-oriented programmer coming from other languages or seeking a leg up in the new world of Python, you will find this book a useful introduction to Python. Minimal previous experience with Python is necessary.

Table of Contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Get in touch
  2. Object-Oriented Design
    1. Introducing object-oriented
    2. Objects and classes
    3. Specifying attributes and behaviors
    4. Data describes object state
    5. Behaviors are actions
    6. Hiding details and creating the public interface
    7. Composition
    8. Inheritance
    9. Inheritance provides abstraction
    10. Multiple inheritance
    11. Case study
    12. Introduction and problem overview
    13. Context view
    14. Logical view
    15. Process view
    16. Development view
    17. Physical view
    18. Conclusion
    19. Recall
    20. Exercises
    21. Summary
  3. Objects in Python
    1. Introducing type hints
    2. Type checking
    3. Creating Python classes
    4. Adding attributes
    5. Making it do something
    6. Talking to yourself
    7. More arguments
    8. Initializing the object
    9. Type hints and defaults
    10. Explaining yourself with docstrings
    11. Modules and packages
    12. Organizing modules
    13. Absolute imports
    14. Relative imports
    15. Packages as a whole
    16. Organizing our code in modules
    17. Who can access my data?
    18. Third-party libraries
    19. Case study
    20. Logical view
    21. Samples and their states
    22. Sample state transitions
    23. Class responsibilities
    24. The TrainingData class
    25. Recall
    26. Exercises
    27. Summary
  4. When Objects Are Alike
    1. Basic inheritance
    2. Extending built-ins
    3. Overriding and super
    4. Multiple inheritance
    5. The diamond problem
    6. Different sets of arguments
    7. Polymorphism
    8. Case study
    9. Logical view
    10. Another distance
    11. Recall
    12. Exercises
    13. Summary
  5. Expecting the Unexpected
    1. Raising exceptions
    2. Raising an exception
    3. The effects of an exception
    4. Handling exceptions
    5. The exception hierarchy
    6. Defining our own exceptions
    7. Exceptions aren't exceptional
    8. Case study
    9. Context view
    10. Processing view
    11. What can go wrong?
    12. Bad behavior
    13. Creating samples from CSV files
    14. Validating enumerated values
    15. Reading CSV files
    16. Don't repeat yourself
    17. Recall
    18. Exercises
    19. Summary
  6. When to Use Object-Oriented Programming
    1. Treat objects as objects
    2. Adding behaviors to class data with properties
    3. Properties in detail
    4. Decorators – another way to create properties
    5. Deciding when to use properties
    6. Manager objects
    7. Removing duplicate code
    8. In practice
    9. Case study
    10. Input validation
    11. Input partitioning
    12. The sample class hierarchy
    13. The purpose enumeration
    14. Property setters
    15. Repeated if statements
    16. Recall
    17. Exercises
    18. Summary
  7. Abstract Base Classes and Operator Overloading
    1. Creating an abstract base class
    2. The ABCs of collections
    3. Abstract base classes and type hints
    4. The collections.abc module
    5. Creating your own abstract base class
    6. Demystifying the magic
    7. Operator overloading
    8. Extending built-ins
    9. Metaclasses
    10. Case study
    11. Extending the list class with two sublists
    12. A shuffling strategy for partitioning
    13. An incremental strategy for partitioning
    14. Recall
    15. Exercises
    16. Summary
  8. Python Data Structures
    1. Empty objects
    2. Tuples and named tuples
    3. Named tuples via typing.NamedTuple
    4. Dataclasses
    5. Dictionaries
    6. Dictionary use cases
    7. Using defaultdict
    8. Counter
    9. Lists
    10. Sorting lists
    11. Sets
    12. Three types of queues
    13. Case study
    14. Logical model
    15. Frozen dataclasses
    16. NamedTuple classes
    17. Conclusion
    18. Recall
    19. Exercises
    20. Summary
  9. The Intersection of Object-Oriented and Functional Programming
    1. Python built-in functions
    2. The len() function
    3. The reversed() function
    4. The enumerate() function
    5. An alternative to method overloading
    6. Default values for parameters
    7. Additional details on defaults
    8. Variable argument lists
    9. Unpacking arguments
    10. Functions are objects, too
    11. Function objects and callbacks
    12. Using functions to patch a class
    13. Callable objects
    14. File I/O
    15. Placing it in context
    16. Case study
    17. Processing overview
    18. Splitting the data
    19. Rethinking classification
    20. The partition() function
    21. One-pass partitioning
    22. Recall
    23. Exercises
    24. Summary
  10. Strings, Serialization, and File Paths
    1. Strings
    2. String manipulation
    3. String formatting
    4. Escaping braces
    5. f-strings can contain Python code
    6. Making it look right
    7. Custom formatters
    8. The format() method
    9. Strings are Unicode
    10. Decoding bytes to text
    11. Encoding text to bytes
    12. Mutable byte strings
    13. Regular expressions
    14. Matching patterns
    15. Matching a selection of characters
    16. Escaping characters
    17. Repeating patterns of characters
    18. Grouping patterns together
    19. Parsing information with regular expressions
    20. Other features of the re module
    21. Making regular expressions efficient
    22. Filesystem paths
    23. Serializing objects
    24. Customizing pickles
    25. Serializing objects using JSON
    26. Case study
    27. CSV format designs
    28. CSV dictionary reader
    29. CSV list reader
    30. JSON serialization
    31. Newline-delimited JSON
    32. JSON validation
    33. Recall
    34. Exercises
    35. Summary
  11. The Iterator Pattern
    1. Design patterns in brief
    2. Iterators
    3. The iterator protocol
    4. Comprehensions
    5. List comprehensions
    6. Set and dictionary comprehensions
    7. Generator expressions
    8. Generator functions
    9. Yield items from another iterable
    10. Generator stacks
    11. Case study
    12. The Set Builder background
    13. Multiple partitions
    14. Testing
    15. The essential k-NN algorithm
    16. k-NN using the bisect module
    17. k-NN using the heapq module
    18. Conclusion
    19. Recall
    20. Exercises
    21. Summary
  12. Common Design Patterns
    1. The Decorator pattern
    2. A Decorator example
    3. Decorators in Python
    4. The Observer pattern
    5. An Observer example
    6. The Strategy pattern
    7. A Strategy example
    8. Strategy in Python
    9. The Command pattern
    10. A Command example
    11. The State pattern
    12. A State example
    13. State versus Strategy
    14. The Singleton pattern
    15. Singleton implementation
    16. Case study
    17. Recall
    18. Exercises
    19. Summary
  13. Advanced Design Patterns
    1. The Adapter pattern
    2. An Adapter example
    3. The Façade pattern
    4. A Façade example
    5. The Flyweight pattern
    6. A Flyweight example in Python
    7. Multiple messages in a buffer
    8. Memory optimization via Python's __slots__
    9. The Abstract Factory pattern
    10. An Abstract Factory example
    11. Abstract Factories in Python
    12. The Composite pattern
    13. A Composite example
    14. The Template pattern
    15. A Template example
    16. Case study
    17. Recall
    18. Exercises
    19. Summary
  14. Testing Object-Oriented Programs
    1. Why test?
    2. Test-driven development
    3. Testing objectives
    4. Testing patterns
    5. Unit testing with unittest
    6. Unit testing with pytest
    7. pytest's setup and teardown functions
    8. pytest fixtures for setup and teardown
    9. More sophisticated fixtures
    10. Skipping tests with pytest
    11. Imitating objects using Mocks
    12. Additional patching techniques
    13. The sentinel object
    14. How much testing is enough?
    15. Testing and development
    16. Case study
    17. Unit testing the distance classes
    18. Unit testing the Hyperparameter class
    19. Recall
    20. Exercises
    21. Summary
  15. Concurrency
    1. Background on concurrent processing
    2. Threads
    3. The many problems with threads
    4. Shared memory
    5. The global interpreter lock
    6. Thread overhead
    7. Multiprocessing
    8. Multiprocessing pools
    9. Queues
    10. The problems with multiprocessing
    11. Futures
    12. AsyncIO
    13. AsyncIO in action
    14. Reading an AsyncIO future
    15. AsyncIO for networking
    16. Design considerations
    17. A log writing demonstration
    18. AsyncIO clients
    19. The dining philosophers benchmark
    20. Case study
    21. Recall
    22. Exercises
    23. Summary
  16. Other Books You May Enjoy
  17. Index
44.192.15.251