Beyond the basic stuff with Python Best practices for writing clean code

Al Sweigart

Book - 2021

"A guide for programmers who already are familiar with the Python programming language that explains how to make code more understandable to other programmers, addresses Python conventions and best practices, and teaches how to use object-oriented programming strategies"--

Saved in:

2nd Floor Show me where

005.133/PYTHON/Sweigart
1 / 1 copies available
Location Call Number   Status
2nd Floor 005.133/PYTHON/Sweigart Checked In
Subjects
Genres
Handbooks and manuals
Published
San Francisco, CA : No Starch Press, Inc [2021]
Language
English
Main Author
Al Sweigart (author)
Item Description
Includes index.
Physical Description
xxv, 352 pages : illustrations ; 24 cm
ISBN
9781593279660
  • Acknowledgments
  • Introduction
  • Who Should Read This Book and Why
  • About This Book
  • Your Programming Journey
  • Part I. Getting Started
  • 1. Dealing with Errors and Asking for Help
  • How to Understand Python Error Messages
  • Examining Tracebacks
  • Searching for Error Messages
  • Preventing Errors with Linters
  • How to Ask for Programming Help
  • Limit Back and Forth by Providing Your Information Upfront
  • State Your Question in the Form of an Actual Question
  • Ask Your Question on the Appropriate Website
  • Summarize Your Question in the Headline
  • Explain What You Want the Code to Do
  • Include the Full Error Message
  • Share Your Complete Code
  • Make Your Code-Readable with Proper Formatting
  • Tell Your Helper What You've Already Tried
  • Describe Your Setup
  • Examples of Asking a Question
  • Summary
  • 2. Environment Setup and the Command Line
  • The Filesystem
  • Paths in Python
  • The Home Directory
  • The Current Working Directory
  • Absolute vs. Relative Paths
  • Programs and Processes
  • The Command Line
  • Opening a Terminal Window
  • Running Programs from the Command Line
  • Using Command Line Arguments
  • Running Python Code from the Command Line with -c
  • Running Python Programs from the Command Line
  • Running the py.exe Program
  • Running Commands from a Python Program
  • Minimizing Typing with Tab Completion
  • Viewing the Command History
  • Working with Common Commands
  • Environment Variables and Path
  • Viewing Environment Variables
  • Working with the Path Environment Variable
  • Changing the Command Line's PATH Environment Variable
  • Permanently Adding Folders to PATH on Windows
  • Permanently Adding Folders to PATH on macOS and Linux
  • Running Python Programs Without the Command Line
  • Running Python Programs on Windows
  • Running Python Programs on macOS
  • Running Python Programs on Ubuntu Linux
  • Summary
  • Part III. Best Practices, Tools, and Techniques
  • 3. Code Formatting with Black
  • How to Lose Friends and Alienate Co-Workers
  • Style Guides and PEP 8
  • Horizontal Spacing
  • Use Space Characters for Indentation
  • Spacing Within a Line
  • Vertical Spacing
  • A Vertical Spacing Example
  • Vertical Spacing Best Practices
  • Black: The Uncompromising Code Formatter
  • Installing Black
  • Running Black from the Command Line
  • Disabling Black for Parts of Your Code
  • Summary
  • 4. Choosing Understandable Names
  • Casing Styles
  • PEP 8's Naming Conventions
  • Appropriate Name Length
  • Too Short Names
  • Too Long Names
  • Make Names Searchable
  • Avoid Jokes, Puns, and Cultural References
  • Don't Overwrite Built-in Names
  • The Worst Possible Variable Names Ever
  • Summary
  • 5. Finding Code Smells
  • Duplicate Code
  • Magic Numbers
  • Commented-Out Code and Dead Code
  • Print Debugging
  • Variables with Numeric Suffixes
  • Classes That Should Just Be Functions or Modules
  • List Comprehensions Within List Comprehensions
  • Empty except Blocks and Poor Error Messages
  • Code Smell Myths
  • Myth: Functions Should Have Only One Return Statement at the End
  • Myth: Functions Should Have at Most One try Statement
  • Myth: Flag Arguments Are Bad
  • Myth: Global Variables Are Bad
  • Myth: Comments Are Unnecessary
  • Summary
  • 6. Writing Pythonic Code
  • The Zen of Python
  • Learning to Love Significant Indentation
  • Commonly Misused Syntax
  • Use enumerate() Instead of range()
  • Use the with Statement Instead of open() and close()
  • Use is to Compare with None Instead of ==.
  • Formatting Strings
  • Use Raw Strings If Your String Has Many Backslashes
  • Format Strings with F-Strings
  • Making Shallow Copies of Lists
  • Pythonic Ways to Use Dictionaries
  • Use get() and Setdefault() with Dictionaries
  • Use Collections.defaultdict for Default Values
  • Use Dictionaries Instead of a switch Statement
  • Conditional Expressions: Python's "Ugly" Ternary Operator
  • Working with Variable Values
  • Chaining Assignment and Comparison Operators
  • Checking Whether a Variable Is One of Many Values
  • Summary
  • 7. Programming Jargon
  • Definitions
  • Python the Language and Python the Interpreter
  • Garbage Collection
  • Literals
  • Keywords
  • Objects, Values, Instances, and Identities
  • Items
  • Mutable and Immutable
  • Indexes, Keys, and Hashes
  • Containers, Sequences, Mapping, and Set Types
  • Dunder Methods and Magic Methods
  • Modules and Packages
  • Collables and First-Class Objects
  • Commonly Confused Terms
  • Statements vs. Expressions
  • Block vs. Clause vs. Body
  • Variable vs. Attribute
  • Function vs. Method
  • Iterable vs. Iterator
  • Syntax vs. Runtime vs. Semantic Errors
  • Parameters vs. Arguments
  • Type Coercion vs. Type Casting
  • Properties vs. Attributes
  • Bytecode vs. Machine Code
  • Script vs. Program, Scripting Language vs. Programming Language
  • Library vs. Framework vs. SDK vs. Engine vs. API
  • Summary
  • Further Reading
  • 8. Common Python Gotchas
  • Don't Add or Delete Items from a List While Looping Over It
  • Don't Copy Mutable Values Without copy.copy() and copy.deepcopy()
  • Don't Use Mutable Values for Default Arguments
  • Don't Build Strings with String Concatenation
  • Don't Expect sort() to Sort Alphabetically
  • Don't Assume Floating-Point Numbers Are Perfectly Accurate
  • Don't Chain Inequality != Operators
  • Don't Forget the Comma in Single-Item Tuples
  • Summary
  • 9. Esoteric Python Oddities
  • Why 256 Is 256 but 257 Is Not 257
  • String Interning
  • Python's Fake Increment and Decrement Operators
  • All of Nothing
  • Boolean Values Are Integer Values
  • Chaining Multiple Kinds of Operators
  • Python's Antigravity Feature
  • Summary
  • Writing Effective Functions
  • Function Names
  • Function Size Trade-Offs
  • Function Parameters and Arguments
  • Default Arguments
  • Using * and ** to Pass Arguments to Functions
  • Using * to Create Variadic Functions
  • Using ** to Create Variadic Functions
  • Using * and ** to Create Wrapper Functions
  • Functional Programming
  • Side Effects
  • Higher-Order Functions
  • Lambda Functions
  • Mapping and Filtering with List Comprehensions
  • Return Values Should Always Have the Same Data Type
  • Raising Exceptions vs. Returning Error Codes
  • Summary
  • 11. Comments, Docstrings, and Type Hints
  • Comments
  • Comment Style
  • Inline Comments
  • Explanatory Comments
  • Summary Comments
  • "Lessons Learned" Comments
  • Legal Comments
  • Professional Comments
  • Codetags and TODO Comments
  • Magic Comments and Source File Encoding
  • Docstrings
  • Type Hints
  • Using Static Analyzers
  • Setting Type Hints for Multiple Types
  • Setting Type Hints for Lists, Dictionaries, and More
  • Backporting Type Hints with Comments
  • Summary
  • 12. Organizing Your Code Projects with Git
  • Git Commits and Repos
  • Using Cookiecutter to Create New Python Projects
  • Installing Git
  • Configuring Your Git Username and Email
  • Installing GUI Git Tools
  • The Git Workflow
  • How Git Keeps Track of File Status
  • Why Stage Files?
  • Creating a Git Repo on Your Computer
  • Adding Files for Git to Track
  • Ignoring Files in the Repo
  • Committing Changes
  • Deleting Files from the Repo
  • Renaming and Moving Files in the Repo
  • Viewing the Commit Log
  • Recovering Old Changes
  • Undoing Uncommitted Local Changes
  • Unstaging a Staged File
  • Rolling Back the Most Recent Commits
  • Rolling Back to a Specific Commit for a Single File
  • Rewriting the Commit History
  • GitHub and the git push Command
  • Pushing an Existing Repository to GitHub
  • Cloning a Repo from an Existing GitHub Repo
  • Summary
  • 13. Measuring Performance and Big O Algorithm Analysis
  • The timeit Module
  • The cProfile Profiler
  • Big O Algorithm Analysis
  • Big O Orders
  • A Bookshelf Metaphor for Big O Orders
  • Big O Measures the Worst-Case Scenario
  • Determining the Big O Order of Your Code
  • Why Lower Orders and Coefficients Don't Matter
  • Big O Analysis Examples
  • The Big O Order of Common Function Calls
  • Analyzing Big O at a Glance
  • Big O Doesn't Matter When n Is Small, and n Is Usually Small
  • Summary
  • 14. Practice Projects
  • The Tower of Hanoi
  • The Output
  • The Source Code
  • Writing the Code
  • Four-in-a-Raw
  • The Output
  • The Source Code
  • Writing the Code
  • Summary
  • Part III. Object-Oriented Python
  • 15. Object-Oriented Programming and Classes
  • Real-World Analogy: Filling Out a Form
  • Creating Objects from Classes
  • Creating a Simple Class: WizCoin
  • Methods, _init_(), and self
  • Attributes
  • Private Attributes and Private Methods
  • The Type() Function and _qualname_Attribute
  • Non-OOP vs. OOP Examples: Tic-Tac-Toe
  • Designing Classes for the Real World Is Hard
  • Summary
  • 16. Object-Oriented Programming and Inheritance
  • How Inheritance Works
  • Overriding Methods
  • The super() Function
  • Favor Composition Over Inheritance
  • Inheritance's Downside
  • The isinstance() and issubclass() Functions
  • Class Methods
  • Class Attributes
  • Static Methods
  • When to Use Class and Static Object-Oriented Features
  • Object-Oriented Buzzwords
  • Encapsulation
  • Polymorphism
  • When Not to Use Inheritance
  • Multiple Inheritance
  • Method Resolution Order
  • Summary
  • 17. Pythonic OOP: Properties and Dunder Methods
  • Properties
  • Turning an Attribute into a Property
  • Using Setters to Validale Data!
  • Read-Only Properties
  • When to Use Properties
  • Python's Dunder Methods
  • String Representation Dunder Methods
  • Numeric Dunder Methods
  • Reflected Numeric Dunder Methods
  • In-Place Augmented Assignment Dunder Methods
  • Comparison Dunder Methods
  • Summary
  • Index