Fluent Python : [clear, Concise, And Effective Programming]

Transcription

FluentPythonLuciano RamalhoBeijing Boston Farnham Sebastopol TokyoO'REILLY

Table of ContentsPrefacePart I.xvPrologue1. ThePython Data ModelPythonic Card DeckAHowSpecial Methods34Are UsedEmulating Numeric Types9String RepresentationArithmetic OperatorsBoolean Value of a Custom81112Type12Overview of Special Methods13Why len14Is NotaMethodChapter Summary14Further15Part II.2. AnReadingData StructuresArray of SequencesOverview of Built-in19SequencesList20Comprehensions and Generator ExpressionsList Comprehensions and ReadabilityListcomps Versus map and filter23Cartesian Products23GeneratorTuplesTuplesExpressionsAre NotasJust Immutable ListsRecordsTuple Unpacking212125262627v

Nested29Tuple Unpacking30Named TuplesTuplesas32Immutable ListsSlicing33Why Slices and Range Exclude the LastSlice ObjectsMultidimensional Slicing and EllipsisAssigning to Slices*Using and with Sequencesof ListsListsBuildingAugmented Assignment with SequencesA ItemAssignment Puzzler34353636373840list.sort and the sorted Built-in Function42Managing Ordered Sequences with bisectSearching with bisectInserting with bisect.insort44When48aList Is Not the Answer4447Arrays48Memory Views51NumPy and SciPy52DequesChapter Summary55Further59and Other Queues57Reading633. Dictionaries and SetsGenericdiet64Mapping Types66ComprehensionsOverview of CommonMapping Methodswith setdefault66Handling Missing KeysMappings with Flexible Key Lookupdefaultdict: Another Take on Missing KeysThemissing Method68Variations of diet75Subclassing UserDictImmutable MappingsSet Theory76707072777980set LiteralsComprehensionsOperations81diet and set Under the Hood85SetSet 82Experiment85Hash Tables in Dictionaries87A Performancevi33Table of Contents

PracticalConsequences of How diet WorksConsequencesChapter Summary90How Sets Work—Practical93Further Reading944. Text versus93Bytes97Character IssuesByte98EssentialsStructs and99Memory102ViewsBasic Encoders/Decoders103Understanding Encode/Decode ProblemsCoping with UnicodeEncodeError105105Coping with UnicodeDecodeErrorSyntaxError When106Modules withUnexpected Encoding108LoadingEncoding of a Byte SequenceHow to Discover the109BOM: A Useful Gremlin110Text FilesHandlingEncoding Defaults: A MadhouseNormalizing Unicode for Saner ComparisonsCase FoldingUtility Functions for Normalized Text MatchingExtreme "Normalization":111114117119120Taking Out DiacriticsSorting Unicode TextSorting with the Unicode Collation121124Algorithm126127The Unicode DatabaseDual-Modestr Versusstr Versusstr andAPIs129bytesbytes in Regular Expressionsbytes on os Functions129130Chapter Summary132Further133Part III.ReadingFunctionsasObjects1415. First-Class FunctionsTreatingaFunction Likean142Object143FunctionsHigher-OrderModern Replacements for map, filter, and reduceAnonymous FunctionsThe Seven Flavors of Callable144145Objects146User-Defined Callable Types147Function148Introspectionvii Table of Contents

From PositionaltoKeyword-OnlyRetrieving Information AboutParametersParameters156Packages for Functional Programming158158Arguments withFreezingChapter SummaryFurther ReadingDesignfunctools.partial161163164Patterns with First-Class FunctionsCase169Study: Refactoring StrategyClassic Strategy170Function-Oriented Strategy174Choosing177the BestFinding StrategiesStrategy: Simple ApproachinaModule170178Command179Chapter SummaryFurther Reading1811827. Function Decorators and Closures185Decorators 101WhenPython186Executes ariable Scope Rules191Closures194The nonlocal DeclarationImplementingaSimple197DecoratorHow It WorksDecorators inthe Standard Libraryfunctools.lru cacheGeneric Functions withSingle DispatchStacked Decorators201202204207Parameterized DecoratorsA Parameterized198200Memoization withRegistration208DecoratorThe Parameterized Clock Decoratorviii152Function AnnotationsThe operator Module6.150208211Chapter Summary213Further214 ReadingTable of Contents

