Object-Oriented Python

Book description

Object-Oriented Python (OOP) is a paradigm that combines data and code into cohesive units, allowing you to think differently about computational problems and solve them in a highly reusable way. Aimed at intermediate-level programmers, Object-Oriented Python is a hands-on tutorial that goes deep into the core tenets of OOP, showing you how to use encapsulation, polymorphism, and inheritance to write games and apps using Python.

The book begins by demonstrating key problems inherent in procedural programming, then guides you through the basics of creating classes and objects in Python. You’ll build on this groundwork by developing buttons, text fields, and other GUI elements that are standard in event-driven environments. You’ll also use many real-world code examples and two pygame-based packages to help turn theory into practice, enabling you to easily write interactive games and applications complete with GUI widgets, animations, multiple scenes, and reusable game logic. In the final chapter, you’ll bring it all together by building a fully functional video game that incorporates many of the OOP techniques and GUI elements covered in the book.

You’ll learn how to:

•Create and manage multiple objects using an object manager object
•Use encapsulation to hide the inner details of objects from client code
•Use polymorphism to define one interface and implement it in multiple classes
•Apply inheritance to build on existing code

Object-Oriented Python is a visual, intuitive guide to fully understanding how OOP operates and how you can use it to make your code more maintainable, readable, and efficient—without sacrificing functionality.

