Uncover modern Python with this guide to Python data structures, design patterns, and effective object-oriented techniquesKey FeaturesIn-depth analysis of many common object-oriented design patterns that are more suitable to Python's unique styleLearn the latest Python syntax and librariesExplore abstract design patterns and implement them in Python 3.8Book DescriptionObject-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 learnImplement objects in Python by creating classes and defining methodsGrasp common concurrency techniques and pitfalls in Python 3Extend class functionality using inheritanceUnderstand when to use object-oriented features, and more importantly when not to use themDiscover what design patterns are and why they are different in PythonUncover the simplicity of unit testing and why it s so important in PythonExplore concurrent object-oriented programmingWho this book is forIf 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. Cover Title Page Copyright and Credits Packt Upsell Contributors Table of Contents Preface Chapter 1: Object-Oriented Design Introducing object-oriented Objects and classes Specifying attributes and behaviors Data describes objects Behaviors are actions Hiding details and creating the public interface Composition Inheritance Inheritance provides abstraction Multiple inheritance Case study Exercises Summary Chapter 2: Objects in Python Creating Python classes Adding attributes Making it do something Talking to yourself More arguments Initializing the object Explaining yourself Modules and packages Organizing modules Absolute imports Relative imports Organizing module content Who can access my data? Third-party libraries Case study Exercises Summary Chapter 3: When Objects Are Alike Basic inheritance Extending built-ins Overriding and super Multiple inheritance The diamond problem Different sets of arguments Polymorphism Abstract base classes Using an abstract base class Creating an abstract base class Demystifying the magic Case study Exercises Summary Chapter 4: Expecting the Unexpected Raising exceptions Raising an exception The effects of an exception Handling exceptions The exception hierarchy Defining our own exceptions Case study Exercises Summary Chapter 5: When to Use Object-Oriented Programming Treat objects as objects Adding behaviors to class data with properties Properties in detail Decorators – another way to create properties Deciding when to use properties Manager objects Removing duplicate code In practice Case study Exercises Summary Chapter 6: Python Data Structures Empty objects Tuples and named tuples Named tuples Dataclasses Dictionaries Dictionary use cases Using defaultdict Counter Lists Sorting lists Sets Extending built-in functions Case study Exercises Summary Chapter 7: Python Object-Oriented Shortcuts Python built-in functions The len() function Reversed Enumerate File I/O Placing it in context An alternative to method overloading Default arguments Variable argument lists Unpacking arguments Functions are objects too Using functions as attributes Callable objects Case study Exercises Summary Chapter 8: Strings and Serialization Strings String manipulation String formatting Escaping braces f-strings can contain Python code Making it look right Custom formatters The format method Strings are Unicode Converting bytes to text Converting text to bytes Mutable byte strings Regular expressions Matching patterns Matching a selection of characters Escaping characters Matching multiple characters Grouping patterns together Getting information from regular expressions Making repeated regular expressions efficient Filesystem paths Serializing objects Customizing pickles Serializing web objects Case study Exercises Summary Chapter 9: The Iterator Pattern Design patterns in brief Iterators The iterator protocol Comprehensions List comprehensions Set and dictionary comprehensions Generator expressions Generators Yield items from another iterable Coroutines Back to log parsing Closing coroutines and throwing exceptions The relationship between coroutines, generators, and functions Case study Exercises Summary Chapter 10: Python Design Patterns I The decorator pattern A decorator example Decorators in Python The observer pattern An observer example The strategy pattern A strategy example Strategy in Python The state pattern A state example State versus strategy State transition as coroutines The singleton pattern Singleton implementation Module variables can mimic singletons The template pattern A template example Exercises Summary Chapter 11: Python Design Patterns II The adapter pattern The facade pattern The flyweight pattern The command pattern The abstract factory pattern The composite pattern Exercises Summary Chapter 12: Testing Object-Oriented Programs Why test? Test-driven development Unit testing Assertion methods Reducing boilerplate and cleaning up Organizing and running tests Ignoring broken tests Testing with pytest One way to do setup and cleanup A completely different way to set up variables Skipping tests with pytest Imitating expensive objects How much testing is enough? Case study Implementing it Exercises Summary Chapter 13: Concurrency Threads The many problems with threads Shared memory The global interpreter lock Thread overhead Multiprocessing Multiprocessing pools Queues The problems with multiprocessing Futures AsyncIO AsyncIO in action Reading an AsyncIO Future AsyncIO for networking Using executors to wrap blocking code Streams Executors AsyncIO clients Case study Exercises Summary Other Books You May Enjoy Index 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