Beyond the Basic Stuff with Python
Total Page:16
File Type:pdf, Size:1020Kb
BEYOND THE BASIC STUFF WITH PYTHON BEST PRACTICES FOR WRITING CLEAN CODE AL SWEIGART BEYOND THE BASIC STUFF WITH PYTHON BEYOND THE BASIC STUFF WITH PYTHON Best Practices for Writing Clean Code Al Sweigart San Francisco BEYOND THE BASIC STUFF WITH PYTHON. Copyright © 2021 by Al Sweigart. 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. ISBN-13: 978-1-59327-966-0 (print) ISBN-13: 978-1-59327-967-7 (ebook) Publisher: William Pollock Executive Editor: Barbara Yien Production Editor: Maureen Forys, Happenstance Type-O-Rama Developmental Editor: Frances Saux Cover Design: Octopod Studios Interior Design: Octopod Studios Cover Illustration: Josh Ellingson Technical Reviewer: Kenneth Love Copyeditor: Anne Marie Walker Compositor: Happenstance Type-O-Rama Proofreader: Rachel Monaghan Indexer: Valerie Perry 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; [email protected] www.nostarch.com Library of Congress Cataloging-in-Publication Data Library of Congress Cataloging-in-Publication Data Names: Sweigart, Al, author. Title: Beyond the basic stuff with python : best practices for writing clean code / Al Sweigart. Description: San Francisco, CA : No Starch Press, Inc., [2021] | Includes index. Identifiers: LCCN 2020034287 (print) | LCCN 2020034288 (ebook) | ISBN 9781593279660 (paperback) | ISBN 9781593279677 (ebook) Subjects: LCSH: Python (Computer program language) | Computer programming. Classification: LCC QA76.73.P98 S943 2021 (print) | LCC QA76.73.P98 (ebook) | DDC 005.13/3—dc23 LC record available at https://lccn.loc.gov/2020034287 LC ebook record available at https://lccn.loc.gov/2020034288 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. For my nephew Jack About the Author Al Sweigart is a software developer and tech book author living in Seattle. Python is his favorite programming language, and he is the developer of sev- eral open source modules for it. His other books are freely available under a Creative Commons license on his website at https://www.inventwithpython.com/. His cat Zophie weighs 11 pounds. About the Technical Reviewer Kenneth Love is a programmer, teacher, and conference organizer. He is a Django contributor and PSF Fellow, and currently works as a tech lead and software engineer for O’Reilly Media. BRIEF CONTENTS Acknowledgments . xix Acknowledgments . xix Introduction . xxi Introduction . xxi PART 1: GETTING STARTED ......................1 PART I: GETTING STARTED .............................1 Chapter 1: Dealing with Errors and Asking for Help . 3 Chapter 1: Dealing with Errors and Asking for Help . 3 Chapter 2: Environment Setup and the Command Line . 17 Chapter 2: Environment Setup and the Command Line . 17 PART 2: BEST PRACTICES, TOOLS, AND TECHNIQUES ..43 PART II: BEST PRACTICES, TOOLS, AND TECHNIQUES ........43 Chapter 3: Code Formatting with Black . 45 Chapter 3: Code Formatting with Black . 45 Chapter 4: Choosing Understandable Names . 59 Chapter 4: Choosing Understandable Names . 59 Chapter 5: Finding Code Smells . 69 Chapter 5: Finding Code Smells . 69 Chapter 6: Writing Pythonic Code . 87 Chapter 6: Writing Pythonic Code . 87 Chapter 7: Programming Jargon . 107 Chapter 7: Programming Jargon . 107 Chapter 8: Common Python Gotchas . 133 Chapter 8: Common Python Gotchas . 133 Chapter 9: Esoteric Python Oddities . 153 Chapter 9: Esoteric Python Oddities . 153 Chapter 10: Writing Effective Functions . 161 Chapter 10: Writing Effective Functions . 161 Chapter 11: Comments, Docstrings, and Type Hints . 181 Chapter 11: Comments, Docstrings, and Type Hints . 181 Chapter 12: Organizing Your Code Projects with Git . 199 Chapter 12: Organizing Your Code Projects with Git . 199 Chapter 13: Measuring Performance and Big O Algorithm Analysis . 225 Chapter 13: Measuring Performance and Big O Algorithm Analysis . .225 Chapter 14: Practice Projects . 247 Chapter 14: Practice Projects . 247 PART 3: OBJECT-ORIENTED PYTHON ............273 PART III: OBJECT-ORIENTED PYTHON . 273 Chapter 15: Object-Oriented Programming and Classes . 275 Chapter 15: Object-Oriented Programming and Classes . 275 Chapter 16: Object-Oriented Programming and Inheritance . 293 Chapter 16: Object-Oriented Programming and Inheritance . 293 Chapter 17: Pythonic OOP: Properties and Dunder Methods . 315 Chapter 17: Pythonic OOP: Properties and Dunder Methods . 315 Index . 339 Index . 339 CONTENTS IN DETAIL CONTENTS IN DETAIL About the Author . vii About the Technical Reviewer . vii ACKNOWLEDGMENTS xix ACKNOWLEDGMENTS XIX INTRODUCTION xxi INTRODUCTION XXI Who Should Read This Book and Why . xxii Who Should Read This Book and Why . xxii About This Book . xxii About This Book . xxii Your Programming Journey . xxiv Your Programming Journey . xxiv PART I: GETTING STARTED 1 PART 1 1 DEALING WITH ERRORS AND ASKING FOR HELP 3 How to Understand Python Error Messages . 4 GETTING STARTED 1 Examining Tracebacks . 4 Searching for Error Messages . 7 1 Preventing Errors with Linters . 8 How to Ask for Programming Help . 9 DEALING WITH ERRORS AND ASKING FOR HELP 3 Limit Back and Forth by Providing Your Information Upfront . 10 State Your Question in the Form of an Actual Question . 10 How to Understand Python Error Messages . 4 Ask Your Question on the Appropriate Website . 10 Examining Tracebacks . 4 Summarize Your Question in the Headline . 11 Searching for Error Messages . 7 Explain What You Want the Code to Do . 11 Preventing Errors with Linters . 8 Include the Full Error Message . 11 How to Ask for Programming Help . 9 Share Your Complete Code . 11 Limit Back and Forth by Providing Your Information Upfront . 10 Make Your Code Readable with Proper Formatting . 12 State Your Question in the Form of an Actual Question . 10 Tell Your Helper What You’ve Already Tried . 13 Ask Your Question on the Appropriate Website . 10 Describe Your Setup . 13 Summarize Your Question in the Headline . 11 Examples of Asking a Question . 14 Explain What You Want the Code to Do . 11 Summary . 14 Include the Full Error Message . 11 Share Your Complete Code . 11 2 Make Your Code Readable with Proper Formatting . 12 ENVIRONMENT SETUP AND THE COMMAND LINE 17 Tell Your Helper What You’ve Already Tried . 13 Describe Your Setup . 13 The Filesystem . 18 Examples of Asking a Question . 14 Paths in Python . 18 Summary . 14 The Home Directory . 19 The Current Working Directory . 19 2 Absolute vs . Relative Paths . 20 Programs and Processes . 21 The Command Line . 22 ENVIRONMENT SETUP AND THE COMMAND LINE 17 Opening a Terminal Window . 23 The Filesystem . 18 Running Programs from the Command Line . 23 Paths in Python . 18 Using Command Line Arguments . 24 The Home Directory . 19 Running Python Code from the Command Line with -c . 26 The Current Working Directory . 19 Running Python Programs from the Command Line . 26 Absolute vs . Relative Paths . 20 Programs and Processes . 21 xi The Command Line . 22 Opening a Terminal Window . 23 Running Programs from the Command Line . 23 Using Command Line Arguments . 24 Running Python Code from the Command Line with -c . 26 Running Python Programs from the Command Line . 26 Running the py .exe Program . 26 Running Commands from a Python Program . 27 Minimizing Typing with Tab Completion . 27 Running the py .exe Program . 26 Running Commands from a Python Program . 27 Minimizing Typing with Tab Completion . 27 Viewing the Command History . 28 Working with Common Commands . 28 Environment Variables and PATH . 35 Viewing Environment Variables . 36 Working with the PATH Environment Variable . 36 Changing the Command Line’s PATH Environment Variable . 37 Permanently Adding Folders to PATH on Windows . 38 Permanently Adding Folders to PATH on macOS and Linux . 39 Running Python Programs Without the Command Line . 39 Running Python Programs on Windows . 40 Running Python Programs on macOS . 41 Running Python Programs on Ubuntu Linux . 41 Summary . 42 PART III: BEST PRACTICES, TOOLS, AND TECHNIQUES 43 3 CODE FORMATTING WITH BLACK 45 How to Lose Friends and Alienate Co-Workers . 46 Style Guides and PEP 8 . 46 Horizontal Spacing . 47 Use Space Characters for Indentation . 47 Spacing Within a Line . 48 Vertical Spacing . 51 A Vertical Spacing Example . 51 Vertical Spacing Best Practices . 52 Black: The Uncompromising Code Formatter . 53 Installing Black . 54 Running Black from the Command Line . 54 Disabling Black for Parts of Your Code ..