Better Python Code
Home > Computing and Information Technology > Computer programming / software engineering > Programming and scripting languages: general > Better Python Code: A Guide for Aspiring Experts
Better Python Code: A Guide for Aspiring Experts

Better Python Code: A Guide for Aspiring Experts

|
     0     
5
4
3
2
1




International Edition


About the Book

Move Beyond Python Code That "Mostly Works" to Code That Is Expressive, Robust, and Efficient Python is arguably the most-used programming language in the world, with applications from primary school education to workaday web development, to the most advanced scientific research institutes. While there are many ways to perform a task in Python, some are wrong, inelegant, or inefficient. Better Python Code is a guide to "Pythonic" programming, a collection of best practices, ways of working, and nuances that are easy to miss, especially when ingrained habits are borrowed from other programming languages. Author David Mertz presents concrete and concise examples of various misunderstandings, pitfalls, and bad habits in action. He explains why some practices are better than others, based on his 25+ years of experience as an acclaimed contributor to the Python community. Each chapter thoroughly covers related clusters of concepts, with chapters sequenced in ascending order of sophistication. Whether you are starting out with Python or are an experienced developer pushing through the limitations of your Python code, this book is for all who aspire to be more Pythonic when writing better Python code. Use the right kind of loops in Python Learn the ins and outs of mutable and immutable objects Get expert advice to avoid Python "gotchas" Examine advanced Python topics Navigate the "attractive nuisances" that exist in Python Learn the most useful data structures in Python and how to avoid misusing them Avoid security mistakes Understand the basics of numeric computation, including floating point numbers and numeric datatypes "My high expectations for this engaging Python book have been exceeded: it offers a great deal of insight for intermediate or advanced programmers to improve their Python skills, includes copious sharing of precious experience practicing and teaching the language, yet remains concise, easy to read, and conversational." --From the Foreword by Alex Martelli Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.

Table of Contents:
Foreword xvii Preface xix Acknowledgments xxv About the Author xxvii Introduction 1 Chapter 1: Looping Over the Wrong Things 3 1.1 (Rarely) Generate a List for Iteration 3 1.2 Use enumerate() Instead of Looping Over an Index 6 1.3 Don't Iterate Over dict.keys() When You Want dict.items() 8 1.4 Mutating an Object During Iteration 9 1.5 for Loops Are More Idiomatic Than while Loops 12 1.6 The Walrus Operator for "Loop-and-a-Half" Blocks 13 1.7 zip() Simplifies Using Multiple Iterables 15 1.8 zip(strict=True) and itertools.zip_longest() 17 1.9 Wrapping Up 20 Chapter 2: Confusing Equality with Identity 21 2.1 Late Binding of Closures 21 2.2 Overchecking for Boolean Values 25 2.3 Comparing x == None 28 2.4 Misunderstanding Mutable Default Arguments 29 2.5 Copies versus References to Mutable Objects 33 2.6 Confusing is with == (in the Presence of Interning) 35 2.7 Wrapping Up 37 Chapter 3: A Grab Bag of Python Gotchas 39 3.1 Naming Things 39 3.2 Quadratic Behavior of Naive String Concatenation 52 3.3 Use a Context Manager to Open a File 56 3.4 Optional Argument key to .sort() and sorted() 59 3.5 Use dict.get() for Uncertain Keys 62 3.6 Wrapping Up 64 Chapter 4: Advanced Python Usage 67 4.1 Comparing type(x) == type(y) 67 4.2 Naming Things (Revisited) 71 4.3 Keep Less-Used Features in Mind 79 4.4 Type Annotations Are Not Runtime Types 98 4.5 Wrapping Up 105 Chapter 5: Just Because You Can, It Doesn't Mean You Should... 107 5.1 Metaclasses 107 5.2 Monkeypatching 112 5.3 Getters and Setters 115 5.4 It's Easier to Ask for Forgiveness Than Permission 118 5.5 Structural Pattern Matching 121 5.6 Regular Expressions and Catastrophic Backtracking 123 5.7 Wrapping Up 126 Chapter 6: Picking the Right Data Structure 129 6.1 collections.defaultdict 129 6.2 collections.Counter 132 6.3 collections.deque 135 6.4 collections.ChainMap 138 6.5 Dataclasses and Namedtuples 141 6.6 Efficient Concrete Sequences 146 6.7 Wrapping Up 150 Chapter 7: Misusing Data Structures 153 7.1 Quadratic Behavior of Repeated List Search 153 7.2 Deleting or Adding Elements to the Middle of a List 157 7.3 Strings Are Iterables of Strings 163 7.4 (Often) Use enum Rather Than CONSTANT 166 7.5 Learn Less Common Dictionary Methods 169 7.6 JSON Does Not Round-Trip Cleanly to Python 174 7.7 Rolling Your Own Data Structures 178 7.8 Wrapping Up 187 Chapter 8: Security 189 8.1 Kinds of Randomness 190 8.2 Putting Passwords or Other Secrets in "Secure" Source Code 195 8.3 "Rolling Your Own" Security Mechanisms 198 8.4 Use SSL/TLS for Microservices 201 8.5 Using the Third-Party requests Library 205 8.6 SQL Injection Attacks When Not Using DB-API 208 8.7 Don’t Use assert to Check Safety Assumptions 212 8.8 Wrapping Up 215 Chapter 9: Numeric Computation in Python 217 9.1 Understanding IEEE-754 Floating Point Numbers 217 9.2 Numeric Datatypes 228 9.3 Wrapping Up 239 Appendix: Topics for Other Books 241 A.1 Test-Driven Development 241 A.2 Concurrency 242 A.3 Packaging 243 A.4 Type Checking 243 A.5 Numeric and Dataframe Libraries 244 Index 245


