Object-Oriented Python
Master OOP by Building Games and GUIs
by Irv Kalb
Object-Oriented Python. Copyright 2022 by Irv Kalb.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
First printing
25 24 23 22 21 1 2 3 4 5 6 7 8 9
ISBN-13: 978-1-7185-0206-2 (print)
ISBN-13: 978-1-7185-0207-9 (ebook)
Publisher: William Pollock
Managing Editor: Jill Franklin
Production Manager: Rachel Monaghan
Production Editor: Kate Kaminski
Developmental Editor: Liz Chadwick
Cover Illustrator: James L. Barry
Interior Design: Octopod Studios
Technical Reviewer: Monte Davidoff
Copyeditor: Rachel Head
Compositor: Maureen Forys, Happenstance Type-O-Rama
Proofreader: Paula L. Fleming
Indexer: Valerie Haynes Perry
The following images are reproduced with permission:
Figure 2-1, photo by David Benbennick, printed under the Creative Commons Attribution-Share Alike 3.0 Unported license, https://creativecommons.org/licenses/by-sa/3.0/deed.en.
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900;
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Names: Kalb, Irv, author.
Title: Object-oriented Python: master OOP by building games and GUIs / Irv Kalb.
Description: San Francisco : No Starch Press, [2021] | Includes index. |
Identifiers: LCCN 2021044174 (print) | LCCN 2021044175 (ebook) | ISBN
9781718502062 (print) | ISBN 9781718502079 (ebook)
Subjects: LCSH: Object-oriented programming (Computer science) | Python
(Computer program language)
Classification: LCC QA76.64 .K3563 2021 (print) | LCC QA76.64 (ebook) |
DDC 005.1/17--dc23
LC record available at https://lccn.loc.gov/2021044174
LC ebook record available at https://lccn.loc.gov/2021044175
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To my wonderful wife, Doreen.
You are the glue that keeps our family together.
Many years ago, I said, I do, but what I meant was, I will.
About the Author
Irv Kalb is an adjunct professor at UCSC Silicon Valley Extension and the University of Silicon Valley (formerly Cogswell Polytechnical College), where he teaches introductory and object-oriented programming courses in Python. Irv has a bachelors and a masters degree in computer science, has been using object-oriented programming for over 30 years in a number of different computer languages, and has been teaching for over 10 years. He has decades of experience developing software, with a focus on educational software. As Furry Pants Productions, he and his wife created and shipped two edutainment CD-ROMs based on the character Darby the Dalmatian. Irv is also the author of Learn to Program with Python 3: A Step-by-Step Guide to Programming (Apress).
Irv was heavily involved in the early development of the sport of Ultimate Frisbee. He led the effort of writing many versions of the official rule book and co-authored and self-published the first book on the sport, Ultimate: Fundamentals of the Sport.
About the Technical Reviewer
Monte Davidoff is an independent software development consultant. His areas of expertise include DevOps and Linux. Monte has been programming in Python for over 20 years. He has used Python to develop a variety of software, including business-critical applications and embedded software.
Acknowledgments
I would like to thank the following people, who helped make this book possible:
- Al Sweigart, for getting me started in the use of pygame (especially with his Pygbutton code) and for allowing me to use the concept of his Dodger game.
- Monte Davidoff, who was instrumental in helping me get the source code and documentation of that code to build correctly through the use of GitHub, Sphinx, and ReadTheDocs. He worked miracles using a myriad of tools to wrestle the appropriate files into submission.
- Monte Davidoff (yes, the same guy), for being an outstanding technical reviewer. Monte made excellent technical and writing suggestions throughout the book, and many of the code examples are more Pythonic and more OOP-ish because of his comments.
- Tep Sathya Khieu, who did a stellar job of drawing all the original diagrams for this book. I am not an artist (I dont even play one on TV). Tep was able to take my primitive pencil sketches and turn them into clear, consistent pieces of art.
- Harrison Yung, Kevin Ly, and Emily Allis, for their contributions of artwork in some of the game art.
- The early reviewers, Illya Katsyuk, Jamie Kalb, Gergana Angelova, and Joe Langmuir, who found and corrected many typos and made excellent suggestions for modifications and clarifications.
- All the editors who worked on this book: Liz Chadwick (developmental editor), Rachel Head (copyeditor), and Kate Kaminski (production editor). They all made huge contributions by questioning and often rewording and reorganizing some of my explanations of concepts. They were also extremely helpful in adding and removing commas [do I need one here?] and lengthening my sentences as I am doing here to make sure that the point comes across cleanly (OK, Ill stop!). Im afraid that Ill never understand when to use which versus that, or when to use a comma and when to use a dash, but Im glad that they know! Thanks also to Maureen Forys (compositor) for her valuable contributions to the finished product.
- All the students who have been in my classes over the years at the UCSC Silicon Valley Extension and at the University of Silicon Valley (formerly Cogswell Polytechnical College). Their feedback, suggestions, smiles, frowns, light-bulb moments, frustrations, knowing head nods, and even thumbs-up (in Zoom classes during the COVID era) were extremely helpful in shaping the content of this book and my overall teaching style.
- Finally, my family, who supported me through the long process of writing, testing, editing, rewriting, editing, debugging, editing, rewriting, editing (and so on) this book and the associated code. I couldnt have done it without them. I wasnt sure if we had enough books in our library, so I wrote another one!
Introduction