Python 3 Object-oriented Programming - Second Edition

Book description

Unleash the power of Python 3 objects

About This Book

  • Stop writing scripts and start architecting programs
  • Learn the latest Python syntax and libraries
  • A practical, hands-on tutorial that teaches you all about abstract design patterns and how to implement them in Python 3

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 object-oriented programming in Python to design software, this is the book for you.

What You Will Learn

  • Implement objects in Python by creating classes and defining methods
  • Separate related objects into a taxonomy of classes and describe the properties and behaviors of those objects via the class interface
  • 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
  • Grasp common concurrency techniques and pitfalls in Python 3
  • Exploit object-oriented programming in key Python technologies such as Kivy and Django.
  • Object-oriented programming concurrently with asyncio

In Detail

Python 3 is more versatile and easier to use than ever. It runs on all major platforms in a huge array of use cases. Coding in Python minimizes development time and increases productivity in comparison to other languages. Clean, maintainable code is easy to both read and write using Python's clear, concise syntax.

Object-oriented programming is a popular design paradigm in which data and behaviors are encapsulated in such a way that they can be manipulated together. Many modern programming languages utilize the powerful concepts behind object-oriented programming and Python is no exception.

Starting with a detailed analysis of object-oriented analysis and design, you will use the Python programming language to clearly grasp key concepts from the object-oriented paradigm. This book fully explains classes, data encapsulation, inheritance, polymorphism, abstraction, and exceptions with an emphasis on when you can use each principle to develop well-designed software.

You'll get an in-depth analysis of many common object-oriented design patterns that are more suitable to Python's unique style. This book will not just teach Python syntax, but will also build your confidence in how to program.

You will also learn how to create maintainable applications by studying higher level design patterns. Following this, you'll learn 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 will be introduced in the book. After you discover the joy of unit testing and just how easy it can be, you'll study higher level libraries such as database connectors and GUI toolkits and learn how they uniquely apply object-oriented principles. You'll learn how these principles will allow you to make greater use of key members of the Python eco-system such as Django and Kivy.

This new edition includes all the topics that made Python 3 Object-oriented Programming an instant Packt classic. It's also packed with updated content to reflect recent changes in the core Python library and covers modern third-party packages that were not available on the Python 3 platform when the book was first published.

Style and approach

Throughout the book you will learn key object-oriented programming techniques demonstrated by comprehensive case studies in the context of a larger project.

Table of contents

  1. Python 3 Object-oriented Programming Second Edition
    1. Table of Contents
    2. Python 3 Object-oriented Programming Second Edition
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Introduction to the second edition
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. 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
    10. 2. 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 the modules
          1. Absolute imports
          2. Relative imports
      3. Organizing module contents
      4. Who can access my data?
      5. Third-party libraries
      6. Case study
      7. Exercises
      8. Summary
    11. 3. 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
    12. 4. 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
    13. 5. When to Use Object-oriented Programming
      1. Treat objects as objects
      2. Adding behavior 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
    14. 6. Python Data Structures
      1. Empty objects
      2. Tuples and named tuples
        1. Named tuples
      3. Dictionaries
        1. Dictionary use cases
        2. Using defaultdict
          1. Counter
      4. Lists
        1. Sorting lists
      5. Sets
      6. Extending built-ins
      7. Queues
        1. FIFO queues
        2. LIFO queues
        3. Priority queues
      8. Case study
      9. Exercises
      10. Summary
    15. 7. 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
    16. 8. Strings and Serialization
      1. Strings
        1. String manipulation
        2. String formatting
          1. Escaping braces
          2. Keyword arguments
          3. Container lookups
          4. Object lookups
          5. Making it look right
        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. Serializing objects
        1. Customizing pickles
        2. Serializing web objects
      4. Case study
      5. Exercises
      6. Summary
    17. 9. 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
    18. 10. 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
      6. The template pattern
        1. A template example
      7. Exercises
      8. Summary
    19. 11. 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
    20. 12. 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 py.test
        1. One way to do setup and cleanup
        2. A completely different way to set up variables
        3. Skipping tests with py.test
      4. Imitating expensive objects
      5. How much testing is enough?
      6. Case study
        1. Implementing it
      7. Exercises
      8. Summary
    21. 13. 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
        5. Streams
          1. Executors
      5. Case study
      6. Exercises
      7. Summary
    22. Index

Product information

  • Title: Python 3 Object-oriented Programming - Second Edition
  • Author(s): Dusty Phillips
  • Release date: August 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784398781