Pro Python advanced coding techniques and tools (Marty Alchin) (z-library.sk, 1lib.sk, z-lib.sk)
Author: Marty Alchin
技术
No Description
📄 File Format:
PDF
💾 File Size:
1.3 MB
8
Views
0
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
Pro Python Marty Alchin
📄 Page
2
Pro Python Copyright © 2010 by Marty Alchin 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 (pbk): 978-1-4302-2757-1 ISBN-13 (electronic): 978-1-4302-2758-8 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editors: Duncan Parkes, Tom Welsh Technical Reviewer: George Vilches Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editors: Nancy Sixsmith, Angel Alchin Compositor: Bytheway Publishing Services Indexer: John Collin Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code.
📄 Page
3
Contents Chapter 1: Principles and Philosophy.....................................................................1 The Zen of Python..............................................................................................................1 Beautiful Is Better Than Ugly .................................................................................................................... 2 Explicit Is Better Than Implicit .................................................................................................................. 2 Simple Is Better Than Complex ................................................................................................................ 3 Complex Is Better Than Complicated........................................................................................................ 3 Flat Is Better Than Nested ........................................................................................................................ 4 Sparse Is Better Than Dense .................................................................................................................... 5 Readability Counts.................................................................................................................................... 5 Special Cases Aren’t Special Enough to Break the Rules......................................................................... 6 Although Practicality Beats Purity ............................................................................................................ 6 Errors Should Never Pass Silently ............................................................................................................ 7 Unless Explicitly Silenced......................................................................................................................... 8 In the Face of Ambiguity, Refuse the Temptation to Guess...................................................................... 9 There Should Be One—and Preferably Only One— Obvious Way to Do It ............................................. 10 Although That Way May Not Be Obvious at First Unless You’re Dutch ................................................... 10 Now Is Better Than Never....................................................................................................................... 11
📄 Page
4
Although Never Is Often Better Than Right Now..................................................................................... 11 If the Implementation is Hard to Explain, It’s a Bad Idea........................................................................ 11 If the Implementation is Easy to Explain, It May Be a Good Idea............................................................ 11 Namespaces Are One Honking Great Idea— Let’s Do More of Those!................................................... 12 Don’t Repeat Yourself ......................................................................................................12 Loose Coupling ................................................................................................................13 The Samurai Principle......................................................................................................13 The Pareto Principle.........................................................................................................14 The Robustness Principle ................................................................................................14 Backward Compatibility...................................................................................................15 The Road to Python 3.0....................................................................................................16 Taking It With You............................................................................................................17 Chapter 2: Advanced Basics .................................................................................19 General Concepts.............................................................................................................19 Iteration .................................................................................................................................................. 19 Caching................................................................................................................................................... 20 Transparency.......................................................................................................................................... 21 Control Flow.....................................................................................................................21 Catching Exceptions ............................................................................................................................... 21 Exception Chains .................................................................................................................................... 24 When Everything Goes Right .................................................................................................................. 26 Proceeding Regardless of Exceptions .................................................................................................... 27 Optimizing Loops .................................................................................................................................... 29 The with Statement ................................................................................................................................ 29 Conditional Expressions ......................................................................................................................... 31 Iteration ...........................................................................................................................33 Sequence Unpacking.............................................................................................................................. 34 List Comprehensions .............................................................................................................................. 35
📄 Page
5
Generator Expressions............................................................................................................................ 36 Set Comprehensions............................................................................................................................... 37 Dictionary Comprehensions.................................................................................................................... 37 Chaining Iterables Together ................................................................................................................... 38 Zipping Iterables Together...................................................................................................................... 38 Collections .......................................................................................................................39 Sets ........................................................................................................................................................ 39 Named Tuples......................................................................................................................................... 43 Ordered Dictionaries............................................................................................................................... 44 Dictionaries with Defaults ...................................................................................................................... 44 Importing Code ................................................................................................................45 Fallback Imports ..................................................................................................................................... 45 Importing from the Future ...................................................................................................................... 46 Using __all__ to Customize Imports....................................................................................................... 47 Relative Imports...................................................................................................................................... 48 The __import__() function...................................................................................................................... 49 The importlib module.............................................................................................................................. 51 Taking It With You............................................................................................................52 Chapter 3: Functions.............................................................................................53 Arguments .......................................................................................................................53 Planning for Flexibility ............................................................................................................................ 54 Variable Positional Arguments................................................................................................................ 54 Variable Keyword Arguments ................................................................................................................. 55 Combining Different Kinds of Arguments ............................................................................................... 56 Invoking Functions with Variable Arguments ......................................................................................... 59 Preloading Arguments ............................................................................................................................ 60 Introspection........................................................................................................................................... 61 Example: Identifying Argument Values................................................................................................... 62 Example: A More Concise Version .......................................................................................................... 64
📄 Page
6
Example: Validating Arguments.............................................................................................................. 66 Decorators .......................................................................................................................67 Closures.................................................................................................................................................. 69 Wrappers ................................................................................................................................................ 71 Decorators with Arguments.................................................................................................................... 72 Decorators with—or without—Arguments ............................................................................................ 74 Example: Memoization ........................................................................................................................... 75 Example: A Decorator to Create Decorators ........................................................................................... 77 Function Annotations.......................................................................................................78 Example: Type Safety ............................................................................................................................. 79 Factoring Out the Boilerplate.................................................................................................................. 86 Example: Type Coercion ......................................................................................................................... 88 Annotating with Decorators.................................................................................................................... 90 Example: Type Safety as a Decorator..................................................................................................... 90 Generators .......................................................................................................................94 Lambdas ..........................................................................................................................96 Introspection....................................................................................................................97 Identifying Object Types ......................................................................................................................... 98 Modules and Packages........................................................................................................................... 98 Docstrings .............................................................................................................................................. 99 Taking It With You..........................................................................................................101 Chapter 4: Classes ..............................................................................................103 Inheritance.....................................................................................................................103 Multiple Inheritance.............................................................................................................................. 105 Method Resolution Order (MRO) ........................................................................................................... 106 Example: C3 Algorithm ......................................................................................................................... 109 Using super() to Pass Control to Other Classes .................................................................................... 115 Introspection......................................................................................................................................... 117
📄 Page
7
How Classes Are Created ..............................................................................................119 Creating Classes at Runtime ................................................................................................................ 120 Metaclasses.......................................................................................................................................... 121 Example: Plugin Framework................................................................................................................. 122 Controlling the Namespace .................................................................................................................. 125 Attributes .......................................................................................................................126 Properties ............................................................................................................................................. 127 Descriptors ........................................................................................................................................... 129 Methods.........................................................................................................................131 Unbound Methods................................................................................................................................. 131 Bound Methods..................................................................................................................................... 132 Class Methods ...................................................................................................................................... 133 Static Methods...................................................................................................................................... 134 Assigning Functions to Classes and Instances..................................................................................... 135 Magic Methods ..............................................................................................................135 Creating Instances................................................................................................................................ 136 Example: Automatic Subclasses........................................................................................................... 137 Dealing with Attributes ......................................................................................................................... 138 String Representations ......................................................................................................................... 140 Taking It With You..........................................................................................................142 Chapter 5: Common Protocols ............................................................................143 Basic Operations............................................................................................................143 Mathematical Operations ..................................................................................................................... 144 Bitwise Operations ............................................................................................................................... 148 Variations.............................................................................................................................................. 150 Numbers ........................................................................................................................152 Sign Operations .................................................................................................................................... 154 Comparison Operations ........................................................................................................................ 154
📄 Page
8
Iterables.........................................................................................................................155 Example: Repeatable Generators ......................................................................................................... 158 Sequences .....................................................................................................................159 Mappings .......................................................................................................................164 Callables ........................................................................................................................165 Context Managers..........................................................................................................166 Taking It With You..........................................................................................................168 Chapter 6: Object Management ..........................................................................169 Namespace Dictionary...................................................................................................170 Example: Borg Pattern.......................................................................................................................... 170 Example: Self-caching properties ........................................................................................................ 173 Garbage Collection.........................................................................................................176 Reference Counting .............................................................................................................................. 177 Cyclical References .............................................................................................................................. 178 Weak References.................................................................................................................................. 180 Pickling ..........................................................................................................................182 Copying..........................................................................................................................186 Shallow Copies ..................................................................................................................................... 187 Deep Copies.......................................................................................................................................... 188 Taking It With You..........................................................................................................190 Chapter 7: Strings...............................................................................................191 Bytes..............................................................................................................................191 Simple Conversion: chr() and ord() ....................................................................................................... 192 Complex Conversion: The Struct Module.............................................................................................. 193 Text................................................................................................................................195 Unicode................................................................................................................................................. 196 Encodings ............................................................................................................................................. 196
📄 Page
9
Simple Substitution .......................................................................................................198 Formatting .....................................................................................................................201 Looking Up Values Within Objects ........................................................................................................ 202 Distinguishing Types of Strings............................................................................................................ 202 Standard Format Specification ............................................................................................................. 203 Example: Plain Text Table of Contents ................................................................................................. 204 Custom Format Specification ............................................................................................................... 205 Taking It With You..........................................................................................................206 Chapter 8: Documentation ..................................................................................207 Proper Naming...............................................................................................................207 Comments......................................................................................................................208 Docstrings......................................................................................................................208 Describe What the Function Does ........................................................................................................ 209 Explain the Arguments ......................................................................................................................... 209 Don’t Forget the Return Value .............................................................................................................. 209 Include Any Expected Exceptions......................................................................................................... 210 Documentation Outside the Code ..................................................................................210 Installation and Configuration............................................................................................................... 210 Tutorials................................................................................................................................................ 210 Reference Documents .......................................................................................................................... 210 Documentation Utilities .................................................................................................211 Formatting ............................................................................................................................................ 212 Links ..................................................................................................................................................... 213 Sphinx................................................................................................................................................... 214 Taking It With You..........................................................................................................215 Chapter 9: Testing...............................................................................................217 Test-Driven Development (TDD) ....................................................................................217 Doctests.........................................................................................................................218
📄 Page
10
Formatting Code ................................................................................................................................... 218 Representing Output............................................................................................................................. 218 Integrating With Documentation........................................................................................................... 219 Running Tests....................................................................................................................................... 220 The unittest module.......................................................................................................221 Setting Up............................................................................................................................................. 221 Writing Tests......................................................................................................................................... 222 Other Comparisons ............................................................................................................................... 226 Testing Strings and Other Sequence Content....................................................................................... 226 Testing Exceptions ............................................................................................................................... 227 Testing Identity ..................................................................................................................................... 229 Tearing Down ....................................................................................................................................... 229 Providing a Custom Test Class ......................................................................................230 Changing Test Behavior........................................................................................................................ 230 Taking It With You..........................................................................................................231 Chapter 10: Distribution .....................................................................................233 Licensing .......................................................................................................................233 GNU General Public License (GPL) ........................................................................................................ 233 Affero General Public License (AGPL) ................................................................................................... 234 GNU Lesser General Public License (LGPL)........................................................................................... 235 Berkeley Software Distribution (BSD) License...................................................................................... 235 Other Licenses...................................................................................................................................... 236 Packaging ......................................................................................................................236 setup.py................................................................................................................................................ 237 MANIFEST.in ......................................................................................................................................... 239 The sdist command .............................................................................................................................. 240 Distribution ....................................................................................................................241 Taking It With You..........................................................................................................242
📄 Page
11
Chapter 11: Sheets: A CSV Framework...............................................................243 Building a Declarative Framework.................................................................................244 Introducing Declarative Programming.................................................................................................. 244 To Build or Not to Build?....................................................................................................................... 245 Building the Framework ................................................................................................246 Managing Options................................................................................................................................. 247 Defining Fields...................................................................................................................................... 249 Attaching a Field to a Class .................................................................................................................. 250 Adding a Metaclass .............................................................................................................................. 252 Bringing It Together.............................................................................................................................. 255 Ordering Fields ..............................................................................................................256 DeclarativeMeta.__prepare__()............................................................................................................ 256 Column.__init__() ................................................................................................................................. 258 Column.__new__() ............................................................................................................................... 262 CounterMeta.__call__() ........................................................................................................................ 263 Choosing an Option............................................................................................................................... 264 Building a Field Library..................................................................................................264 StringField ............................................................................................................................................ 265 IntegerColumn ...................................................................................................................................... 266 FloatColumn.......................................................................................................................................... 266 DecimalColumn .................................................................................................................................... 266 DateColumn .......................................................................................................................................... 267 Getting Back to CSV.......................................................................................................271 Checking Arguments ............................................................................................................................ 272 Populating Values ................................................................................................................................. 274 The Reader ........................................................................................................................................... 276 The Writer ............................................................................................................................................. 280 Taking It With You..........................................................................................................282
📄 Page
12
PEP 8: Style Guide for Python .............................................................................283 Introduction ...................................................................................................................283 A Foolish Consistency is the Hobgoblin of Little Minds .................................................283 Code Layout ...................................................................................................................284 Indentation............................................................................................................................................ 284 Tabs or Spaces? ................................................................................................................................... 284 Maximum Line Length .......................................................................................................................... 284 Blank Lines ........................................................................................................................................... 284 Encodings (PEP 263)............................................................................................................................. 285 Imports ..........................................................................................................................285 Whitespace in Expressions and Statements..................................................................286 Pet Peeves............................................................................................................................................ 286 Other Recommendations ...................................................................................................................... 287 Comments......................................................................................................................288 Block Comments................................................................................................................................... 289 Inline Comments................................................................................................................................... 289 Documentation Strings ..................................................................................................289 Version Bookkeeping .....................................................................................................290 Naming Conventions......................................................................................................290 Descriptive: Naming Styles................................................................................................................... 290 Prescriptive: Naming Conventions........................................................................................................ 291 Programming Recommendations ..................................................................................294 Copyright .......................................................................................................................297 PEP 10: Voting Guidelines...................................................................................299 Abstract .........................................................................................................................299 Rationale........................................................................................................................299 Voting Scores.................................................................................................................299
📄 Page
13
Copyright .......................................................................................................................300 PEP 20: The Zen of Python..................................................................................301 Abstract .........................................................................................................................301 The Zen of Python..........................................................................................................301 Easter Egg......................................................................................................................301 Copyright .......................................................................................................................302 PEP 257: Docstring Conventions.........................................................................303 Abstract .........................................................................................................................303 Rationale........................................................................................................................303 Specification..................................................................................................................303 What is a Docstring?............................................................................................................................. 303 One-Line Docstrings ............................................................................................................................. 304 Multi-Line Docstrings ........................................................................................................................... 305 Handling Docstring Indentation ............................................................................................................ 306 Copyright .......................................................................................................................307 Acknowledgments .........................................................................................................307 PEP 387: Backwards Compatibility Policy..........................................................309 Abstract .........................................................................................................................309 Rationale........................................................................................................................309 Backwards Compatibility Rules .....................................................................................309 Making Incompatible Changes ......................................................................................310 Copyright .......................................................................................................................311 PEP 3000: Python 3000 ......................................................................................313 Abstract .........................................................................................................................313 Naming ..........................................................................................................................313 PEP Numbering..............................................................................................................313
📄 Page
14
Timeline .........................................................................................................................313 Compatibility and Transition ..........................................................................................314 Implementation Language .............................................................................................315 Meta-Contributions........................................................................................................315 Copyright .......................................................................................................................315 PEP 3003: Python Language Moratorium ...........................................................317 Abstract .........................................................................................................................317 Rationale........................................................................................................................317 Details............................................................................................................................318 Cannot Change ..................................................................................................................................... 318 Case-by-Case Exemptions.................................................................................................................... 318 Allowed to Change................................................................................................................................ 318 Retroactive.....................................................................................................................319 Extensions .....................................................................................................................319 Copyright .......................................................................................................................319 Index...................................................................................................................321
📄 Page
15
C H A P T E R 1 1 Principles and Philosophy If it seems strange to begin a programming book with a chapter about philosophy, that’s actually evidence of why this chapter is so important. Python was created to embody and encourage a certain set of ideals that have helped guide the decisions of its maintainers and its community for nearly 20 years. Understanding these concepts will help you make the most out of what the language and its community have to offer. Of course, we’re not talking about Plato or Nietzsche here. Python deals with programming problems, and its philosophies are designed to help build reliable, maintainable solutions. Some of these philosophies are officially branded into the Python landscape, while others are guidelines commonly accepted by Python programmers, but all of them will help you write code that is powerful, easy to maintain and understandable to other programmers. The philosophies laid out in this chapter can be read from start to finish here, but don’t expect to commit them all to memory in one pass. The rest of this book will refer back here often, by illustrating which concepts come into play in various situations. After all, the real value of philosophy is understanding how to apply it when it matters most. The Zen of Python Perhaps the best known collection of Python philosophy was written by Tim Peters, long-time contributor to the language and its newsgroup, comp.lang.python.1 This Zen of Python condenses some of the most common philosophical concerns into a brief list that’s been recorded as both its own Python Enhancement Proposal (PEP)2 and within Python itself. Something of an easter egg, Python includes a module called this. >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. 1 http://propython.com/comp-lang-python/ 2 http://propython.coms/pep-20/
📄 Page
16
CHAPTER 1 PRINCIPLES AND PHILOSOPHY 2 Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! This list was primarily intended as a humorous accounting of Python philosophy, but over the years, numerous Python applications have used these guidelines to greatly improve the quality, readability and maintainability of their code. Just listing the Zen of Python is of little value, though, so the following sections will explain each idiom in more detail. Beautiful Is Better Than Ugly Perhaps it’s fitting that this first notion is arguably the most subjective of the whole bunch. After all, beauty is in the eye of the beholder, a fact which has been discussed for centuries. It serves as a blatant reminder that philosophy is far from absolute. Still, having something like this in writing provides a goal to strive for, which is the ultimate purpose of all these ideals. One obvious application of this philosophy is in Python’s own language structure, which minimizes the use of punctuation, instead preferring English words where appropriate. Another advantage is Python’s focus on keyword arguments, which help clarify function calls that would otherwise be difficult to understand. Consider the following two possible ways of writing the same code, and consider which one looks more beautiful. is_valid = form != null && form.is_valid(true) is_valid = form is not None and form.is_valid(include_hidden_fields=True) The second example reads a bit more like natural English, and explicitly including the name of the argument gives greater insight into its purpose. In addition to language concerns, coding style can be influenced by similar notions of beauty. The name is_valid, for example, asks a simple question, which the method can then be expected to answer with its return value. A name like validate would’ve been ambiguous because it would be an accurate name even if no value were returned at all. Being so subjective, however, it’s dangerous to rely too heavily on beauty as a criterion for a design decision. If other ideals have been considered and you’re still left with two workable options, certainly consider factoring beauty into the equation, but do make sure that other facets are taken into account first. You’ll likely find a good choice using some of the other criteria long before reaching this point. Explicit Is Better Than Implicit Though this notion may seem easier to interpret than beauty, it’s actually one of the trickier guidelines to follow. On the surface, it seems simple enough: don’t do anything the programmer didn’t explicitly command. Beyond just Python itself, frameworks and libraries have a similar responsibility because their code will be accessed by other programmers whose goals will not always be known in advance. Unfortunately, truly explicit code must account for every nuance of a program’s execution, from memory management to display routines. Some programming languages do expect that level of detail from their programmers, but Python doesn’t. In order to make the programmer’s job easier and allow you to focus on the problem at hand, there need to be some trade-offs along the way.
📄 Page
17
CHAPTER 1 PRINCIPLES AND PHILOSOPHY 3 In general, Python asks you to declare your intentions explicitly, rather than issue every command necessary to make that intention a reality. For example, when assigning a value to a variable, you don’t need to worry about setting aside the necessary memory, assigning a pointer to the value and cleaning up the memory once it’s no longer in use. Memory management is a necessary part of variable assignment, so Python takes care of it behind the scenes. Assigning the value is enough of an explicit declaration of intent to justify the implicit behavior. On the other hand, regular expressions in the Perl programming language automatically assign values to special variables any time a match is found. Someone unfamiliar with the way Perl handles that situation wouldn’t understand a code snippet that relies on it because variables would seem to come from thin air, with no assignments related to them. Python programmers try to avoid this type of implicit behavior in favor of more readable code. Because different applications will have different ways of declaring intentions, no single generic explanation will apply to all cases. Instead, this guideline will come up quite frequently throughout the book, clarifying how it would be applied to various situations. Simple Is Better Than Complex This is a considerably more concrete guideline, with implications primarily in the design of interfaces to frameworks and libraries. The goal here is to keep the interface as straightforward as possible, leveraging a programmer’s knowledge of existing interfaces as much as possible. For example, a caching framework could use the same interface as standard dictionaries rather than inventing a whole new set of method calls. Of course, there are many other applications of this rule, such as taking advantage of the fact that most expressions can evaluate to true or false without explicit tests. For example, the following two lines of code are functionally identical for strings, but notice the difference in complexity between them. if value is not None and value != '': if value: As you can see, the second option is much simpler to read and understand. All the situations covered in the first example will evaluate to false anyway, so the simpler test is just as effective. It also has two other benefits: it runs faster, having fewer tests to perform, and it also works in more cases, because individual objects can define their own method of determining whether they should evaluate to true or false. It may seem like this is something of a convoluted example, but it’s just the type of thing that comes up quite frequently. By relying on simpler interfaces, you can often take advantage of optimizations and increased flexibility while producing more readable code. Complex Is Better Than Complicated Sometimes, however, a certain level of complexity is required in order to get the job done. Database adapters, for example, don’t have the luxury of using a simple dictionary-style interface, but instead require an extensive set of objects and methods to cover all of their features. The important thing to remember in those situations is that complexity doesn’t necessarily require it to be complicated. The tricky bit with this one, obviously, is distinguishing between the two. Dictionary definitions of each term often reference the other, considerably blurring the line between the two. For the sake of this guideline, most situations tend to take the following view of the two terms. • Complex—made up of many interconnected parts. • Complicated—so complex as to be difficult to understand.
📄 Page
18
CHAPTER 1 PRINCIPLES AND PHILOSOPHY 4 So in the face of an interface that requires a large number of things to keep track of, it’s even more important to retain as much simplicity as possible. This can take the form of consolidating methods onto a smaller number of objects, perhaps grouping objects into more logical arrangements or even simply making sure to use names that make sense without having to dig into the code to understand them. Flat Is Better Than Nested This guideline might not seem to make sense at first, but it’s about how structures are laid out. The structures in question could be objects and their attributes, packages and their included modules or even code blocks within a function. The goal is to keep things as relationships of peers as much possible, rather than parents and children. For example, take the following code snippet, which illustrates the problem. if x > 0: if y > 100: raise ValueError("Value for y is too large.") else: return y else: if x == 0: return False else: raise ValueError("Value for x cannot be negative.") In this example, it’s fairly difficult to follow what’s really going on because the nested nature of the code blocks requires you to keep track of multiple levels of conditions. Consider the following alternative approach to writing the same code, flattening it out. if x > 0 and y > 100: raise ValueError("Value for y is too large.") elif x > 0: return y elif x == 0: return False else: raise ValueError("Value for x cannot be negative.") Notice how much easier it is to follow the logic in the second example because all the conditions are at the same level. It even saves two lines of code by avoiding the extraneous else blocks along the way. This is actually the main reason for the existence of the elif keyword; Python’s use of indentation means that complex if blocks can quickly get out of hand otherwise. Caution What might not be as obvious is that the refactoring of this example ends up testing x > 0 twice, where it was only performed once previously. If that test had been an expensive operation, such as a database query, refactoring it in this way would reduce the performance of the program, so it wouldn’t be worth it. This is covered in detail in a later guideline: Practicality Beats Purity.
📄 Page
19
CHAPTER 1 PRINCIPLES AND PHILOSOPHY 5 In the case of package layouts, flat structures can often allow a single import to make the entire package available under a single namespace. Otherwise, the programmer would need to know the full structure in order to find the particular class or function required. Some packages are so complex that a nested structure will help reduce clutter on each individual namespace, but it’s best to start flat and nest only when problems arise. Sparse Is Better Than Dense This principle largely pertains to the visual appearance of Python source code, favoring the use of whitespace to differentiate among blocks of code. The goal is to keep highly related snippets together, while separating them from subsequent or unrelated code, rather than simply having everything run together in an effort to save a few bytes on disk. In the real world, there are plenty of specific concerns to address, such as how to separate module- level classes or deal with one-line if blocks. Though no single set of rules will be appropriate for all projects, PEP-83 does specify many aspects of source code layout that help you adhere to this principle. It provides a number of hints on how to format import statements, classes, functions and even many types of expressions. It’s interesting to note that PEP-8 includes a number of rules about expressions in particular, which specifically encourage avoiding extra spaces. Take the following examples, taken straight from PEP-8. Yes: spam(ham[1], {eggs: 2}) No: spam( ham[ 1 ], { eggs: 2 } ) Yes: if x == 4: print x, y; x, y = y, x No: if x == 4 : print x , y ; x , y = y , x Yes: spam(1) No: spam (1) Yes: dict['key'] = list[index] No: dict ['key'] = list [index] The key to this apparent discrepancy is that whitespace is a valuable resource and should be distributed responsibly. After all, if everything tries to stand out in any one particular way, nothing really does stand out at all. If you use whitespace to separate even highly related bits of code like the above expressions, truly unrelated code isn’t any different from the rest. That’s perhaps the most important part of this principle and the key to applying it to other aspects of code design. When writing libraries or frameworks, it’s generally better to define a small set of unique types of objects and interfaces that can be reused across the application, maintaining similarity where appropriate and differentiating the rest. Readability Counts Finally, we have a principle everybody in the Python world can get behind, but that’s mostly because it’s one of the most vague in the entire collection. In a way, it sums up the whole of Python philosophy in one deft stroke, but it also leaves so much undefined that it’s worth examining a bit further. Readability covers a wide range of issues, such as the names of modules, classes, functions and variables. It includes the style of individual blocks of code and the whitespace between them. It can even 3 http://propython.com/pep-8/
📄 Page
20
CHAPTER 1 PRINCIPLES AND PHILOSOPHY 6 pertain to the separation of responsibilities among multiple functions or classes if that separation is done so that it’s more readable to the human eye. That’s the real point here: code gets read not only by computers, but also by humans who have to maintain it. Those humans have to read existing code far more often than they have to write new code, and it’s often code that was written by someone else. Readability is all about actively promoting human understanding of code. Development is much easier in the long run when everyone involved can simply open up a file and easily understand what’s going on in it. This seems like a given in organizations with high turnover, where new programmers must regularly read the code of their predecessors, but it’s true even for those who have to read their own code weeks, months or even years after it was written. Once we lose our original train of thought, all we have to remind us is the code itself, so it’s very valuable to take the extra time to make it easy to read. The best part is how little extra time it often takes. It can be as simple as adding a blank line between two functions or naming variables with nouns and functions with verbs. It’s really more of a frame of mind than a set of rules, though. A focus on readability requires you to always look at your code as a human being would, rather than only as a computer would. Remember the Golden Rule: do for others what you’d like them to do for you. Readability is random acts of kindness sprinkled throughout your code. Special Cases Aren’t Special Enough to Break the Rules Just as “Readability counts” is a banner phrase for how we should approach our code at all times, this principle is about the conviction with which we must pursue it. It’s all well and good to get it right most of the time, but all it takes is one ugly chunk of code to undermine all that hard work. What’s perhaps most interesting about this rule, though, is that it doesn’t pertain just to readability or any other single aspect of code. It’s really just about the conviction to stand behind the decisions you’ve made, regardless of what those are. If you’re committed to backward compatibility, internationalization, readability or anything else, don’t break those promises just because a new feature comes along and makes some things a bit easier. Although Practicality Beats Purity And here’s where things get tricky. The previous principle encourages you to always do the right thing, regardless of how exceptional one situation might be, where this one seems to allow exceptions whenever the right thing gets difficult. The reality is a bit more complicated, though, and merits some discussion. Up to this point, it seemed simple enough at a glance: the fastest, most efficient code might not always be the most readable, so you may have to accept subpar performance to gain code that’s easier to maintain. This is certainly true in many cases, and much of Python’s standard library is less than ideal in terms of raw performance, instead opting for pure Python implementations that are more readable and more portable to other environments, like Jython or IronPython. On a larger scale, though, the problem goes deeper than that. When designing a system at any level, it’s easy to get into a heads-down mode, where you focus exclusively on the problem at hand and how best to solve it. This might involve algorithms, optimizations, interface schemes or even refactorings, but it typically boils down to working on one thing so hard that you don’t look at the bigger picture for a while. In that mode, programmers commonly do what seems best within the current context, but when backing out a bit for a better look, those decisions don’t match up with the rest of the application as a whole. It’s not always easy to know which way to go at this point. Do you try to optimize the rest of the application to match that perfect routine you just wrote? Do you rewrite the otherwise-perfect function in hopes of gaining a more cohesive whole? Or do you just leave the inconsistency alone, hoping it
The above is a preview of the first 20 pages. Register to read the complete e-book.
Recommended for You
Loading recommended books...
Failed to load, please try again later