Pythonic Programming Tips for Becoming an Idiomatic Python Programmer 1st Edition Dmitry Zinoviev - Get instant access to the full ebook with detailed content
Pythonic Programming Tips for Becoming an Idiomatic Python Programmer 1st Edition Dmitry Zinoviev - Get instant access to the full ebook with detailed content
com
https://ebookmeta.com/product/pythonic-programming-tips-for-
becoming-an-idiomatic-python-programmer-1st-edition-dmitry-
zinoviev/
OR CLICK HERE
DOWLOAD EBOOK
https://ebookmeta.com/product/pythonic-programming-tips-for-becoming-
an-idiomatic-python-programmer-1st-edition-dmitry-zinoviev-2/
ebookmeta.com
https://ebookmeta.com/product/resourceful-code-reuse-write-compile-
link-run-1st-edition-dmitry-zinoviev-2/
ebookmeta.com
https://ebookmeta.com/product/resourceful-code-reuse-write-compile-
link-run-1st-edition-dmitry-zinoviev/
ebookmeta.com
https://ebookmeta.com/product/english-and-translation-in-the-european-
union-unity-and-multiplicity-in-the-wake-of-brexit-1st-edition-alice-
leal/
ebookmeta.com
The Problem of Property: Taking the Freedom of Nonowners
Seriously 1st Edition Karl Widerquist
https://ebookmeta.com/product/the-problem-of-property-taking-the-
freedom-of-nonowners-seriously-1st-edition-karl-widerquist/
ebookmeta.com
https://ebookmeta.com/product/natural-language-processing-a-machine-
learning-perspective-yue-zhang/
ebookmeta.com
https://ebookmeta.com/product/bite-of-the-truth-the-black-fan-2-1st-
edition-laura-greenwood/
ebookmeta.com
Earth Observation Data Analytics Using Machine and Deep
Learning: Modern Tools, Applications and Challenges 1st
Edition Sanjay Garg
https://ebookmeta.com/product/earth-observation-data-analytics-using-
machine-and-deep-learning-modern-tools-applications-and-
challenges-1st-edition-sanjay-garg/
ebookmeta.com
Pythonic Programming
Tips for Becoming an Idiomatic
Python Programmer
by Dmitry Zinoviev
Every precaution was taken in the preparation of this book. However, the publisher
assumes no responsibility for errors or omissions, or for damages that may result from
the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team
create better software and have more fun. For more information, as well as the latest
Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been
DRM-free. We pioneered the beta book concept, where you can purchase and read a
book while it’s still being written, and provide feedback to the author to help make a
better book for everyone. Free resources for all purchasers include source code
downloads (if applicable), errata and discussion forums, all available on the book's
home page at pragprog.com. We’re here to make your life easier.
Acknowledgments
Preface
Introduction
About the Software
About the Notation
About the Reader
1. Documentation Tips
Tip
1. Hello, World!
Tip
2. Import This
Tip
3. Know Ownership and Licensing
Tip
4. Use Quotes of All Sorts
Tip
5. Keep Letter Case Consistent
Tip
6. Wrap Long Lines
Tip
7. Self-Document Your Code
Tip
8. Do Not Misuse Docstrings
Tip
9. Let input() Speak for Itself
Tip
10. Mark Dummy Variables
Tip
11. Distinguish Parameters and Arguments
Tip
12. Avoid “Magic” Values
Tip
13. Enumerate Your Constants
2. General Tips
Tip
14. Chain Comparison Operators
Tip
15. Expand the Tabs
Tip
16. Pickle It
Tip
17. Avoid range() in Loops
Tip
18. Pass It
Tip
19. Try It
Tip
20. Embrace Comprehensions
Tip
21. Make Your Code Compact with Conditional
Expressions
Tip
22. Find the “Missing” Switch
Tip
23. Eschew Comprehension Expressions, If
Needed
Tip
24. Use Slicing to Reverse and Split
Tip
25. sum() Almost Anything
Tip
26. Transpose with zip()
Tip
27. Discover All Characters in One Place
Tip
28. glob() the Files
Tip
29. Use Strings as Files
Tip
30. Pick to str() or to repr()
Tip
31. Remember, input() Remembers
Tip
32. Do Linear Algebra in Python
Tip
33. Construct a One-Element Tuple
Tip
34. Improve Readability with Raw Strings
Tip
35. Unpack Lists and Tuples
Tip
36. Print a List
Tip
37. Flatten That List
Tip
38. Treat Your Code as a Module
Tip
39. Let Modules Act Independently
Tip
40. Add Class Attributes
Tip
41. Serialize Objects
Tip
42. Count with defaultdict
Tip
43. Count with Counter
Tip
44. Explore How int() Supports Other Bases
Tip
45. Discover Complex Numbers
Tip
46. Rational Numbers Exist
Tip
47. Discover an Infinity
Tip
48. Carve It in Stone
Tip
49. No Trees? Use a dict()
4. Function Tips
Tip
50. Make Functions Always Return Something
Tip
51. Return Consistently
Tip
52. Let the Caller Print
Tip
53. Return Many Values
Tip
54. Understand Optional and Keyword
Parameters
Tip
55. Pass Arguments Your Way
Tip
56. Omit Else After Return
Tip
57. Chain Function Calls
Tip
58. Yield, Do Not Return
Tip
59. Return and Apply Functions
Tip
60. Savvy Anonymous Functions
5. Performance Tips
Tip
61. Time It
Tip
62. Avoid Function Calls; They Are Costly
Tip
63. Build, Then Print
Tip
64. Format with Formatted Strings
Tip
65. Import Wisely
Tip
66. Import as Needed
Tip
67. Optimize Lookups
Tip
68. Cache It
Tip
69. Checkpoint, It Saves Time
Tip
70. Sort Big in Place
Tip
71. Delete Your Garbage
Tip
72. Beware of Large and Slow Ints
Tip
73. Waste Space, Save Time
Tip
74. Do Not str() a str
6. Safety Tips
Tip
75. Call That Function
Tip
76. Get the Hang of Local Variables
Tip
77. Grasp What Is Truth
Tip
78. Check for Range
Tip
79. Strip User Input
Tip
80. Let Python Close Your Files
Tip
81. Read Files Safely
Tip
82. Hide Everything
Tip
83. Use Properties
Tip
84. Compare One to Many
Tip
85. Check, Then Touch
Tip
86. Assert Conditions
Tip
87. Do Not eval(); It Is Evil
Tip
88. Parse with literal_eval()
Tip
89. Treat Variables as References
Tip
90. Isolate Exceptions
Tip
91. Compare Prefixes and Suffixes
Tip
92. Remember, There Are No Globals
Tip
93. Is Is Not What You Think It Is
Tip
94. Distinguish type() and isinstance()
Tip
95. 50,000 Is Not a Number, but 50_000 Is
Tip
96. Do Not Confuse Boolean and Bitwise
Operators
Tip
97. Do Not Call Your List “List”
Tip
98. Do Not Change That Loop Variable
Tip
99. str.split() by White Spaces
Tip
100
. Get over str.split()
7. Wrapping Up
Bibliography
→ Dmitri Tcherevik
Founder and CEO, Morebell, Inc.
There are at least two reasons to read this book. First, it equips
the reader with dozens of useful Python idioms that can and
should be used on a daily basis by any practitioner. Second (and
in my opinion even more important) is that it provides a
consistent view of the language spirit and instills in the reader a
true Pythonic view of the world of modern programming.
→ Ilya Usvyatsky
Senior Software Development Engineer, Amazon Web
Services
As someone who uses Python to automate security operations, I
find the tips in this book useful in writing and maintaining clean
code for my scripts. I would recommend it especially to people
who already know Python but are looking for tips in writing
Pythonic code.
→ Dan Wanjohi
Security Engineer, World Bank Group
This is a great collection of unusual and useful Python features
and a great chance to reflect about nonobvious points. Newbies
will find a lot of new and useful information, and experienced
developers will find new information and be reminded of most
useful tips.
→ Evgenii Kozhanov
Software Engineer, Inplat Technologies Ltd.
This is a wonderful book. In highly accessible and always fun
prose, it explores all the nooks and crannies of Python
programming, exploring not only how it works but also what it
means.
→ Aditi Singh
Graduate Assistant, Kent State University
Acknowledgments
This book is my third Pragmatic book produced in enjoyable
cooperation with the outstanding editor Adaobi Obi Tulton. I can’t
stop admiring her helpfulness, mastery of the language, knowledge
of the procedures, and willingness to understand the subject.
The book was inspired by many students that I had taught, tutored,
and advised in the last twenty years, too plentiful to acknowledge by
name. However, I must explicitly mention Andrea Olsen and Anna
Nyulund: they were the muses of the book who triggered me into
writing the manuscript.
Thank you!
Yours, D.Z.
Professor
mailto:[email protected]
Boston, April 2021
Footnotes
[1] https://stackoverflow.com
Every programming language and system has its spirit. The spirit of
FORTRAN 66/77 is bulky multidimensional arrays of real numbers,
uppercase letters, and a lack of recursion. The spirit of C is pointers
and the happy sisters malloc and free. The spirit of Java is pages-long
classes and the Java virtual machine. What is the spirit of Python,
then?
This book offers almost one hundred tips that explain how to write
Pythonic code in the namesake language. It is hard to explain what
“Pythonic” means. Just like Zen that I mention in Tip 2, Import This
and elsewhere in the book, “pythonicity” (yes, there is such word!) is
an epiphany, an enlightenment that is not learned but experienced.
Hopefully, after browsing or reading the book, you will become a
more Pythonic programmer—and, therefore, a better Python
programmer in general.
The tips are grouped into six chapters: documentation tips, data
types/data structures tips, safety tips, performance tips, function
design tips, and general tips. They embrace different aspects of
pythonicity: how to make your programs correct, safe, fast, easy to
read, and easy to maintain.
About the Software
All you need is Python. Almost any Python suffices. Ninety-five
percent of the tips are compatible with any currently supported
version (3.4 and above) and with 2.7, which is not supported
anymore but is still used in legacy software. Five percent of the tips
work only for Python 3.4 and above. And there is only one
exception: Tip 64, Format with Formatted Strings—that requires
Python 3.6.
About the Notation
Each tip in the book comes with a brief “stars-and-numbers”
annotation that describes its complexity and compatibility. The
number of stars ranges from one ★ (a simple, almost trivial tip) to
three ★★★ (an advanced tip). Naturally, the star ratings are
subjective, but rest assured that any one-star tip is much simpler
than any three-star tip. Most of the tips belong to the middle
category.
Is that you? Enjoy the book! Are you someone else? Enjoy it all the
same!
Footnotes
[2] https://www.tiobe.com/tiobe-index/
Documentation Tips
Software documentation is essential. A poorly documented program
is hard to understand and, as a result, hard to maintain. Python is
proud of being a self-documented language—which is not the same
as a self-documenting language. You, as a programmer, are still in
charge of developing the documentation.
import__hello__
Hello world!
=
>
I hear you ask, What is the point of having this useless module?
__hello__ is an Easter egg: an unexpected or undocumented feature
in computer software included as a joke. Jokes are fun, and
programming is fun, too. Let’s have fun.
Tip 2 Import This
★2.7, 3.4+ Every Python installation comes with the module named
this. The module has nothing to do with the namesake Java or C++
keyword denoting a reference to a class object. The module contains
The Zen of Python—a set of guiding principles that define the
“Pythonic” programming style. Just like with any other kind of Zen,
the Zen of Python is learned by practicing. Practicing meditation
does not hurt, but practicing good programming helps. Let’s start
our practice by looking at the principles first. Type:
importthis
credits()
And how about the current legal status of the product? Is it legally
suitable for your needs? Type license. I do not include the other
functions’ output because it may differ for different builds of Python.
But there seems to be a function for every occasion.
Tip 4 Use Quotes of All Sorts
★2.7, 3.4+ You can enclose string literals in Python in four types of
quotation marks. Single and double quotation marks define single-
line strings. Line breaks are not allowed within:
'''Mary
had a little
lamb'''
" ""Mary
had a little
lamb"""
my_cat_s_age = 11
myCatSAge = 11
_var = 'I do not care'
defsilly_function():
pass
defanotherSillyFunction():
pass
importmyjunk
Use CapitalizedWords for classes (but not for class objects). Here is
a class identifier:
c
lassADoNothingClass:
ass
p
Readability counts!
Tip 6 Wrap Long Lines
★2.7, 3.4+ Python is known as a language of one-liners: statements
that consist of only one line. That line, for sure, may be quite long. A
long line does not fit your IDE or text editor’s window and is hard to
read. If you have to use a long line statement, break it into several
lines. Python treats a single backslash “\” at the very end of a line as
a continuation symbol—it is ignored, and the line break that follows
it is ignored too.
1+2+3+4\
+5+6
s.lower().strip() \
.split()
dir( \
)
Note that if you typed an opening bracket (“[”, “(”, or “{”) that has
not been closed yet, there is no need for the continuation symbol:
Python will patiently wait until you restore the balance:
If you put any spaces in front of the "world" in the previous example
(as I did), they will become a part of the string! Only the rest of the
wrapped line is removed, not the beginning of the continuation.
Tip 7 Self-Document Your Code
★2.7, 3.4+ A module, function, method, and class can and should be
self-documented. Self-documentation is a feature that allows you to
combine object implementation and object description in one piece
of code. As a courtesy to yourself and your future customers, always
self-document everything that can be self-documented!
d
eflen(obj):
'Return the number of items in a container.'
# Do something
print(len.__doc__)
Return the number of items in a container.
=
>
Note that the parentheses do not follow the function name. You do
not call the function here; you refer to its attribute. In Python,
functions and methods are objects and have attributes too.
Second, you can call the built-in function help and pass the object in
question as an argument. Again, do not call the object if it is a
function or a method.
help(len)
As a bonus, you get the expected parameters and the name of the
module in which the object is defined.
Tip 8 Do Not Misuse Docstrings
★2.7, 3.4+ Docstrings are a form of documentation, but you should
use them only to describe the purpose of having objects and the
way of using them. For everything else, such as explaining the
implementation detail and design choices, there are comments.
# This is a comment
'''
This is not a comment
'''
'This is not a comment, either'
You can use function print to display the prompt, followed by input to
collect the input. A better solution is to let input speak for itself; if
you pass an argument to input, the argument will be printed just
before the function stops:
It may well be that bluff King Harry relished more the homely
Saxonism of such psalms than the Stabat Maters and Te Deums and
Jubilates, which assuredly would have better pleased the Princess
Katharine of Aragon. Yet even at a time when the writers of such
psalmodies received small crumbs of favor from the Court, the
English Bible was by no means a free-goer into all companies.
“A nobleman or gentleman may read it”—(I quote
from a Statute of Henry VIII.’s time)—“in his house, or
in his garden, or orchard, yet quietly and without
disturbance of order. A merchant may read it to
himself privately: But the common people, women,
artificers, apprentices, journeymen and servingmen,
are to be punished with one month’s imprisonment, as
often as they are detected in reading the Bible, either
privately or openly.”[80]
Truly this English realm was a strange one in those times, and this
a strange King—who has listened approvingly to Hugh Latimer’s
sermons—who harries Tyndale as he had harried Tyndale’s enemy—
More; who fights the Pope, fights Luther, holds the new Bible (even
Cranmer’s) in leash, who gives pension to Sternhold, works easy
riddance of all the wives he wishes, pulls down Religious Houses for
spoils, calls himself Defender of the Faith, and maybe goes to see (if
then on show) Gammer Gurton’s Needle,[81] and is hilariously
responsive to such songs as this:—
“I cannot eat but little meat
My Stomach is not good
But sure I think, that I can drink
With him that wears a hood;
Tho’ I go bare, take ye no care
I nothing am a colde,
I stuffe my skin so full within
Of jolly good ale and olde.”
I within whose first quarter three great kings held three great
thrones:—Charles V. of Spain, Francis I. of France, and Henry
VIII. of England. New questions were astir; Art—in the seats of
Art—was blazing at its best: the recent fall of Constantinople under
the Turk had sent a tide of Greek scholars, Greek art, and Greek
letters flowing over Western Europe, and drifting into the antiquated
courts of Oxford and Cambridge. I spoke of the magnificent Wolsey,
and of his great university endowments; also, of that ripe scholar, Sir
Thomas More, who could not mate his religion, or his statesmanship
with the caprices of the King, and so, died by the axe. We saw
Cranmer—meaning to be good, if goodness did not call for strength;
we heard Latimer’s swift, homely speech, and saw Tyndale with his
English Testament—both these coming to grief; and we had
glimpses of John Knox shaking the pulpit with his frail hand, and
shaking all Scotch Christendom with his fearless, strident speech.
We heard the quaint psalmody of Sternhold, and the sweeter and
more heathen verse of Wyatt and of Surrey; lastly, I gave a sketch of
that old schoolmaster, Roger Ascham, who by his life, tied the reigns
of Henry and of Elizabeth together, and who taught Greek and Latin
and penmanship and Archery to that proud princess—whom we
encounter now—in her high ruff, and her piled-up head-dress, with a
fair jewelled hand that puts a man’s grip upon the sceptre.
Elizabethan England.
Elizabeth was in her twenty-sixth year when she came to the
throne, and it was about the middle of the sixteenth century; the
precise year being 1558. The England she was to dominate so
splendidly was not a quiet England: the fierce religious controversies
which had signalized the reign of Henry VIII.—who thwacked with
his kingly bludgeon both ways and all ways—and which continued
under Edward VI.—who was feebly Protestant; and which had
caught new vigor under Mary—who was arrant and slavish Papist—
had left gouts of blood and a dreadful exasperation. Those great
Religious Houses, which only a quarter of a century before, were
pleasantly embayed in so many charming valleys of Great Britain—
with their writing-rooms, their busy transcribing clerks—their great
gardens, were, most of them, despoiled—and to be seen no more.
An old Venetian Ambassador,[85] writing to the Seigneury in those
days, says—“London itself is disfigured by the ruins of a multitude of
Churches and Monasteries which once belonged to Friars and Nuns.”
Piers Plowman, long before, had attacked the sins growing up in the
pleasant Abbey Courts; Chaucer had echoed the ridicule in his Abbot
riding to Canterbury, with jingling trappings: Gower had repeated
the assault in his Vox Clamantis, and Skelton had turned his ragged
rhymes into the same current of satire. But all would have availed
nothing except the arrogant Henry VIII. had set his foot upon them,
and crushed them out.
There was a wild justice in it—if not an orderly one. The spoils
went to fill the Royal coffers; many of those beautiful properties
were bestowed upon favorites; many princely estates are still held in
England, by title tracing back to those days of spoliation—a fact
which will be called to mind, I suspect—with unction, in case of any
great social revolution in that country. Under Mary, some of these
estates had been restored to Church dignitaries; but the restoration
had not been general: and Elizabeth could not if she would, and
would not if she could, sanction any further restitution.
She was Protestant—but rather from policy than any heartiness of
belief. It did not grieve her one whit, that her teacher, Roger
Ascham, had been private secretary to bloody Mary: the
lukewarmness of her great minister, Lord Burleigh, did not disturb
her; she always kept wax tapers burning by a crucifix in her private
chamber; a pretty rosary gave her no shock; but she was shocked at
the marriage of any member of the priesthood, always. In fact, if
Spanish bigotry had not forced her into a resolute antagonism of
Rome, I think history would have been in doubt whether to count
her most a Lutheran, or most a Roman.
Yet she made the Papists smoke for it—as grimly as ever her sister
Mary did the Protestants—if they stood one whit in the way of
England’s grasp on power.
Edmund Spenser.
As for Edmund Spenser, he was a year older at this date—twenty-
five: he had taken his master’s degree at Cambridge and had just
returned to London from a visit to the North of England, where he
had encountered some fair damsel to whom he had been paying
weary and vain suit, and whom he had embalmed in his Shepherd’s
Calendar (just then being made ready for the press) under the name
of Rosalind.
and his tears keep a-drip through a great many of those charming
eclogues—called the Shepherd’s Calendar. Some of the
commentators on Spenser have queried—gravely—whether he ever
forgot this Rosalind; and whether the occurrence of the name and
certain woe-worn words in some madrigal of later years did not
show a wound unhealed and bleeding. We are all at liberty to guess,
and I am inclined to doubt here. I think he was equal to forgetting
this Rosalind before the ink of the Shepherd’s Calendar was fairly
dry. He loved dreams and fed on dreams; and I suspect enjoyed the
dream of his woe more than he ever suffered from a sting of rebuff.
Indeed, much as we must all admire his poetic fervor and fancies,
I do not find in him traces of heroic mould;—easily friendly rather
than firmly so;—full of an effusive piety, but not coming in way of
martyrdom for faith’s sake;—a tenderly contemplative man, loving
and sensing beauty in the same sure and abounding way in which
Turner has sense of color—exhaustless in his stock of brilliant and
ingenious imagery—running to similes as mountain rills run to rivers;
a courtier withal—honeyed and sometimes fulsome; a richly
presentable man (if portraits may be trusted), with a well-trimmed
face, a cautious face—dare I say—almost a smirking face;—the face
of a self-contained man who thinks allowably well of his parts, and is
determined to make the most of them. And in the brows over the
fine eyes there is a bulging out—where phrenologists place the
bump of language—that shows where his forte lies: No such word-
master had been heard to sing since the days when Chaucer sung.
He is deeply read in Chaucer too; and read in all—worth reading—
who came between. His lingual aptitudes are amazing. He can tear
words in tatters, and he can string them rhythmically in all shapes;
he makes his own law in language, as he grows heated in his work;
twists old phrases out of shape; makes new ones; binds them
together; tosses them as he will to the changing level of his thought:
so that whereas one may go to Chaucer, in points of language, as to
an authority—one goes to Spenser as to a mine of graceful and
euphonious phrases: but the authority is wanting—or, at least, is not
so safe. He makes uses for words which no analogy and no good
order can recognize. And his new words are not so much the
product of keen, shrewd search after what will fullest and strongest
express a feeling or a thought, or give color to epithet, as they are
the luxuriant outcropping of a tropical genius for language, which
delights in abundant forms, and makes them with an easy show of
its own fecundity, or for the chance purpose of filling a line, or
meting out the bounds of an orderly prosody.
He came up to London, as I said, about the year 1578, at the
invitation of a prig of a classmate, who makes him known to Philip
Sidney: Sidney is the very man to recognize and appreciate the
tender beauty of those woful plaints in the Shepherd’s Calendar, and
invites the poet down to Penshurst, that charming home of the
Sidneys, in Kent. There, such interest is made for him that he is
appointed to a secretaryship in Ireland, where the Queen’s
lieutenants are stamping out revolt. Spenser sees much of this fiery
work; and its blaze reddens some of the pages of the Faery Queen.
In the distribution of spoils, after the Irish revolt was put down, the
poet has bestowed upon him, amongst other plums, some three
thousand acres of wild land, with Kilcolman Castle, which stands
upon a valley spur of this domain. This castle is represented as an
uninteresting fortress—like Johnnie Armstrong’s tower in Scotland—
upon the borders of a small lake or mere, and the landscape—
stretching in unlovely waste around it—savage and low and tame.
Yet he finds rich rural pictures there—this idealist and dreamer: let
him see only so much of sky as comes between the roofs of a city
alley, and he will pluck out of it a multitude of twinkling stars; let
him look upon a rood square of brown grass-land, and he will set it
alight with scores of daisies and of primroses.