Table of contents

  1. Title Page
  2. Copyright
  3. Dedication
  4. About the Author
  5. Acknowledgments
  6. Introduction
    1. Who Is This Book For?
    2. Python Version(s) and Installation
    3. How Will I Explain OOP?
    4. What’s in the Book
    5. Development Environments
    6. Widgets and Example Games
  7. Part I: Introducing Object-oriented Programming
    1. Chapter 1: Procedural Python Examples
      1. Higher or Lower Card Game
        1. Representing the Data
        2. Implementation
        3. Reusable Code
      2. Bank Account Simulations
        1. Analysis of Required Operations and Data
        2. Implementation 1—Single Account Without Functions
        3. Implementation 2—Single Account with Functions
        4. Implementation 3—Two Accounts
        5. Implementation 4—Multiple Accounts Using Lists
        6. Implementation 5—List of Account Dictionaries
      3. Common Problems with Procedural Implementation
      4. Object-Oriented Solution—First Look at a Class
      5. Summary
    2. Chapter 2: Modeling Physical Objects with Object-Oriented Programming
      1. Building Software Models of Physical Objects
        1. State and Behavior: Light Switch Example
      2. Classes, Objects, and Instantiation
        1. Writing a Class in Python
        2. Scope and Instance Variables
        3. Differences Between Functions and Methods
        4. Creating an Object from a Class
        5. Calling Methods of an Object
        6. Creating Multiple Instances from the Same Class
        7. Python Data Types Are Implemented as Classes
        8. Definition of an Object
      3. Building a Slightly More Complicated Class
      4. Representing a More Complicated Physical Object as a Class
        1. Passing Arguments to a Method
        2. Multiple Instances
        3. Initialization Parameters
      5. Classes in Use
      6. OOP as a Solution
      7. Summary
    3. Chapter 3: Mental Models of Objects and the Meaning of “self”
      1. Revisiting the DimmerSwitch Class
      2. High-Level Mental Model #1
      3. A Deeper Mental Model #2
      4. What Is the Meaning of “self”?
      5. Summary
    4. Chapter 4: Managing Multiple Objects
      1. Bank Account Class
      2. Importing Class Code
      3. Creating Some Test Code
        1. Creating Multiple Accounts
        2. Multiple Account Objects in a List
        3. Multiple Objects with Unique Identifiers
        4. Building an Interactive Menu
      4. Creating an Object Manager Object
        1. Building the Object Manager Object
        2. Main Code That Creates an Object Manager Object
      5. Better Error Handling with Exceptions
        1. try and except
        2. The raise Statement and Custom Exceptions
      6. Using Exceptions in Our Bank Program
        1. Account Class with Exceptions
        2. Optimized Bank Class
        3. Main Code That Handles Exceptions
      7. Calling the Same Method on a List of Objects
      8. Interface vs. Implementation
      9. Summary
  8. Part II: Graphical User interfaces with Pygame
    1. Chapter 5: Introduction to Pygame
      1. Installing Pygame
      2. Window Details
        1. The Window Coordinate System
        2. Pixel Colors
      3. Event-Driven Programs
      4. Using Pygame
        1. Bringing Up a Blank Window
        2. Drawing an Image
        3. Detecting a Mouse Click
        4. Handling the Keyboard
        5. Creating a Location-Based Animation
        6. Using Pygame rects
      5. Playing Sounds
        1. Playing Sound Effects
        2. Playing Background Music
      6. Drawing Shapes
        1. Reference for Primitive Shapes
      7. Summary
    2. Chapter 6: Object-Oriented Pygame
      1. Building the Screensaver Ball with OOP Pygame
        1. Creating a Ball Class
        2. Using the Ball Class
        3. Creating Many Ball Objects
        4. Creating Many, Many Ball Objects
      2. Building a Reusable Object-Oriented Button
        1. Building a Button Class
        2. Main Code Using a SimpleButton
        3. Creating a Program with Multiple Buttons
      3. Building a Reusable Object-Oriented Text Display
        1. Steps to Display Text
        2. Creating a SimpleText Class
      4. Demo Ball with SimpleText and SimpleButton
      5. Interface vs. Implementation
      6. Callbacks
        1. Creating a Callback
        2. Using a Callback with SimpleButton
      7. Summary
    3. Chapter 7: Pygame GUI Widgets
      1. Passing Arguments into a Function or Method
        1. Positional and Keyword Parameters
        2. Additional Notes on Keyword Parameters
        3. Using None as a Default Value
        4. Choosing Keywords and Default Values
        5. Default Values in GUI Widgets
      2. The pygwidgets Package
        1. Setting Up
        2. Overall Design Approach
        3. Adding an Image
        4. Adding Buttons, Checkboxes, and Radio Buttons
        5. Text Output and Input
        6. Other pygwidgets Classes
        7. pygwidgets Example Program
      3. The Importance of a Consistent API
      4. Summary
  9. Part III: Encapsulation, Polymorphism, and Inheritance
    1. Chapter 8: Encapsulation
      1. Encapsulation with Functions
      2. Encapsulation with Objects
        1. Objects Own Their Data
      3. Interpretations of Encapsulation
        1. Direct Access and Why You Should Avoid It
        2. Strict Interpretation with Getters and Setters
        3. Safe Direct Access
      4. Making Instance Variables More Private
        1. Implicitly Private
        2. More Explicitly Private
      5. Decorators and @property
      6. Encapsulation in pygwidgets Classes
      7. A Story from the Real World
      8. Abstraction
      9. Summary
    2. Chapter 9: Polymorphism
      1. Sending Messages to Real-World Objects
      2. A Classic Example of Polymorphism in Programming
      3. Example Using Pygame Shapes
        1. The Square Shape Class
        2. The Circle and Triangle Shape Classes
        3. The Main Program Creating Shapes
        4. Extending a Pattern
      4. pygwidgets Exhibits Polymorphism
      5. Polymorphism for Operators
        1. Magic Methods
        2. Comparison Operator Magic Methods
        3. A Rectangle Class with Magic Methods
        4. Main Program Using Magic Methods
        5. Math Operator Magic Methods
        6. Vector Example
      6. Creating a String Representation of Values in an Object
      7. A Fraction Class with Magic Methods
      8. Summary
    3. Chapter 10: Inheritance
      1. Inheritance in Object-Oriented Programming
      2. Implementing Inheritance
      3. Employee and Manager Example
        1. Base Class: Employee
        2. Subclass: Manager
        3. Test Code
      4. The Client’s View of a Subclass
      5. Real-World Examples of Inheritance
        1. InputNumber
        2. DisplayMoney
        3. Example Usage
      6. Multiple Classes Inheriting from the Same Base Class
      7. Abstract Classes and Methods
      8. How pygwidgets Uses Inheritance
      9. Class Hierarchy
      10. The Difficulty of Programming with Inheritance
      11. Summary
    4. Chapter 11: Managing Memory Used by Objects
      1. Object Lifetime
        1. Reference Count
        2. Garbage Collection
      2. Class Variables
        1. Class Variable Constants
        2. Class Variables for Counting
      3. Putting It All Together: Balloon Sample Program
        1. Module of Constants
        2. Main Program Code
        3. Balloon Manager
        4. Balloon Class and Objects
      4. Managing Memory: Slots
      5. Summary
  10. Part IV: Using OOP in Game Development
    1. Chapter 12: Card Games
      1. The Card Class
      2. The Deck Class
      3. The Higher or Lower Game
        1. Main Program
        2. Game Object
      4. Testing with __name__
      5. Other Card Games
        1. Blackjack Deck
        2. Games with Unusual Card Decks
      6. Summary
    2. Chapter 13: Timers
      1. Timer Demonstration Program
      2. Three Approaches for Implementing Timers
        1. Counting Frames
        2. Timer Event
        3. Building a Timer by Calculating Elapsed Time
      3. Installing pyghelpers
      4. The Timer Class
      5. Displaying Time
        1. CountUpTimer
        2. CountDownTimer
      6. Summary
    3. Chapter 14: Animation
      1. Building Animation Classes
        1. SimpleAnimation Class
        2. SimpleSpriteSheetAnimation Class
        3. Merging Two Classes
      2. Animation Classes in pygwidgets
        1. Animation Class
        2. SpriteSheetAnimation Class
        3. Common Base Class: PygAnimation
        4. Example Animation Program
      3. Summary
    4. Chapter 15: Scenes
      1. The State Machine Approach
      2. A pygame Example with a State Machine
      3. A Scene Manager for Managing Many Scenes
      4. A Demo Program Using a Scene Manager
        1. The Main Program
        2. Building the Scenes
        3. A Typical Scene
      5. Rock, Paper, Scissors Using Scenes
      6. Communication Between Scenes
        1. Requesting Information from a Target Scene
        2. Sending Information to a Target Scene
        3. Sending Information to All Scenes
        4. Testing Communications Among Scenes
      7. Implementation of the Scene Manager
        1. run() Method
        2. Main Methods
        3. Communication Between Scenes
      8. Summary
    5. Chapter 16: Full Game: Dodger
      1. Modal Dialogs
        1. Yes/No and Alert Dialogs
        2. Answer Dialogs
      2. Building a Full Game: Dodger
        1. Game Overview
        2. Implementation
        3. Extensions to the Game
      3. Summary
    6. Chapter 17: Design Patterns and Wrap-Up
      1. Model View Controller
        1. File Display Example
        2. Statistical Display Example
        3. Advantages of the MVC Pattern
      2. Wrap-Up
  11. Index

Product information

  • Title: Object-Oriented Python
  • Author(s): Irv Kalb
  • Release date: January 2022
  • Publisher(s): No Starch Press
  • ISBN: 9781718502062