M A N N I N G Yong Cui 63 techniques to improve your Python code
Why Python? Cross-platform Open source community support Resourceful libraries Quick prototyping Expressive and readable syntax Why do we learn Python?
Python How-To 63 TECHNIQUES TO IMPROVE YOUR PYTHON CODE YONG CUI M A N N I N G SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2023 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein. Manning Publications Co. Development editor: Marina Michaels 20 Baldwin Road Technical development editor: René van den Berg PO Box 761 Review editor: Aleksandar Dragosavljević Shelter Island, NY 11964 Production editor: Keri Hales Copy editor: Keir Simpson Proofreader: Melody Dolab Technical proofreaders: Ignacio Beltran Torres and Walter Alexander Mata Lopez Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617299742 Printed in the United States of America
To my wife, Tingting Gu, who sat next to me on numerous late nights while I was writing this book.
iv contents preface xiv acknowledgments xvi about this book xvii about the author xxi about the cover illustration xxii 1 Developing a pragmatic learning strategy 1 1.1 Aiming at becoming a pragmatic programmer 2 Focusing on writing readable Python code 2 ■ Considering maintainability even before you write any code 3 1.2 What Python can do well or as well as other languages 4 1.3 What Python can’t do or can’t do well 6 1.4 What you’ll learn in this book 6 Focusing on domain-independent knowledge 6 ■ Solving problems through synthesis 8 ■ Learning skills in context 9
CONTENTS v PART 1 USING BUILT-IN DATA MODELS.................................11 2 Processing and formatting strings 13 2.1 How do I use f-strings for string interpolation and formatting? 14 Formatting strings before f-strings 14 ■ Using f-strings to interpolate variables 15 ■ Using f-strings to interpolate expressions 16 ■ Applying specifiers to format f-strings 18 Discussion 22 ■ Challenge 23 2.2 How do I convert strings to retrieve the represented data? 23 Checking whether strings represent alphanumeric values 24 Casting strings to numbers 25 ■ Evaluating strings to derive their represented data 27 ■ Discussion 29 ■ Challenge 29 2.3 How do I join and split strings? 29 Joining strings with whitespaces 30 ■ Joining strings with any delimiters 31 ■ Splitting strings to create a list of strings 32 Discussion 34 ■ Challenge 34 2.4 What are the essentials of regular expressions? 34 Using regular expressions in Python 35 ■ Creating the pattern with a raw string 36 ■ Understanding the essentials of a search pattern 38 ■ Dissecting the matches 41 ■ Knowing the common methods 43 ■ Discussion 44 ■ Challenge 44 2.5 How do I use regular expressions to process texts? 44 Creating a working pattern to find the matches 45 ■ Extracting the needed data from the matches 46 ■ Using named groups for text processing 47 ■ Discussion 48 ■ Challenge 48 3 Using built-in data containers 50 3.1 How do I choose between lists and tuples? 51 Using tuples for immutability and using lists for mutability 51 Using tuples for heterogeneity and using lists for homogeneity 52 Discussion 53 ■ Challenge 54 3.2 How do I sort lists of complicated data using custom functions? 54 Sorting lists using the default order 54 ■ Using a built-in function as the sorting key 55 ■ Using custom functions for more complicated sorting needs 56 ■ Discussion 57 ■ Challenge 57 3.3 How do I build a lightweight data model using named tuples? 58 Understanding alternative data models 58 ■ Creating named tuples to hold data 59 ■ Discussion 61 ■ Challenge 62
CONTENTSvi 3.4 How do I access dictionary keys, values, and items? 62 Using dynamic view objects (keys, values, and items) directly 63 Being cautious with the KeyError exception 64 ■ Avoiding KeyError with a hygiene check first: The non-Pythonic way 65 Using the get method to access a dictionary item 65 ■ Watching for the setdefault method’s side effect 66 ■ Discussion 67 Challenge 68 3.5 When do I use dictionaries and sets instead of lists and tuples? 68 Taking advantage of the constant lookup efficiency 68 Understanding hashable and hashing 70 ■ Discussion 74 Challenge 74 3.6 How do I use set operations to check the relationships between lists? 74 Checking whether a list contains all items of another list 74 Checking whether a list contains any element of another list 76 Dealing with multiple set objects 77 ■ Discussion 79 Challenge 80 4 Dealing with sequence data 82 4.1 How do I retrieve and manipulate subsequences with slice objects? 83 Taking advantage of the full features of slicing 83 ■ Not confusing slices with ranges 86 ■ Using named slice objects to process sequence data 87 ■ Manipulating list items with slicing operations 88 ■ Discussion 89 ■ Challenge 89 4.2 How do I use positive and negative indexing to retrieve items? 90 Positive indexing starts from the beginning of the list 90 Negative indexing starts from the end of the list 90 ■ Combining positive and negative indices as needed 91 ■ Discussion 92 Challenge 92 4.3 How do I find items in a sequence? 92 Checking an item’s presence 92 ■ Using the index method to locate the item 93 ■ Finding substrings in a string 94 ■ Finding an instance of custom classes in a list 95 ■ Discussion 96 ■ Challenge 96 4.4 How do I unpack a sequence? Beyond tuple unpacking 96 Unpacking short sequences with one-to-one correspondence 97 Retrieving consecutive items using the starred expression 98 Denoting unwanted items with underscores to remove distraction 99 ■ Discussion 101 ■ Challenge 101
CONTENTS vii 4.5 When should I consider data models other than lists and tuples? 101 Using sets where membership is concerned 101 ■ Using deques if you care about first-in-first-out 102 ■ Processing multidimensional data with NumPy and Pandas 104 ■ Discussion 104 Challenge 105 5 Iterables and iterations 107 5.1 How do I create common data containers using iterables? 108 Getting to know iterables and iterators 109 ■ Inspecting iterability 110 ■ Using iterables to create built-in data containers 112 ■ Discussion 114 ■ Challenge 115 5.2 What are list, dictionary, and set comprehensions? 115 Creating lists from iterables using list comprehension 115 Creating dictionaries from iterables using dictionary comprehension 117 Creating sets from iterables using set comprehension 117 ■ Applying a filtering condition 118 ■ Using embedded for loops 119 Discussion 120 ■ Challenge 121 5.3 How do I improve for-loop iterations with built-in functions? 121 Enumerating items with enumerate 122 ■ Reversing items with reversed 123 ■ Aligning iterables with zip 124 ■ Chaining multiple iterables with chain 125 ■ Filtering the iterable with filter 127 ■ Discussion 127 ■ Challenge 128 5.4 Using optional statements within for and while loops 128 Exiting the loops with the break statement 130 ■ Skipping an iteration with the continue statement 132 ■ Using else statements in the for and while loops 134 ■ Discussion 137 Challenge 137 PART 2 DEFINING FUNCTIONS ..........................................139 6 Defining user-friendly functions 141 6.1 How do I set default arguments to make function calls easier? 142 Calling functions with default arguments 142 ■ Defining functions with default arguments 143 ■ Avoiding the pitfall of setting default arguments for mutable parameters 145 Discussion 148 ■ Challenge 148
CONTENTSviii 6.2 How do I set and use the return value in function calls? 149 Returning a value implicitly or explicitly 149 ■ Defining functions returning zero, one, or multiple values 150 ■ Using multiple values returned from a function call 153 ■ Discussion 154 Challenge 154 6.3 How do I use type hints to write understandable functions? 154 Providing type hinting to variables 155 ■ Using type hinting in function definitions 156 ■ Applying advanced type-hinting skills to function definitions 157 ■ Discussion 160 ■ Challenge 161 6.4 How do I increase function flexibility with *args and **kwargs? 161 Knowing positional and keyword arguments 162 ■ Accepting a variable number of positional arguments 163 ■ Accepting a variable number of keyword arguments 165 ■ Discussion 166 Challenge 166 6.5 How do I write proper docstrings for a function? 166 Examining the basic structure of a function’s docstring 167 Specifying the function’s action as the summary 168 ■ Documenting the parameters and the return value 169 ■ Specifying any exceptions possibly raised 170 ■ Discussion 171 ■ Challenge 171 7 Using functions beyond the basics 173 7.1 How do I use lambda functions for small jobs? 174 Creating a lambda function 174 ■ Using lambdas to perform a small one-time job 175 ■ Avoiding pitfalls when using lambda functions 176 ■ Discussion 178 ■ Challenge 178 7.2 What are the implications of functions as objects? 179 Storing functions in a data container 179 ■ Sending functions as arguments to higher-order functions 181 ■ Using functions as a return value 182 ■ Discussion 183 ■ Challenge 183 7.3 How do I check functions’ performance with decorators? 183 Decorating a function to show its performance 185 ■ Dissecting the decorator function 186 ■ Wrapping to carry over the decorated function’s metadata 190 ■ Discussion 192 ■ Challenge 192
CONTENTS ix 7.4 How can I use generator functions as a memory-efficient data provider? 193 Creating a generator to yield perfect squares 193 ■ Using generators for their memory efficiency 195 ■ Using generator expressions where applicable 196 ■ Discussion 197 Challenge 197 7.5 How do I create partial functions to make routine function calls easier? 197 “Localizing” shared functions to simplify function calls 198 Creating a partial function to localize a function 199 Discussion 200 ■ Challenge 200 PART 3 DEFINING CLASSES...............................................201 8 Defining user-friendly classes 203 8.1 How do I define the initialization method for a class? 204 Demystifying self: The first parameter in __init__ 204 ■ Setting proper arguments in __init__ 208 ■ Specifying all attributes in __init__ 209 ■ Defining class attributes outside the __init__ method 212 ■ Discussion 212 ■ Challenge 213 8.2 When do I define instance, static, and class methods? 213 Defining instance methods for manipulating individual instances 213 Defining static methods for utility functionalities 214 ■ Defining class methods for accessing class-level attributes 215 ■ Discussion 217 Challenge 217 8.3 How do I apply finer access control to a class? 217 Creating protected methods by using an underscore as the prefix 218 Creating private methods by using double underscores as the prefix 220 ■ Creating read-only attributes with the property decorator 221 ■ Verifying data integrity with a property setter 223 ■ Discussion 224 ■ Challenge 225 8.4 How do I customize string representation for a class? 225 Overriding __str__ to show meaningful information for an instance 225 ■ Overriding __repr__ to provide instantiation information 226 ■ Understanding the differences between __str__ and __repr__ 227 ■ Discussion 229 ■ Challenge 229
CONTENTSx 8.5 Why and how do I create a superclass and subclasses? 229 Identifying the use scenario of subclasses 230 ■ Inheriting the superclass’s attributes and methods automatically 231 ■ Overriding the superclass’s methods to provide customized behaviors 232 ■ Creating non-public methods of the superclass 235 ■ Discussion 236 ■ Challenge 236 9 Using classes beyond the basics 238 9.1 How do I create enumerations? 239 Avoiding a regular class for enumerations 239 ■ Creating an enumeration class 241 ■ Using enumerations 242 ■ Defining methods for the enumeration class 243 ■ Discussion 244 Challenge 244 9.2 How do I use data classes to eliminate boilerplate code? 245 Creating a data class using the dataclass decorator 245 ■ Setting default values for the fields 246 ■ Making data classes immutable 248 ■ Creating a subclass of an existing data class 249 ■ Discussion 250 ■ Challenge 250 9.3 How do I prepare and process JSON data? 251 Understanding JSON’s data structure 251 ■ Mapping data types between JSON and Python 252 ■ Deserializing JSON strings 253 ■ Serializing Python data to JSON format 255 Discussion 257 ■ Challenge 257 9.4 How do I create lazy attributes to improve performance? 257 Identifying the use scenario 258 ■ Overriding the __getattr_ special method to implement lazy attributes 259 ■ Implementing a property as a lazy attribute 261 ■ Discussion 262 ■ Challenge 262 9.5 How do I define classes to have distinct concerns? 262 Analyzing a class 263 ■ Creating additional classes to isolate the concerns 265 ■ Connecting related classes 266 ■ Discussion 269 Challenge 269 PART 4 MANIPULATING OBJECTS AND FILES ........................271 10 Fundamentals of objects 273 10.1 How do I inspect an object’s type to improve code flexibility? 274 Checking an object’s type using type 275 ■ Checking an object’s type using isinstance 276 ■ Checking an object’s type generically 277 Discussion 279 ■ Challenge 279
CONTENTS xi 10.2 What’s the lifecycle of instance objects? 279 Instantiating an object 280 ■ Being active in applicable namespaces 281 ■ Tracking reference counts 282 ■ Destructing the object 284 ■ Discussion 285 ■ Challenge 286 10.3 How do I copy an object? 286 Creating a (shallow) copy 287 ■ Noting the potential problem of a shallow copy 288 ■ Creating a deep copy 291 ■ Discussion 291 Challenge 292 10.4 How do I access and change a variable in a different scope? 292 Accessing any variable: The LEGB rule for name lookup 293 Changing a global variable in a local scope 294 ■ Changing an enclosing variable 296 ■ Discussion 297 ■ Challenge 297 10.5 What’s callability, and what does it imply? 297 Distinguishing classes from functions 298 ■ Revisiting the higher- order function map 299 ■ Using callable as the key argument 299 ■ Creating decorators as classes 300 Discussion 302 ■ Challenge 302 11 Dealing with files 304 11.1 How do I read and write files using context management? 305 Opening and closing files: Context manager 305 ■ Reading data from a file in different ways 307 ■ Writing data to a file in different ways 310 ■ Discussion 313 ■ Challenge 313 11.2 How do I deal with tabulated data files? 313 Reading a CSV file using csv reader 313 ■ Reading a CSV file that has a header 314 ■ Writing data to a CSV file 316 Discussion 317 ■ Challenge 318 11.3 How do I preserve data as files using pickling? 318 Pickling objects for data preservation 318 ■ Restoring data by unpickling 319 ■ Weighing the pros and cons of pickling 321 Discussion 324 ■ Challenge 324 11.4 How do I manage files on my computer? 324 Creating a directory and files 325 ■ Retrieving the list of files of a specific kind 326 ■ Moving files to a different folder 326 Copying files to a different folder 328 ■ Deleting a specific kind of files 329 ■ Discussion 329 ■ Challenge 329 11.5 How do I retrieve file metadata? 330 Retrieving the filename-related information 330 ■ Retrieving the file’s size and time information 331 ■ Discussion 333 ■ Challenge 333
CONTENTSxii PART 5 SAFEGUARDING THE CODEBASE ..............................335 12 Logging and exception handling 337 12.1 How do I monitor my program with logging? 338 Creating the Logger object to log application events 338 Using files to store application events 339 ■ Adding multiple handlers to the logger 341 ■ Discussion 342 ■ Challenge 342 12.2 How do I save log records properly? 343 Categorizing application events with levels 343 ■ Setting a handler’s level 345 ■ Setting formats to the handler 346 Discussion 347 ■ Challenge 348 12.3 How do I handle exceptions? 348 Handling exceptions with try.. .except... 349 ■ Specifying the exception in the except clause 351 ■ Handling multiple exceptions 352 ■ Showing more information about an exception 354 ■ Discussion 355 ■ Challenge 355 12.4 How do I use else and finally clauses in exception handling? 355 Using else to continue the logic of the code in the try clause 356 Cleaning up the exception handling with the finally clause 357 Discussion 359 ■ Challenge 359 12.5 How do I raise informative exceptions with custom exception classes? 360 Raising exceptions with a custom message 360 ■ Preferring built- in exception classes 362 ■ Defining custom exception classes 363 Discussion 365 ■ Challenge 366 13 Debugging and testing 367 13.1 How do I spot problems with tracebacks? 368 Understanding how a traceback is generated 369 ■ Analyzing a traceback when running code in a console 370 ■ Analyzing a traceback when running a script 371 ■ Focusing on the last call in a traceback 372 ■ Discussion 373 ■ Challenge 373 13.2 How do I debug my program interactively? 373 Activating the debugger with a breakpoint 374 ■ Running code line by line 375 ■ Stepping into another function 377 Inspecting pertinent variables 378 ■ Discussion 379 Challenge 379
CONTENTS xiii 13.3 How do I test my functions automatically? 380 Understanding the basis for testing functions 380 ■ Creating a TestCase subclass for testing functions 381 ■ Setting up the test 384 ■ Discussion 385 ■ Challenge 385 13.4 How do I test a class automatically? 385 Creating a TestCase subclass for testing a class 386 ■ Responding to test failures 387 ■ Discussion 388 ■ Challenge 388 PART 6 BUILDING A WEB APP ...........................................391 14 Completing a real project 393 14.1 How do I use a virtual environment for my project? 394 Understanding the rationale for virtual environments 394 ■ Creating a virtual environment for each project 395 ■ Installing packages in the virtual environment 396 ■ Using virtual environments in Visual Studio Code 397 ■ Discussion 398 ■ Challenge 398 14.2 How do I build the data models for my project? 399 Identifying the business needs 399 ■ Creating helper classes and functions 400 ■ Creating the Task class to address these needs 401 ■ Discussion 407 ■ Challenge 407 14.3 How do I use SQLite as my application’s database? 408 Creating the database 408 ■ Retrieving records from the database 409 ■ Saving records to the database 411 Updating a record in a database 412 ■ Deleting a record from the database 413 ■ Discussion 413 ■ Challenge 413 14.4 How do I build a web app as the frontend? 414 Understanding the essential features of streamlit 414 Understanding the app’s interface 415 ■ Tracking user activities using session state 417 ■ Setting up the sidebar 420 ■ Showing the tasks 423 ■ Showing a task’s details 425 ■ Creating a new task 426 ■ Organizing your project 427 ■ Running the app 428 ■ Discussion 429 ■ Challenge 429 solutions to the challenges 431 index 465 Appendices A–E can be found in the digital and online versions of this book.
xiv preface We’re probably the luckiest generation in human history. We’re no longer in the Neo- lithic Age or the Industrial Age; we’ve entered the Information Age. Advanced infor- mation technologies, particularly computers and networks, have transformed human life. We can take a flight from our hometown to another place thousands of miles away in less than half a day. We can make a doctor’s appointment using a smartphone and attend the appointment through a video call, if we prefer. We can order almost anything from online stores and get it delivered within days or even hours. These transformations have been accompanied by the accumulation of tremen- dous amounts of data over the past couple of decades. The work of processing and analyzing this data has contributed to the emergence of a new interdisciplinary sub- ject: data science. As a behavioral scientist, I spend a significant amount of time deal- ing with data, so you might say that I’m applying data science to behavioral research. It takes more than paper and pencil to process data of this magnitude, however. Instead, I’ve been writing code to clean data and run statistical models with a wonder- ful programming language: Python. As a self-taught coder, I know it’s not easy to grasp Python or any other program- ming language—not because it takes a long time to learn all the techniques (and know which ones to use when), but because too many learning resources are available, such as online courses, tutorial videos, blog articles, and certainly books. How do you choose the ones that are most suitable for you? I had the same question when I started learning Python. Over the years, I’ve tried a variety of resources, and I’ve found that the best learning resources are books,
PREFACE xv because books have well-structured content that makes it possible to take a deep dive into the language. During the learning process, you can set your own pace. Whenever you need to, you can slow down to digest hard topics. In addition, you can refer to the books on your bookshelf quickly should any questions arise. Most of the Python books on the market are written for beginners (providing detailed coverage of the language’s basic features) or advanced users (covering spe- cialized techniques that are less generalizable). Without doubt, a few of those books are great. From the learning-curve perspective, however, I felt that a book was missing: one for Python learners at the late-beginner and early-intermediate levels. These stages are critical, as learners are forming the right coding habits and figuring out the proper Pythonic techniques for a given context. From the content perspective, I thought the missing book should address general programming problems that most readers could relate to their work, no matter what they do with Python: web develop- ment or data science. In other words, more readers could benefit from such a book because it would provide general domain-independent knowledge. I wrote this book to fill the gap between beginner and advanced books. I hope you’ll feel that you’ve learned a few things after reading it.
xvi acknowledgments I’d like to thank my mentors, Dr. Paul Cinciripini and Dr. Jason Robinson at the Uni- versity of Texas MD Anderson Cancer Center, for supporting me as I pursued the use of Python as the language for our analytic work. That effort eventually led to this book. I also want to thank the Manning team: Publisher Marjan Bace for leading the excel- lent editorial and production teams; Associate Publisher Michael Stephens for inviting me to write this book; Senior Development Editor Marina Michaels for coordinating and editing; René van den Berg for technical editing; Walter Alexander and Ignacio Torres for providing code review; Aleksandar Dragosavljević for organizing peer reviews; as well as the production staff for their hard work in formatting this book. Finally, thank you to the reviewers, who provided valuable feedback: Alexei Znamensky, Alexey Vyskubov, Ariel Andres, Brent Boylan, Chris Kolosiwsky, Christo- pher Kardell, Christopher Villanueva, Claudiu Schiller, Clifford Thurber, Dirk Gomez, Ganesh Swaminathan, Georgios Doumas, Gerald Mack, Gregory Grimes, Igor Dudchenko, Iyabo Sindiku, James Matlock, Jeffrey M. Smith, Josh McAdams, Keerthi Shetty, Larry Cai, Louis Aloia, Marcus Geselle, Mary Anne Thygesen, Mike Baran, Ninoslav Cerkez, Oliver Korten, Piergiorgio Faraglia, Radhakrishna M.V., Rajinder Yadav, Raymond Cheung, Robert Wenner, Shankar Swamy, Sriram Macharla, Giri S. Swaminathan, Steven Herrera, and Vitosh K. Doynov. Their suggestions helped make this a better book.
xvii about this book In this book, I focus on teaching the essential techniques of Python from a specialty- independent perspective. Although a variety of Python packages are available for different specialties, such as data science and web development, these packages are built on the core features of Python. No matter what domain-specific Python packages you use for your job, you must have a good understanding of essential techniques, such as choosing the proper data models and writing well-structured functions and classes. These techniques make it possible for you to use your domain-specific packages comfortably. Who should read this book If you’ve been self-teaching and using Python for some time, but feel that your Python knowledge is unstructured, I consider you to be a late-beginner or early-intermediate user. This book is right for you because you need to reinforce and synthesize your Python knowledge in a structured way. In this book, I identify several topics in each chapter to address common problems that you may encounter in your work. My coverage of these topics teaches you more than how to address a specific problem; it also frames the con- tent in a larger context, showing why and how the topic matters when you’re working on a project. This way, you’re not learning individual techniques to complete separate tasks; you’re completing a project and learning these techniques in the process. How this book is organized: A road map This book consists of six parts, as shown in the following figure. In the first part (chap- ters 2–5), you study the built-in data models, such as strings, lists, and dictionaries. These data models are the building blocks of any project. In the second part (chapters
ABOUT THIS BOOKxviii 6 and 7), you learn about best practices for defining functions. Functions are integral to any project because they’re responsible for manipulating data to create the desired output. In the third part (chapters 8 and 9), you learn techniques for defining custom classes. Instead of using built-in classes, we define custom classes to better model the data in our project. In the fourth part (chapters 10 and 11), you learn the fundamen- tals of using objects and manipulating files on your computers. In the fifth part (chap- ters 12 and 13), you learn a variety of techniques to safeguard your programs, including logging, exception handling, and testing. In the sixth part (chapter 14), you • Objects: mutability, hashability, callability, copying, instantiation and destruction • Inspection: type, isinstance, generic types • Namespace: scope, LEGB, global, nonlocal • Using files: context manager, tabulated data, metadata, moving and copying • Pickling: flexibility and integrity Part 1. Built-in data models • Strings: formatting and data extracting • Lists: mutability, homogeneity, sorting • Tuples: immutability, heterogeneity, named tuples • Dictionaries: hashability, key-value pairs, view objects • Sets: hashability, set operations • Sequences: indexing, slicing, unpacking, searching • Iterables: comprehensions, iterations Part 2. Writing good functions • Structure: input arguments, return value • Default arguments: immutable and mutable • Variable number of arguments: *args, **kwargs • Annotations: type hints, generic types • Docstrings: parameters, return value, exceptions • Advanced concepts: lambda, decorator, closure, higher-order function, generator, partial function Part 3. Defining good classes • Initialization: specifying all attributes • Methods: instance, static, and class methods • Access control: protected, private, property • String representations: __str__ and __repr__ • Hierarchy: superclass and subclass • Enumerations: enum and iterations • Data classes: removing boilerplate, fields • Lazy evaluation: property and __getattr__ Part 4. Using objects and files Part 6. Completing a project to build a web app Working on the project as the shared context Part 5. Safeguarding programs • Logging: levels, handlers, proper log records • Exceptions: try...except...else...finally, handle specific exceptions, custom exceptions • Debugging: tracebacks, interactive debugging • Testing: test cases, functions, classes
Comments 0
Loading comments...
Reply to Comment
Edit Comment