Object-Oriented IdiomsPart IV.8.Object References, Mutability, and Recycling221Variables Are Not Boxes222Identity, Equality, and AliasesChoosingBetweenThe RelativeCopies225of TuplesImmutabilityby Default226Are ShallowDeep and Shallow CopiesFunction ParametersMutable TypesDefensivedel and223and is asas227230Arbitrary ObjectsReferences231Parameter Defaults: Bad IdeaProgrammingGarbageof232with Mutable Parameters234Collection236Weak ons of Weak ReferencesTricksPython Plays withChapter SummaryFurther Reading241Immutables2422442459. A Pythonic Object249Object Representations250Vector Class Redux250An Alternative Constructor253classmethod Versus staticmethod254Formatted255DisplaysA Hashable Vector2d259Private and "Protected" Attributes inSavingSpace with theslotsPython264Class Attribute267269The Problems with slotsClass Attributes10.OverridingChapter SummaryFurther Reading269Sequence Hacking, Hashing, and SlicingVector: A User-Defined Sequence TypeVector Take # 1: Vector2d CompatibleProtocols and Duck Typing277Vector Take #2: A Sliceable282272273278278281Sequence283Slicing WorksSlice-AwaregetitemHowAVector Take #3:Dynamic285Attribute Access286Table of Contents ix

Vector Take #4:Hashing andFormattingChapter SummaryFurther ReadingaFaster290 Vector Take #5:29630330411. Interfaces: From Protocols to ABCsInterfaces and Protocols in309CulturePythonPython Digs SequencesMonkey-Patching to Implement a Protocol310312at RuntimeAlex Martellis WaterfowlSubclassingan316ABC321ABCs in the StandardLibrary323ABCs in collections.abc323The Numbers Tower of ABCs325Defining and Using an ABCABC326Syntax DetailsSubclassing theA Virtual330Tombola ABC331Subclass of Tombola334How the Tombola Subclasses Were TestedUsageof register in PracticeGeese Can Behaveas34034234412. Inheritance: For Good or For WorseBuilt-in351IsSubclassingTypes TrickyMultiple Inheritance and Method Resolution OrderMultiple Inheritance in the Real WorldCoping with Multiple Inheritance1. Distinguish Interface Inheritance from Implementation Inheritance2. Make InterfacesABCs3553603623633633634. Make Mixins363Explicit by NamingMay Also BeaMixin; The Reverse Is Not True3646. Don't Subclass from More Than One Concrete Class3647. Provide3648.Classes to UsersAggregate"Favor Object Composition Over Class Inheritance."Tkinter: TheGood, the Bad,and theUglyModern Example: Mixins in Django Generic ViewsChapter SummaryFurther Reading Explicit with3523. Use Mixins for Code Reuse5. An ABCx337340DucksChapter SummaryFurther ReadingA314Table of Contents365365366370371

13.Operator Overloading: Doing It RightOverloadingOperator375101376Unary OperatorsOverloading 376for Vector Addition379Overloading for Scalar MultiplicationRich Comparison OperatorsAugmented Assignment OperatorsChapter SummaryFurther Reading*Part V.14.384388392397398Control FlowIterables, Iterators, and Generators405Sentence Take # 1: A406Why SequencesSequenceof WordsAre Iterable: The iter Function408Iterables Versus Iterators409Sentence Take #2: A Classic Interior413Iterator: Bad Idea415Sentence Take #3: A Generator Function416MakingHowaSentenceanGenerator Function WorksSentence Take #4: ASentence Take417Lazy Implementation#5: A Generator420421ExpressionGenerator Expressions: When to Use ThemAnotherExample:423Arithmetic Progression Generator424Arithmetic Progression with itertoolsGenerator Functions in the Standard427428LibraryPython 3.3: yield fromIterable Reducing Functions437A Closer Look at the iter Function440New Syntax inStudy:Generators inGeneratorsCoroutinesCaseasaDatabase Conversion438Utility441443443Chapter SummaryFurther Reading15. ContextDo444Managers and else BlocksThis, ThenContextThat: else BlocksManagers451Beyond if452454and with BlocksThe contextlib Utilities458Using @contextmanagerChapter Summary459Reading463Further463Table of Contents xi

