Pydon'ts – Write elegant Python code

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Would you like me to teach you how to write Python ? code that is more elegant, more effective, and more expressive than what you have been writing until now... for free? I am looking to teach Python to people who want to master Python to the best of their abilities. Do you? If you do, then I know I can help you improve your skills. Python has dozens of built-ins, and all of them have multiple usage patterns. No human knows all of them by heart, but my research bundled them up nicely in a book for you to reference and learn from. With the help of the +20 chapters and +260 pages of this book, you will learn more about all the built-ins and their usage patterns, and your code will become more elegant, it will become more effective, and it will also become more expressive.

Author(s): Rodrigo Girão Serrão
Publisher: Independently published
Year: 2021

Language: English
Pages: 344

Foreword
Pydon’t disrespect the Zen of Python
Zen of Python
References
Does elegance matter?
Introduction
Beware, opinions ahead
Elegance is not a dispensable luxury
Don’t write that, that’s unreadable
The right tool for the job
Optimise for rewrite
Conclusion
References
Code style matters
Introduction
Code style
Tools for your tool belt
Conclusion
References
Naming matters
Introduction
Naming conventions
PEP 8 recommendations
Standard names
Verbosity
Picking a name
Context is key
Practical example
Conclusion
References
Chaining comparison operators
Introduction
Chaining of comparison operators
Pitfalls
Ugly chains
Examples in code
Conclusion
References
Assignment expressions and the walrus operator :=
Walrus operator and assignment expressions
Examples in code
Conclusion
References
Truthy, Falsy, and bool
“Truthy” and “Falsy”
The __bool__ dunder method
Remarks
Examples in code
Conclusion
References
Deep unpacking
Introduction
Assignments
Examples in code
Conclusion
References
Unpacking with starred assignments
Starred Assignment
Examples in code
References
EAFP and LBYL coding styles
EAFP and LBYL
EAFP instead of LBYL?
Conclusion
References
Zip up
Introduction
How zip works
Zip is lazy
Three is a crowd
Mismatched lengths
Create a dictionary with zip
Examples in code
Conclusion
References
Enumerate me
Introduction
How enumerate works
Optional start argument
Unpacking when iterating
Examples in code
Conclusion
References
str and repr
str and repr
The __str__ and __repr__ dunder methods
Examples in code
Conclusion
References
Structural pattern matching tutorial
Introduction
Structural pattern matching Python could already do
Your first match statement
Pattern matching the basic structure
Matching the structure of objects
__match_args__
Wildcards
Naming sub-patterns
Traversing recursive structures
Conclusion
References
Structural pattern matching anti-patterns
Introduction
There should be only one obvious way to do it
A short and sweet if statement
Be smart(er)
Basic mappings
Conclusion
References
Watch out for recursion
Introduction
Watch out for recursion
Examples in code
Conclusion
References
Sequence indexing
Introduction
Sequence indexing
Maximum legal index and index errors
Negative indices
Indexing idioms
To index or not to index?
Best practices in code
Conclusion
Idiomatic sequence slicing
Introduction
Slicing syntax
What to slice?
Slicing from the beginning
Slicing until the end
Slicing with negative indices
Slicing and range
Idiomatic slicing patterns
Empty slices
More empty slices
Examples in code
Conclusion
References
Mastering sequence slicing
Introduction
Slicing step
Sequence copying
Manipulating mutable sequences
More idiomatic slicing
Conclusion
References
Inner workings of sequence slicing
Introduction
The slice class
Getting items from sequences
Setting items, deleting items, and container emulation
Comma-separated indices and slices
Examples in code
Conclusion
References
Boolean short-circuiting
Introduction
Return values of the and and or operators
Short-circuiting
Short-circuiting in plain English
all and any
Short-circuiting in chained comparisons
Examples in code
Conclusion
References
The power of reduce
Introduction
How reduce works
The rabbit hole of the built-in reductions
Why bother?
Far-fetched reductions
The identity element…
Why some people dislike reduce
Examples in code
Conclusion
References
Usages of underscore
Introduction
Recovering last result in the session
Prefixes and suffixes for variable names
Underscore as a sink
Matching everything in the new match statement
String localisation
Improve number readability
Conclusion
References
name dunder attribute
Introduction
What is __name__?
The module attribute __name__
__name__ as an object type attribute
Examples in code
Conclusion
References
Bite-sized refactoring
Introduction
Refactoring
What to refactor?
Case study
Conclusion
References
String translate and maketrans methods
Introduction
str.translate
Non-equivalence to str.replace
Generic translation tables
str.maketrans
Examples in code
Conclusion
References
Boost your productivity with the REPL
Introduction
REPL
Just fire up the REPL
REPL mechanics
The last result
Getting help from within the REPL
Tips for quick hacks
Other tools
Conclusion
References
set and frozenset
Introduction
(Mathematical) sets
(Common) Operations on sets
Differences between set and frozenset
What are sets used for?
Examples in code
Conclusion
References
List comprehensions 101
Introduction
What is a list comprehension?
Anatomy of a list comprehension
Example list comprehensions without filtering
Example list comprehensions with filtering
Equivalence with for loops
Nesting for loops
Nesting if statements
Arbitrary nesting
List comprehensions instead of map and filter
Examples in code
Conclusion
References
Conditional expressions
Introduction
What is a conditional expression?
Rationale
Short-circuiting
Conditional expressions and if statements
Precedence
Conditional expressions that evaluate to Booleans
Examples in code
Conclusion
References
Pass-by-value, reference, and assignment
Introduction
Is Python pass-by-value?
Is Python pass-by-reference?
Python object model
Python is pass-by-assignment
Making copies
Examples in code
Conclusion
References
String formatting comparison
Introduction
String formatting rationale
Three string formatting methods
Value conversion
Alignment
Named placeholders
Accessing nested data structures
Parametrised formatting
Custom formatting
Examples in code
Conclusion
References
Closing thoughts