Python Tricks: The Book Dan Bader For online informationand ordering ofthis and other books byDan Bader,pleasevisitrealpython.com. Formoreinformation,pleasecon- [email protected]. Copyright©DanBader(realpython.com),2016–2019 ISBN:9781775093305(paperback) ISBN:9781775093312(electronic) CoverdesignbyAnjaPircherDesign(anjapircher.com) “Python” and the Python logos are trademarks or registered trade- marks of the Python Software Foundation, used by Dan Bader with permissionfromtheFoundation. Thankyoufordownloadingthisebook. Thisebookislicensedforyour personalenjoymentonly. Thisebookmaynotbere-soldorgivenaway tootherpeople. Ifyouwouldliketosharethisbookwithanotherper- son, please purchase an additional copy for each recipient. If you’re reading this book and did not purchase it, or it was not purchased foryouruseonly,thenpleasereturntorealpython.com/pytricks-book andpurchaseyourowncopy. Thankyouforrespectingthehardwork behindthisbook. Updated 2019-09-05 I would like to thank Michael Howitz, Johnathan Willitts, Julian Orbach, Johnny Giorgis, Bob White, Daniel Meyer, Michael Stueben, Smital Desai, Andreas Kreisig, David Perkins, Jay Prakash Singh, Ben Felder, Steve Schmerler, MalcolmTelfer,andBoxiangSunfortheirexcellentfeedback. WhatPythonistasSayAboutPythonTricks: TheBook “I love, love, love the book. It’s like having a seasoned tutor explain- ing,well,tricks! I’mlearningPythononthejobandI’mcomingfrom Powershell, which I learned on the job—so lots of new, great stu(cid:1086). WheneverIgetstuckinPython(usuallywithFlaskblueprintsorIfeel likemycodecouldbemorePythonic)Ipostquestionsinourinternal Pythonchatroom. I’m often amazed at some of the answers co-workers give me. Dict comprehensions, lambdas, and generators often pepper their feed- back. Iamalwaysimpressedandyet(cid:1088)abbergastedathowpowerful Python is when you know these tricks and can implement them correctly. YourbookwasexactlywhatIwantedtohelpgetmefromabewildered Powershellscriptertosomeonewhoknowshowandwhentousethese Pythonic‘tricks’everyonehasbeentalkingabout. Assomeonewhodoesn’thavemydegreeinCSit’snicetohavethetext toexplainthingsthatothersmighthavelearnedwhentheywereclas- sicallyeducated. Iamreallyenjoyingthebookandamsubscribedto theemailsaswell,whichishowIfoundoutaboutthebook.” —DanielMeyer,Sr.DesktopAdministratoratTeslaInc. “I (cid:1087)rst heard about your book from a co-worker who wanted to trick me with your example of how dictionaries are built. I was almost100%sureaboutthereasonwhytheendproductwasamuch smaller/simpler dictionary but I must confess that I did not expect theoutcome:) HeshowedmethebookviavideoconferencingandIsortofskimmed throughitashe(cid:1088)ippedthepagesforme,andIwasimmediatelycuri- oustoreadmore. ThatsameafternoonIpurchasedmyowncopyandproceededtoread yourexplanationforthewaydictionariesarecreatedinPythonand laterthatday,asImetadi(cid:1086)erentco-workerforco(cid:1086)ee,Iusedthesame trickonhim:) Hethensprungadi(cid:1086)erentquestiononthesameprinciple,andbecause ofthewayyouexplainedthingsinyourbook,Iwasabletonotsimply guess the result but to correctly answer what the outcome would be. Thatmeansthatyoudidagreatjobatexplainingthings:) IamnotnewinPythonandsomeoftheconceptsinsomeofthechap- tersarenotnewtome, butImustsaythatIdogetsomethingoutof every chapter so far, so kudos for writing a very nice book and for doing a fantastic job at explaining concepts behind the tricks! I will certainlyletmyfriendsandco-workersknowaboutyourbook.” —OgMaciel,PythonDeveloperatRedHat “I really enjoyed reading Dan’s book. He explains important Python is == aspectswithclearexamples(usingtwotwincatstoexplain vs forexample). Itisnotjustcodesamples,itdiscussesrelevantimplementationdetails comprehensibly. Whatreallymattersthoughisthatthisbookmakes youwritebetterPythoncode! ThebookisactuallyresponsibleforrecentnewgoodPythonhabitsI picked up, for example: using custom exceptions and ABC’s (I found Dan’sblogsearchingforabstractclasses.) Thesenewlearningsalone areworththeprice.” —BobBelderbos,EngineeratOracle&Co-FounderofPyBites Contents Contents 6 Foreword 9 1 Introduction 11 1.1 What’saPythonTrick? . . . . . . . . . . . . . . . . 11 1.2 WhatThisBookWillDoforYou . . . . . . . . . . . 13 1.3 HowtoReadThisBook . . . . . . . . . . . . . . . . 14 2 PatternsforCleanerPython 15 2.1 CoveringYourA**WithAssertions . . . . . . . . . . 16 2.2 ComplacentCommaPlacement . . . . . . . . . . . . 25 with 2.3 ContextManagersandthe Statement . . . . . . 29 2.4 Underscores,Dunders,andMore . . . . . . . . . . . 36 2.5 AShockingTruthAboutStringFormatting . . . . . . 48 2.6 “TheZenofPython”EasterEgg . . . . . . . . . . . . 56 3 E(cid:1087)ectiveFunctions 57 3.1 Python’sFunctionsAreFirst-Class . . . . . . . . . . 58 3.2 LambdasAreSingle-ExpressionFunctions . . . . . . 68 3.3 ThePowerofDecorators . . . . . . . . . . . . . . . 73 *args **kwargs 3.4 FunWith and . . . . . . . . . . . . 86 3.5 FunctionArgumentUnpacking . . . . . . . . . . . . 91 3.6 NothingtoReturnHere . . . . . . . . . . . . . . . . 94 6 Contents 4 Classes&OOP 97 4.1 ObjectComparisons: “is”vs“==” . . . . . . . . . . . 98 __repr__ 4.2 StringConversion(EveryClassNeedsa ) . 101 4.3 DefiningYourOwnExceptionClasses . . . . . . . . 111 4.4 CloningObjectsforFunandProfit . . . . . . . . . . 116 4.5 AbstractBaseClassesKeepInheritanceinCheck . . . 124 4.6 WhatNamedtuplesAreGoodFor . . . . . . . . . . . 128 4.7 ClassvsInstanceVariablePitfalls . . . . . . . . . . . 136 4.8 Instance,Class,andStaticMethodsDemystified . . . 142 5 CommonDataStructuresinPython 152 5.1 Dictionaries,Maps,andHashtables . . . . . . . . . 155 5.2 ArrayDataStructures . . . . . . . . . . . . . . . . . 162 5.3 Records,Structs,andDataTransferObjects . . . . . 172 5.4 SetsandMultisets . . . . . . . . . . . . . . . . . . . 184 5.5 Stacks(LIFOs) . . . . . . . . . . . . . . . . . . . . 188 5.6 Queues(FIFOs) . . . . . . . . . . . . . . . . . . . . 194 5.7 PriorityQueues . . . . . . . . . . . . . . . . . . . . 200 6 Looping&Iteration 204 6.1 WritingPythonicLoops . . . . . . . . . . . . . . . . 205 6.2 ComprehendingComprehensions . . . . . . . . . . 209 6.3 ListSlicingTricksandtheSushiOperator . . . . . . 213 6.4 BeautifulIterators . . . . . . . . . . . . . . . . . . . 217 6.5 GeneratorsAreSimplifiedIterators . . . . . . . . . . 230 6.6 GeneratorExpressions . . . . . . . . . . . . . . . . 237 6.7 IteratorChains . . . . . . . . . . . . . . . . . . . . 244 7 DictionaryTricks 248 7.1 DictionaryDefaultValues . . . . . . . . . . . . . . . 249 7.2 SortingDictionariesforFunandProfit . . . . . . . . 253 7.3 EmulatingSwitch/CaseStatementsWithDicts . . . . 257 7.4 TheCraziestDictExpressionintheWest . . . . . . . 262 7.5 SoManyWaystoMergeDictionaries . . . . . . . . . 269 7.6 DictionaryPretty-Printing . . . . . . . . . . . . . . 272 7 Contents 8 PythonicProductivityTechniques 275 8.1 ExploringPythonModulesandObjects . . . . . . . . 276 8.2 IsolatingProjectDependenciesWithVirtualenv . . . 280 8.3 PeekingBehindtheBytecodeCurtain . . . . . . . . . 285 9 ClosingThoughts 290 9.1 FreeWeeklyTipsforPythonDevelopers . . . . . . . 292 9.2 RealPythonVideoCourseLibrary . . . . . . . . . . 293 9.3 PythonistaCafe: ACommunityforPythonDevelopers 294 8 Foreword It’sbeenalmosttenyearssinceIfirstgotacquaintedwithPythonasa programminglanguage. WhenIfirstlearnedPythonmanyyearsago, itwaswithalittlereluctance. Ihadbeenprogramminginadifferent language before, and all of the sudden at work, I was assigned to a differentteamwhereeveryoneusedPython. Thatwasthebeginning ofmyownPythonjourney. WhenIwasfirstintroducedtoPython,Iwastoldthatitwasgoingto beeasy,thatIshouldbeabletopickitupquickly. WhenIaskedmy colleagues for resources for learning Python, all they gave me was a link to Python’s official documentation. Reading the documentation wasconfusingatfirst,anditreallytookmeawhilebeforeIevenfelt comfortable navigating through it. Often I found myself needing to lookforanswersinStackOverflow. Comingfromadifferentprogramminglanguage,Iwasn’tlookingfor just any resource for learning how to program or what classes and objectsare. Iwaslookingforspecificresourcesthatwouldteachme thefeaturesofPython,whatsetsitapart,andhowwritinginPython isdifferentthanwritingcodeinanotherlanguage. Itreallyhastakenmemanyyearstofullyappreciatethislanguage. As IreadDan’sbook,IkeptthinkingthatIwishedIhadaccesstoabook likethiswhenIstartedlearningPythonmanyyearsago. Forexample,oneofthemanyuniquePythonfeaturesthatsurprised me at first were list comprehensions. As Dan mentions in the book, atellofsomeonewhojustcametoPythonfromadifferentlanguage 9