16. Coroutines467How Coroutines Evolved from Generators468Basic Behavior of469Example:aGenerator UsedCoroutine toas aCoroutineCompute Running AveragePrimingCoroutine Termination and Exception HandlingReturning a Value from a CoroutineUsing yield fromThe Meaning of yield fromaDecorators for CoroutineUse Case: Coroutines for Discrete Event Simulation481487493493494Concurrency with502504Futures509509Example:StylesA Sequential Download ScriptDownloading with concurrent.futures511514Where Are the Futures?515I/O and the GILBlockingLaunching519Processes with concurrent.futuresExperimenting with Executor.mapDownloads with Progress Display andErrorHandlingin the519521ErrorHandlingflags2 ExamplesUsing futures.as completedThreading and MultiprocessingChapter SummaryFurther Reading524529531Alternatives534534535Concurrency with asyncio541Thread Versus Coroutine: A543Comparisonasyncio.Future: Nonblocking by DesignYielding from Futures, Tasks, and CoroutinesDownloading with asyncio and aiohttpRunning Circling Around Blocking CallsEnhancing the asyncio downloader ScriptUsing asyncio.as completedUsing an Executor to Avoid Blocking the Event LoopFrom Callbacks to Futures and CoroutinesDoing Multiple RequestsWriting asyncioxii475479The Taxi Fleet SimulationWeb Downloads in Three18.474About Discrete Event SimulationsChapter SummaryFurther Reading17.472 Table of ContentsServersfor Each Download549550552556558559564566569571

AnAnasyncio TCP Serveraiohttp Web ServerSmarter Clients for Better572577Concurrency580Chapter SummaryFurther ReadingPart VI.19.581583MetaprogrammingDynamic Attributes and Properties589DataWranglingDynamicExploring JSON-Like Data with Dynamic Attributes590The Invalid Attribute Name Problem595Flexible597withAttributesCreation withObjectRestructuring the592newOSCON Feed with shelve598Linked Record Retrieval with PropertiesUsingaPropertyfor Attribute ValidationLineltem Take #1: Class forLineltem Take #2: AA602anItem inan608Order608Validating Property609Proper Look at PropertiesPropertiesProperty611Override Instance Attributes612Documentation614615Coding a Property FactoryHandling Attribute Deletion618Essential Attributes and Functions for AttributeHandling620Special Attributes that Affect Attribute HandlingBuilt-in Functions for Attribute HandlingSpecial Methods for Attribute HandlingChapter SummaryFurther Reading62062062162362362920. Attribute DescriptorsDescriptor Example:Attribute ValidationLineltem Take #3: A629Simple DescriptorLineltem Take #4: AutomaticStorage AttributeLineltem Take #5: A New Descriptor630Names635641TypeNonoverriding DescriptorsOverridingOverriding DescriptorOverriding Descriptor Without getNonoverriding DescriptorOverwriting a Descriptor in the Class644Methods Are650Versus646647648649DescriptorsTable of Contents xiii

Descriptor Usage TipsDescriptor docstring and Overriding DeletionChapter Summary652Further65521. ClassReadingWhat660FactoryA Class Decorator forCustomizing DescriptorsHappens When: Import Time VersusThe Evaluation Time ExercisesMetaclasses 101The Metaclass Evaluation Time ExerciseRuntime663665666670673A Metaclass for677The Metaclass679Customizing DescriptorsSpecial MethodprepareClasses as ObjectsChapter SummaryFurther ReadingAfterwordA.655659MetaprogrammingA Class654Support Scripts681682683687691Python Jargon719Index729xiv Table of Contents

TableofContents Preface xv PartI. Prologue 1. ThePythonDataModel 3 APythonicCardDeck 4 HowSpecialMethodsAre Used 8 EmulatingNumericTypes 9 StringRepresentation 11 ArithmeticOperators 12 BooleanValueof aCustomType 12 OverviewofSpecialMethods 13 WhylenIs NotaMethod 14 ChapterSummary 14 FurtherReading 15 PartII.Data Structures 2. AnArrayofSequences 19