Best Sellers


Product Details
  • ISBN-13: 9780138320942
  • Publisher: Pearson Education (US)
  • Publisher Imprint: Addison Wesley
  • Height: 232 mm
  • No of Pages: 288
  • Spine Width: 12 mm
  • Weight: 552 gr
  • ISBN-10: 0138320942
  • Publisher Date: 05 Dec 2023
  • Binding: Paperback
  • Language: English
  • Returnable: Y
  • Sub Title: A Guide for Aspiring Experts
  • Width: 178 mm


Similar Products

Add Photo
Add Photo

Customer Reviews

REVIEWS      0     
Click Here To Be The First to Review this Product
Better Python Code: A Guide for Aspiring Experts
Pearson Education (US) -
Better Python Code: A Guide for Aspiring Experts
Writing guidlines
We want to publish your review, so please:
  • keep your review on the product. Review's that defame author's character will be rejected.
  • Keep your review focused on the product.
  • Avoid writing about customer service. contact us instead if you have issue requiring immediate attention.
  • Refrain from mentioning competitors or the specific price you paid for the product.
  • Do not include any personally identifiable information, such as full names.

Better Python Code: A Guide for Aspiring Experts

Required fields are marked with *

Review Title*
Review
    Add Photo Add up to 6 photos
    Would you recommend this product to a friend?
    Tag this Book Read more
    Does your review contain spoilers?
    What type of reader best describes you?
    I agree to the terms & conditions
    You may receive emails regarding this submission. Any emails will include the ability to opt-out of future communications.

    CUSTOMER RATINGS AND REVIEWS AND QUESTIONS AND ANSWERS TERMS OF USE

    These Terms of Use govern your conduct associated with the Customer Ratings and Reviews and/or Questions and Answers service offered by Bookswagon (the "CRR Service").


    By submitting any content to Bookswagon, you guarantee that:
    • You are the sole author and owner of the intellectual property rights in the content;
    • All "moral rights" that you may have in such content have been voluntarily waived by you;
    • All content that you post is accurate;
    • You are at least 13 years old;
    • Use of the content you supply does not violate these Terms of Use and will not cause injury to any person or entity.
    You further agree that you may not submit any content:
    • That is known by you to be false, inaccurate or misleading;
    • That infringes any third party's copyright, patent, trademark, trade secret or other proprietary rights or rights of publicity or privacy;
    • That violates any law, statute, ordinance or regulation (including, but not limited to, those governing, consumer protection, unfair competition, anti-discrimination or false advertising);
    • That is, or may reasonably be considered to be, defamatory, libelous, hateful, racially or religiously biased or offensive, unlawfully threatening or unlawfully harassing to any individual, partnership or corporation;
    • For which you were compensated or granted any consideration by any unapproved third party;
    • That includes any information that references other websites, addresses, email addresses, contact information or phone numbers;
    • That contains any computer viruses, worms or other potentially damaging computer programs or files.
    You agree to indemnify and hold Bookswagon (and its officers, directors, agents, subsidiaries, joint ventures, employees and third-party service providers, including but not limited to Bazaarvoice, Inc.), harmless from all claims, demands, and damages (actual and consequential) of every kind and nature, known and unknown including reasonable attorneys' fees, arising out of a breach of your representations and warranties set forth above, or your violation of any law or the rights of a third party.


    For any content that you submit, you grant Bookswagon a perpetual, irrevocable, royalty-free, transferable right and license to use, copy, modify, delete in its entirety, adapt, publish, translate, create derivative works from and/or sell, transfer, and/or distribute such content and/or incorporate such content into any form, medium or technology throughout the world without compensation to you. Additionally,  Bookswagon may transfer or share any personal information that you submit with its third-party service providers, including but not limited to Bazaarvoice, Inc. in accordance with  Privacy Policy


    All content that you submit may be used at Bookswagon's sole discretion. Bookswagon reserves the right to change, condense, withhold publication, remove or delete any content on Bookswagon's website that Bookswagon deems, in its sole discretion, to violate the content guidelines or any other provision of these Terms of Use.  Bookswagon does not guarantee that you will have any recourse through Bookswagon to edit or delete any content you have submitted. Ratings and written comments are generally posted within two to four business days. However, Bookswagon reserves the right to remove or to refuse to post any submission to the extent authorized by law. You acknowledge that you, not Bookswagon, are responsible for the contents of your submission. None of the content that you submit shall be subject to any obligation of confidence on the part of Bookswagon, its agents, subsidiaries, affiliates, partners or third party service providers (including but not limited to Bazaarvoice, Inc.)and their respective directors, officers and employees.

    Accept

    New Arrivals

    Inspired by your browsing history


    Your review has been submitted!

    You've already reviewed this product!