Python 3 Object-Oriented Programming. - Third Edition

Book description

Uncover modern Python with this guide to Python data structures, design patterns, and effective object-oriented techniques

Key Features

  • In-depth analysis of many common object-oriented design patterns that are more suitable to Python's unique style
  • Learn the latest Python syntax and libraries
  • Explore abstract design patterns and implement them in Python 3.8

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. This third edition of Python 3 Object-Oriented Programming fully explains classes, data encapsulation, and exceptions with an emphasis on when you can use each principle to develop well-designed software.

Starting with a detailed analysis of object-oriented programming, you will use the Python programming language to clearly grasp key concepts from the object-oriented paradigm. You will learn how to create maintainable applications by studying higher level design patterns. The book will show you the complexities of string and file manipulation, and how Python distinguishes between binary and textual data. Not one, but two very powerful automated testing systems, unittest and pytest, will be introduced in this book. You'll get a comprehensive introduction to Python's concurrent programming ecosystem.

By the end of the book, you will have thoroughly learned object-oriented principles using Python syntax and be able to create robust and reliable programs confidently.

What you will learn

  • Implement objects in Python by creating classes and defining methods
  • Grasp common concurrency techniques and pitfalls in Python 3
  • Extend class functionality using inheritance
  • Understand when to use object-oriented features, and more importantly when not to use them
  • Discover what design patterns are and why they are different in Python
  • Uncover the simplicity of unit testing and why it s so important in Python
  • Explore concurrent object-oriented programming

Who this book is for

If you're new to object-oriented programming techniques, or if you have basic Python skills and wish to learn in depth how and when to correctly apply OOP in Python, this is the book for you. If you are an object-oriented programmer for other languages or seeking a leg up in the new world of Python 3.8, you too will find this book a useful introduction to Python. Previous experience with Python 3 is not necessary.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Python 3 Object-Oriented Programming Third Edition
  3. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. Object-Oriented Design
    1. Introducing object-oriented
    2. Objects and classes
    3. Specifying attributes and behaviors
      1. Data describes objects
      2. Behaviors are actions
    4. Hiding details and creating the public interface
    5. Composition
    6. Inheritance
      1. Inheritance provides abstraction
      2. Multiple inheritance
    7. Case study
    8. Exercises
    9. Summary
  7. Objects in Python
    1. Creating Python classes
      1. Adding attributes
      2. Making it do something
        1. Talking to yourself
        2. More arguments
      3. Initializing the object
      4. Explaining yourself
    2. Modules and packages
      1. Organizing modules
        1. Absolute imports
        2. Relative imports
    3. Organizing module content
    4. Who can access my data?
    5. Third-party libraries
    6. Case study
    7. Exercises
    8. Summary
  8. When Objects Are Alike
    1. Basic inheritance
      1. Extending built-ins
      2. Overriding and super
    2. Multiple inheritance
      1. The diamond problem
      2. Different sets of arguments
    3. Polymorphism
    4. Abstract base classes
      1. Using an abstract base class
      2. Creating an abstract base class
      3. Demystifying the magic
    5. Case study
    6. Exercises
    7. Summary
  9. Expecting the Unexpected
    1. Raising exceptions
      1. Raising an exception
      2. The effects of an exception
      3. Handling exceptions
      4. The exception hierarchy
      5. Defining our own exceptions
    2. Case study
    3. Exercises
    4. Summary
  10. When to Use Object-Oriented Programming
    1. Treat objects as objects
    2. Adding behaviors to class data with properties
      1. Properties in detail
      2. Decorators – another way to create properties
      3. Deciding when to use properties
    3. Manager objects
      1. Removing duplicate code
      2. In practice
    4. Case study
    5. Exercises
    6. Summary
  11. Python Data Structures
    1. Empty objects
    2. Tuples and named tuples
      1. Named tuples
    3. Dataclasses
    4. Dictionaries
      1. Dictionary use cases
      2. Using defaultdict
        1. Counter
    5. Lists
      1. Sorting lists
    6. Sets
    7. Extending built-in functions
    8. Case study
    9. Exercises
    10. Summary
  12. Python Object-Oriented Shortcuts
    1. Python built-in functions
      1. The len() function
      2. Reversed
      3. Enumerate
      4. File I/O
      5. Placing it in context
    2. An alternative to method overloading
      1. Default arguments
      2. Variable argument lists
      3. Unpacking arguments
    3. Functions are objects too
      1. Using functions as attributes
      2. Callable objects
    4. Case study
    5. Exercises
    6. Summary
  13. Strings and Serialization
    1. Strings
      1. String manipulation
      2. String formatting
        1. Escaping braces
        2. f-strings can contain Python code
        3. Making it look right
        4. Custom formatters
        5. The format method
      3. Strings are Unicode
        1. Converting bytes to text
        2. Converting text to bytes
      4. Mutable byte strings
    2. Regular expressions
      1. Matching patterns
        1. Matching a selection of characters
        2. Escaping characters
        3. Matching multiple characters
        4. Grouping patterns together
      2. Getting information from regular expressions
        1. Making repeated regular expressions efficient
    3. Filesystem paths
    4. Serializing objects
      1. Customizing pickles
      2. Serializing web objects
    5. Case study
    6. Exercises
    7. Summary
  14. The Iterator Pattern
    1. Design patterns in brief
    2. Iterators
      1. The iterator protocol
    3. Comprehensions
      1. List comprehensions
      2. Set and dictionary comprehensions
      3. Generator expressions
    4. Generators
      1. Yield items from another iterable
    5. Coroutines
      1. Back to log parsing
      2. Closing coroutines and throwing exceptions
      3. The relationship between coroutines, generators, and functions
    6. Case study
    7. Exercises
    8. Summary
  15. Python Design Patterns I
    1. The decorator pattern
      1. A decorator example
      2. Decorators in Python
    2. The observer pattern
      1. An observer example
    3. The strategy pattern
      1. A strategy example
      2. Strategy in Python
    4. The state pattern
      1. A state example
      2. State versus strategy
      3. State transition as coroutines
    5. The singleton pattern
      1. Singleton implementation
      2. Module variables can mimic singletons
    6. The template pattern
      1. A template example
    7. Exercises
    8. Summary
  16. Python Design Patterns II
    1. The adapter pattern
    2. The facade pattern
    3. The flyweight pattern
    4. The command pattern
    5. The abstract factory pattern
    6. The composite pattern
    7. Exercises
    8. Summary
  17. Testing Object-Oriented Programs
    1. Why test?
      1. Test-driven development
    2. Unit testing
      1. Assertion methods
      2. Reducing boilerplate and cleaning up
      3. Organizing and running tests
      4. Ignoring broken tests
    3. Testing with pytest
      1. One way to do setup and cleanup
      2. A completely different way to set up variables
      3. Skipping tests with pytest
    4. Imitating expensive objects
    5. How much testing is enough?
    6. Case study
      1. Implementing it
    7. Exercises
    8. Summary
  18. Concurrency
    1. Threads
      1. The many problems with threads
        1. Shared memory
        2. The global interpreter lock
      2. Thread overhead
    2. Multiprocessing
      1. Multiprocessing pools
      2. Queues
      3. The problems with multiprocessing
    3. Futures
    4. AsyncIO
      1. AsyncIO in action
      2. Reading an AsyncIO Future
      3. AsyncIO for networking
      4. Using executors to wrap blocking code
        1. Streams
        2. Executors
      5. AsyncIO clients
    5. Case study
    6. Exercises
    7. Summary
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Python 3 Object-Oriented Programming. - Third Edition
  • Author(s): Dusty Phillips
  • Release date: October 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789615852