Writing Idiomatic Python 3 PDF
Writing Idiomatic Python 3 PDF
Je Knupp
2013
ii
Preface
Theres a famous old quote about writing maintainable software:
Always code as if the guy who ends up maintaining your code
will be a violent psychopath who knows where you live.
--John Woods comp.lang.c++
While Im not usually one for aphorisms, this one strikes a chord with me.
Maybe its because Ive spent my professional career writing software at huge
companies, but I have yet to inherit code that didnt eventually cause me to
curse the original author at some point. Everyone (besides you, of course, dear
reader) struggles to write code thats easy to maintain. When Python became
popular, many thought that, because of its terseness, it would naturally lead to
more maintainable software.
Alas, maintainability is not an emergent property of using an expressive language. Badly written Python code is just as unmaintainable as badly written
C++, Perl, Java and all the rest of the languages known for their, ahem, readability. Terse code is not a free lunch.
So what do we do? Resign ourselves to maintaining code we cant understand?
Rant on Twitter and The Daily WTF about the awful code we have to work on?
What must we do to stop the pain?
Write. Idiomatic. Code.
Its that simple. Idioms in a programming language are a sort of lingua franca
to let future readers know exactly what were trying to accomplish. We may
document our code extensively, write exhaustive unit tests, and hold code reviews
three times a day, but the fact remains: when someone else needs to make changes,
the code is king. If that someone is you, all the documentation in the world wont
help you understand unreadable code. After all, how can you even be sure the
code is doing what the documentation says?
Were usually reading someone elses code because theres a problem. But
idiomatic code helps here, too. Even if its wrong, when code is written idiomatiii
iv
PREFACE
ically, its far easier spot bugs. Idiomatic code reduces the cognitive load on the
reader. After learning a languages idioms, youll spend less time wondering Wait,
why are they using a named tuple there and more time understanding what the
code actually does.
After you learn and internalize a languages idioms, reading the code of a likeminded developer feels like speed reading. Youre no longer stopping at every
line, trying to gure out what it does while struggling to keep in mind what came
before. Instead, youll nd yourself almost skimming the code, thinking things like
OK, open a le, transform the contents to a sorted list, generate the giant report
in a thread-safe way. When you have that level of insight into code someone else
wrote, theres no bug you cant x and no enhancement you cant make.
All of this sounds great, right? Theres only one catch: you have to know and
use a languages idioms to benet. Enter Writing Idiomatic Python. What started
as a hasty blog post of idioms (fueled largely by my frustration while xing the
code of experienced developers new to Python) is now a full-edged eBook.
I hope you nd the book useful. It is meant to be a living document, updated
in near-real time with corrections, clarications, and additions. If you nd an
error in the text or have diculty deciphering a passage, please feel free to email
me at [email protected]. With their permission, Ill be adding the names of all
who contribute bug xes and clarications to the appendix.
Cheers,
Je Knupp
January, 2013
Change List
Version 1.1, February 2, 2013
New idiom: Use sys.exit in your script to return proper error codes
idiom
Greatly expanded discussion in Avoid comparing directly to True, False, or
None and added mention of comparison to None when checking if optional
arguments were set (to match the idiom Avoid using , [], and {} as default
parameters to functions.
Expanded Use the * operator to represent therest of a list idiom expanded with additional cases
Fixed page numbering issue causing numbers in table of contents and index
not to match the text
Fixed various typos and grammatical errors
Changed font size and various layout issues (some of which caused text to
run o the page
Changed preface text
vi
CONTENTS
Contents
Preface
Change List
Version 1.1, February 2, 2013 . . . . . . . . . . . . . . . . . . . . . . . .
Version 1.2, February 17, 2013 . . . . . . . . . . . . . . . . . . . . . . .
Contents
1 Control Structures and Functions
1.1 If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.1 Avoid comparing directly to True, False, or None . . . . .
1.1.2 Avoid repeating variable name in compound if statement .
1.1.3 Avoid placing conditional branch code on the same line as
the colon . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 For loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2.1 Use the enumerate function in loops instead of creating an
index variable . . . . . . . . . . . . . . . . . . . . . . . .
1.2.2 Use the in keyword to iterate over an iterable . . . . . .
1.2.3 Use else to execute code after a for loop concludes . . . .
1.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3.1 Avoid using '', [], and {} as default parameters to functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3.2 Use *args and **kwargs to accept arbitrary arguments . .
2 Working with Data
2.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1.1 Use a list comprehension to create a transformed version
of an existing list . . . . . . . . . . . . . . . . . . . . . . . .
2.1.2 Use the * operator to represent the rest of a list . . . .
iii
v
v
v
vi
1
1
1
4
5
6
6
7
8
9
9
11
15
15
15
16
CONTENTS
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2.1 Use the default parameter of dict.get to provide default
values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2.2 Use a dict comprehension to build a dict clearly and
eciently . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3.1 Prefer the format function for formatting strings . . . . . .
2.3.2 Use ''.join when creating a single string for list elements
2.3.3 Chain string functions to make a simple series of transformations more clear . . . . . . . . . . . . . . . . . . . . . . .
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4.1 Use underscores in function and variable names to help mark
private data . . . . . . . . . . . . . . . . . . . . . . . . .
2.4.2 Dene __str__ in a class to show a human-readable representation . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.5.1 Use sets to eliminate duplicate entries from Iterable containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.5.2 Use a set comprehension to generate sets concisely . . . .
2.5.3 Understand and use the mathematical set operations . . .
Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.6.1 Use a generator to lazily load innite sequences . . . . . .
2.6.2 Prefer a generator expression to a list comprehension
for simple iteration . . . . . . . . . . . . . . . . . . . . . . .
Context Managers . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.7.1 Use a context manager to ensure resources are properly
managed . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.8.1 Use tuples to unpack data . . . . . . . . . . . . . . . . . .
2.8.2 Use _ as a placeholder for data in a tuple that should be
ignored . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.9.1 Avoid using a temporary variable when performing a swap
of two values . . . . . . . . . . . . . . . . . . . . . . . . . .
vii
17
17
18
19
19
20
21
22
22
25
26
26
28
29
31
31
33
34
34
35
35
36
37
37
39
39
39
viii
CONTENTS
3.2
3.3
3.4
Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2.1 Use all capital letters when declaring global constant values
3.2.2 Avoid placing multiple statements on a single line . . . . . .
3.2.3 Format your code according to PEP8 . . . . . . . . . . . . .
Executable Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.1 Use sys.exit in your script to return proper error codes .
3.3.2 Use the if __name__ == '__main__' pattern to allow a
le to be both imported and run directly . . . . . . . . . .
Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.1 Prefer absolute imports to relative imports . . . . . .
3.4.2 Do not use from foo import * to import the contents of a
module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.3 Arrange your import statements in a standard order . . . .
41
41
42
43
44
44
46
47
47
48
49
4 General Advice
4.1 Avoid Reinventing the Wheel . . . . . . . . . . . . . . . . . . . . .
4.1.1 Learn the Contents of the Python Standard Library . . . .
4.1.2 Get to know PyPI (the Python Package Index) . . . . . . .
4.2 Modules of Note . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2.1 Learn the contents of the itertools module . . . . . . . . . .
4.2.2 Use functions in the os.path module when working with
directory paths . . . . . . . . . . . . . . . . . . . . . . . . .
51
51
51
52
53
53
5 Contributors
55
54
Chapter 1
None
False
Everything else is considered True (and thus most things are implicitly True).
The last condition for determining False, by checking the value returned by
__len__ or __nonzero__, allows you to dene how truthiness should work for
any class you create.
if statements in Python make use of truthiness implicitly, and you should
too. Instead of checking if a variable foo is True like this
if foo == True:
Harmful
def number_of_evil_robots_attacking():
return 10
def should_raise_shields():
# "We only raise Shields when one or more giant robots attack,
# so I can just return that value..."
return number_of_evil_robots_attacking()
if should_raise_shields() == True:
raise_shields()
print('Shields raised')
1.1. IF STATEMENTS
else:
print('Safe! No giant robots attacking')
1.1.1.2 Idiomatic
def number_of_evil_robots_attacking():
return 10
def should_raise_shields():
# "We only raise Shields when one or more giant robots attack,
# so I can just return that value..."
return number_of_evil_robots_attacking()
if should_raise_shields():
raise_shields()
print('Shields raised')
else:
print('Safe! No giant robots attacking')
1.1.2
When one wants to check a variable against a number of values, repeatedly listing
the variable being checked is unnecessarily verbose. Using an iterable makes
the code more clear and improves readability.
1.1.2.1
Harmful
is_generic_name = False
name = 'Tom'
if name == 'Tom' or name == 'Dick' or name == 'Harry':
is_generic_name = True
1.1.2.2
Idiomatic
name = 'Tom'
is_generic_name = name in ('Tom', 'Dick', 'Harry')
1.1. IF STATEMENTS
1.1.3.2 Idiomatic
name = 'Jeff'
address = 'New York, NY'
if name:
print(name)
print(address)
1.2
For loops
1.2.1
Programmers coming from other languages are used to explicitly declaring a variable to track the index of a container in a loop. For example, in C++:
for (int i=0; i < container.size(); ++i)
{
// Do stuff
}
Harmful
1.2.1.2
Idiomatic
1.2.2.2 Idiomatic
my_list = ['Larry', 'Moe', 'Curly']
for element in my_list:
print (element)
1.2.3
One of the lesser known facts about Pythons for loop is that it can include an
else clause. The else clause is executed after the iterator is exhausted, unless
the loop was ended prematurely due to a break statement. This allows you to
check for a condition in a for loop, break if the condition holds for an element,
else take some action if the condition did not hold for any of the elements being
looped over. This obviates the need for conditional ags in a loop solely used to
determine if some condition held.
In the scenario below, we are running a report to check if any of the email addresses our users registered are malformed (users can register multiple addresses).
The idiomatic version is more concise thanks to not having to deal with the
has_malformed_email_address ag. Whats more, even if another programmer wasnt familiar with the for ... else idiom, our code is clear enough to
teach them.
1.2.3.1
Harmful
1.2.3.2
Idiomatic
1.3. FUNCTIONS
1.3 Functions
1.3.1 Avoid using '', [], and {} as default parameters to
functions
Though this is explicitly mentioned in the Python tutorial, it nevertheless surprises
even experienced developers. In short: prefer names=None to names=[] for default
parameters to functions. Below is the Python Tutorials treatment of the issue.
1.3.1.1 Harmful
# The default value [of a function] is evaluated only once.
# This makes a difference when the default is a mutable object
# such as a list, dictionary, or instances of most classes. For
# example, the following function accumulates the arguments
# passed to it on subsequent calls.
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
# This will print
#
# [1]
# [1, 2]
# [1, 2, 3]
1.3.1.2 Idiomatic
# If you
# calls,
def f(a,
if L
10
# [1]
# [2]
# [3]
1.3. FUNCTIONS
11
12
def make_api_call_v4(
foo, bar, baz, qux, foo_polarity, baz_coefficient):
return make_api_call_v3(
foo, bar, baz, qux, foo_polarity) * baz_coefficient
def make_api_call_v5(
foo, bar, baz, qux, foo_polarity,
baz_coefficient, quux_capacitor):
# I don't need 'foo', 'bar', or 'baz' anymore, but I have to
# keep supporting them...
return baz_coefficient * quux_capacitor
def make_api_call_v6(
foo, bar, baz, qux, foo_polarity, baz_coefficient,
quux_capacitor, bar_has_hopped):
if bar_has_hopped:
baz_coefficient *= -1
return make_api_call_v5(foo, bar, baz, qux,
foo_polarity, baz_coefficient,
quux_capacitor)
def make_api_call_v7(
foo, bar, baz, qux, foo_polarity, baz_coefficient,
quux_capacitor, bar_has_hopped, true):
return true
def make_api_call_v8(
foo, bar, baz, qux, foo_polarity, baz_coefficient,
quux_capacitor, bar_has_hopped, true, false):
return false
def make_api_call_v9(
foo, bar, baz, qux, foo_polarity, baz_coefficient,
quux_capacitor, bar_has_hopped,
true, false, file_not_found):
return file_not_found
1.3.2.2
Idiomatic
1.3. FUNCTIONS
13
Chapter 2
a list from existing data. This is especially true when elements are both checked
for some condition and transformed in some way.
There are also (usually) performance benets to using a list comprehension
(or alternately, a generator expression) due to optimizations in the cPython
interpreter.
2.1.1.1 Harmful
some_other_list = range(10)
some_list = list()
for element in some_other_list:
if is_prime(element):
some_list.append(element + 5)
2.1.1.2 Idiomatic
some_other_list = range(10)
some_list = [element + 5
for element in some_other_list
if is_prime(element)]
15
16
2.1.2
Often times, especially when dealing with the arguments to functions, its useful
to extract a few elements at the beginning (or end) of a list while keeping the
rest for use later. Python 2 has no easy way to accomplish this aside from using
slices as shown below. Python 3 allows you to use the * operator on the left
hand side of an assignment to represent the rest of a sequence.
2.1.2.1
Harmful
2.1.2.2
Idiomatic
2.2. DICTIONARIES
17
2.2 Dictionaries
2.2.1 Use the default parameter of dict.get to provide default
values
Often overlooked in the denition of dict.get is the default parameter. Without using default (or the collections.defaultdict class), your code will be
littered with confusing if statements. Remember, strive for clarity.
2.2.1.1 Harmful
log_severity = None
if 'severity' in configuration:
log_severity = configuration['severity']
else:
log_severity = 'Info'
2.2.1.2 Idiomatic
log_severity = configuration.get('severity', 'Info')
18
2.2.2
Harmful
user_email = {}
for user in users_list:
if user.email:
user_email[user.name] = user.email
2.2.2.2
Idiomatic
2.3. STRINGS
19
2.3 Strings
2.3.1 Prefer the format function for formatting strings
There are three general ways of formatting strings (that is, creating a string that
is a mix of hard-coded strings and string variables). Easily the worst approach
is to use the + operator to concatenate a mix of static strings and variables. Using
old-style string formatting is slightly better. It makes use of a format string and
the % operator to ll in values, much like printf does in other languages.
The clearest and most idiomatic way to format strings is to use the format
function. Like old-style formatting, it takes a format string and replaces placeholders with values. The similarities end there, though. With the format function,
we can use named placeholders, access their attributes, and control padding and
string width, among a number of other things. The format function makes string
formatting clean and concise.
2.3.1.1 Harmful
def get_formatted_user_info_worst(user):
# Tedious to type and prone to conversion errors
return 'Name: ' + user.name + ', Age: ' + \
str(user.age) + ', Sex: ' + user.sex
def get_formatted_user_info_slightly_better(user):
# No visible connection between the format string placeholders
# and values to use. Also, why do I have to know the type?
# Don't these types all have __str__ functions?
return 'Name: %s, Age: %i, Sex: %c' % (
user.name, user.age, user.sex)
2.3.1.2 Idiomatic
def get_formatted_user_info(user):
# Clear and concise. At a glance I can tell exactly what
# the output should be. Note: this string could be returned
# directly, but the string itself is too long to fit on the
# page.
output = 'Name: {user.name}, Age: {user.age}'
', Sex: {user.sex}'.format(user=user)
return output
20
2.3.2
Its faster, uses less memory, and youll see it everywhere anyway. Note that the
two quotes represent the delimiter between list elements in the string were
creating. '' just means we wish to concatenate the elements with no characters
between them.
2.3.2.1
Harmful
2.3.2.2
Idiomatic
2.3. STRINGS
21
2.3.3.2 Idiomatic
book_info = ' The Three Musketeers: Alexandre Dumas'
formatted_book_info = book_info.strip().upper().replace(':', ' by')
22
2.4
Classes
2.4.1
Harmful
class Foo():
def __init__(self):
self.id = 8
self.value = self.get_value()
2.4. CLASSES
def get_value(self):
pass
def should_destroy_earth(self):
return self.id == 42
class Baz(Foo):
def get_value(self, some_new_parameter):
"""Since 'get_value' is called from the base class's
__init__ method and the base class definition doesn't
take a parameter, trying to create a Baz instance will
fail.
"""
pass
class Qux(Foo):
"""We aren't aware of Foo's internals, and we innocently
create an instance attribute named 'id' and set it to 42.
This overwrites Foo's id attribute and we inadvertently
blow up the earth.
"""
def __init__(self):
super(Qux, self).__init__()
self.id = 42
# No relation to Foo's id, purely coincidental
q = Qux()
b = Baz() # Raises 'TypeError'
q.should_destroy_earth() # returns True
q.id == 42 # returns True
2.4.1.2 Idiomatic
class Foo():
def __init__(self):
"""Since 'id' is of vital importance to us, we don't
want a derived class accidentally overwriting it. We'll
prepend with double underscores to introduce name
mangling.
"""
self.__id = 8
self.value = self.__get_value() # Our 'private copy'
def get_value(self):
pass
23
24
def should_destroy_earth(self):
return self.__id == 42
# Here, we're storing an 'private copy' of get_value,
# and assigning it to '__get_value'. Even if a derived
# class overrides get_value is a way incompatible with
# ours, we're fine
__get_value = get_value
class Baz(Foo):
def get_value(self, some_new_parameter):
pass
class Qux(Foo):
def __init__(self):
"""Now when we set 'id' to 42, it's not the same 'id'
that 'should_destroy_earth' is concerned with. In fact,
if you inspect a Qux object, you'll find it doesn't
have an __id attribute. So we can't mistakenly change
Foo's __id attribute even if we wanted to.
"""
self.id = 42
# No relation to Foo's id, purely coincidental
super(Qux, self).__init__()
q = Qux()
b = Baz() # Works fine now
q.should_destroy_earth() # returns False
q.id == 42 # returns True
with pytest.raises(AttributeError):
getattr(q, '__id')
2.4. CLASSES
25
2.4.2.2 Idiomatic
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return '{0}, {1}'.format(self.x, self.y)
p = Point(1, 2)
print (p)
# Prints '1, 2'
26
2.5
Sets
2.5.1
Its quite common to have a list or dict with duplicate values. In a list of
all surnames of employees at a large company, were bound to encounter common
surnames more than once in the list. If we need a list of all the unique surnames,
we can use a set to do the work for us. Three aspects of sets make them the
perfect answer to our problem:
1. A set contains only unique elements
2. Adding an already existing element to a set is essentially ignored
3. A set can be built from any Iterable whose elements are hashable
Continuing the example, we may have an existing display function that accepts a sequence and displays its elements in one of many formats. After creating
a set from our original list, will we need to change our display function?
Nope. Assuming our display function is implemented reasonably, our set
can be used as a drop-in replacement for a list. This works thanks to the fact
that a set, like a list, is an Iterable and can thus be used in a for loop, list
comprehension, etc.
2.5.1.1
Harmful
unique_surnames = []
for surname in employee_surnames:
if surname not in unique_surnames:
unique_surnames.append(surname)
def display(elements, output_format='html'):
if output_format == 'std_out':
for element in elements:
print(element)
elif output_format == 'html':
as_html = '<ul>'
for element in elements:
as_html += '<li>{}</li>'.format(element)
return as_html + '</ul>'
else:
raise RuntimeError('Unknown format {}'.format(output_format))
2.5. SETS
27
2.5.1.2 Idiomatic
unique_surnames = set(employee_surnames)
def display(elements, output_format='html'):
if output_format == 'std_out':
for element in elements:
print(element)
elif output_format == 'html':
as_html = '<ul>'
for element in elements:
as_html += '<li>{}</li>'.format(element)
return as_html + '</ul>'
else:
raise RuntimeError('Unknown format {}'.format(output_format))
28
2.5.2
The set comprehension syntax is relatively new in Python and, therefore, often
overlooked. Just as a list can be generated using a list comprehension, a
set can be generated using a set comprehension. In fact, the syntax is nearly
identical (modulo the enclosing characters).
2.5.2.1
Harmful
users_first_names = set()
for user in users:
users_first_names.add(user.first_name)
2.5.2.2
Idiomatic
2.5. SETS
29
For programmers who havent seen a Set data type before, it may appear to
be of limited use. Key to understanding their usefulness is understanding their
origin in mathematics. Set Theory is the branch of mathematics devoted to the
study of sets. Understanding the basic mathematical set operations is the key to
harnessing their power.
Dont worry; you dont need a degree in math to understand or use sets. You
just need to remember a few simple operations:
Union The set of elements in A, B, or both A and B (written A | B in Python).
Intersection The set of elements in both A and B (written A & B in Python).
Dierence The set of elements in A but not B (written A - B in Python).
*Note: order matters here. A - B is not necessarily the same as B - A.
Symmetric Dierence The set of elements in either A or B, but not both A and
B (written A B in Python).
When working with lists of data, a common task is nding the elements that
appear in all of the lists. Any time you need to choose elements from two or more
sequences based on properties of sequence membership, look to use a set.
Below, well explore some typical examples.
2.5.3.1 Harmful
def get_both_popular_and_active_users():
# Assume the following two functions each return a
# list of user names
most_popular_users = get_list_of_most_popular_users()
most_active_users = get_list_of_most_active_users()
popular_and_active_users = []
for user in most_active_users:
if user in most_popular_users:
popular_and_active_users.append(user)
return popular_and_active_users
30
2.5.3.2
Idiomatic
def get_both_popular_and_active_users():
# Assume the following two functions each return a
# list of user names
return(set(
get_list_of_most_active_users()) & set(
get_list_of_most_popular_users()))
2.6. GENERATORS
31
2.6 Generators
2.6.1 Use a generator to lazily load innite sequences
Often, its useful to provide a way to iterate over a sequence thats essentially
innite. Other times, you need to provide an interface to a sequence thats
incredibly expensive to calculate, and you dont want your user sitting on their
hands waiting for you to nish building a list.
In both cases, generators are your friend. A generator is a special type of
coroutine which returns an iterable. The state of the generator is saved, so
that the next call into the generator continues where it left o. In the examples
below, well see how to use a generator to help in each of the cases mentioned
above.
2.6.1.1 Harmful
def get_twitter_stream_for_keyword(keyword):
"""Get's the 'live stream', but only at the moment
the function is initially called. To get more entries,
the client code needs to keep calling
'get_twitter_livestream_for_user'. Not ideal.
"""
imaginary_twitter_api = ImaginaryTwitterAPI()
if imaginary_twitter_api.can_get_stream_data(keyword):
return imaginary_twitter_api.get_stream(keyword)
current_stream = get_twitter_stream_for_keyword('#jeffknupp')
for tweet in current_stream:
process_tweet(tweet)
# Uh, I want to keep showing tweets until the program is quit.
# What do I do now? Just keep calling
# get_twitter_stream_for_keyword? That seems stupid.
def get_list_of_incredibly_complex_calculation_results(data):
return [first_incredibly_long_calculation(data),
second_incredibly_long_calculation(data),
third_incredibly_long_calculation(data),
]
2.6.1.2 Idiomatic
def get_twitter_stream_for_keyword(keyword):
"""Now, 'get_twitter_stream_for_keyword' is a generator
32
2.6. GENERATORS
33
2.6.2.2 Idiomatic
for uppercase_name in (name.upper() for name in get_all_usernames()):
process_normalized_username(uppercase_name)
34
2.7
Context Managers
2.7.1
Similar to the RAII principle in languages like C++ and D, context managers
(objects meant to be used with the with statement) can make resource management both safer and more explicit. The canonical example is le IO.
Take a look at the Harmful code below. What happens if raise_exception
does, in fact, raise an exception? Since we havent caught it in the code below, it
will propagate up the stack. Weve hit an exit point in our code that might have
been overlooked, and we now have no way to close the opened le.
There are a number of classes in the standard library that support or use
a context manager. In addition, user dened classes can be easily made to
work with a context manager by dening __enter__ and __exit__ methods.
Functions may be wrapped with context managers through the contextlib
module.
2.7.1.1
Harmful
2.7.1.2
Idiomatic
2.8. TUPLES
35
2.8 Tuples
2.8.1 Use tuples to unpack data
In Python, it is possible to unpack data for multiple assignment. Those familiar
with LISP may know this as desctructuring bind.
2.8.1.1 Harmful
list_from_comma_separated_value_file = ['dog', 'Fido', 10]
animal = list_from_comma_separated_value_file[0]
name = list_from_comma_separated_value_file[1]
age = list_from_comma_separated_value_file[2]
output = ('{name} the {animal} is {age} years old'.format(
animal=animal, name=name, age=age))
2.8.1.2 Idiomatic
list_from_comma_separated_value_file = ['dog', 'Fido', 10]
(animal, name, age) = list_from_comma_separated_value_file
output = ('{name} the {animal} is {age} years old'.format(
animal=animal, name=name, age=age))
36
2.8.2
When setting a tuple equal to some ordered data, oftentimes not all of the data
is actually needed. Instead of creating throwaway variables with confusing names,
use the _ as a placeholder to tell the reader, This data will be discarded.
2.8.2.1
Harmful
2.8.2.2
Idiomatic
2.9. VARIABLES
37
2.9 Variables
2.9.1 Avoid using a temporary variable when performing a swap
of two values
There is no reason to swap using a temporary variable in Python. We can use
tuples to make our intention more clear.
2.9.1.1 Harmful
foo = 'Foo'
bar = 'Bar'
temp = foo
foo = bar
bar = temp
2.9.1.2 Idiomatic
foo = 'Foo'
bar = 'Bar'
(foo, bar) = (bar, foo)
Chapter 3
40
other than the sense that they intuitively belong under controller.
If all of those modules became classes, interoperability immediately becomes
an issue. We must carefully and precisely determine the methods we will expose
publicly, how state will be updated, and the way in which our class supports
testing. And instead of a dict or list, we have Processing and Persistence
objects we must write code to support.
Note that nothing in the description of Chirp necessitates the use of any
classes. Simple import statements make code sharing and encapsulation easy.
Passing state explicitly as arguments to functions keeps everything loosely coupled.
And it becomes far easier to receive, process, and transform data owing through
our system.
To be sure, classes may be a cleaner or more natural way to represent some
things. In many instances, Object Oriented Programming is a handy paradigm.
Just dont make it the only paradigm you use.
3.2. FORMATTING
41
3.2 Formatting
3.2.1 Use all capital letters when declaring global constant
values
To distinguish constants dened at the module level (or global in a single script)
from imported names, use all uppercase letters.
3.2.1.1 Harmful
seconds_in_a_day = 60 * 60 * 24
# ...
def display_uptime(uptime_in_seconds):
percentage_run_time = (
uptime_in_seconds/seconds_in_a_day) * 100
# "Huh!? Where did seconds_in_a_day come from?"
return 'The process was up {percent} percent of the day'.format(
percent=int(percentage_run_time))
# ...
uptime_in_seconds = 60 * 60 * 24
display_uptime(uptime_in_seconds)
3.2.1.2 Idiomatic
SECONDS_IN_A_DAY = 60 * 60 * 24
# ...
def display_uptime(uptime_in_seconds):
percentage_run_time = (
uptime_in_seconds/SECONDS_IN_A_DAY) * 100
# "Clearly SECONDS_IN_A_DAY is a constant defined
# elsewhere in this module."
return 'The process was up {percent} percent of the day'.format(
percent=int(percentage_run_time))
# ...
uptime_in_seconds = 60 * 60 * 24
display_uptime(uptime_in_seconds)
42
3.2.2
Though the language denition allows one to use ; to delineate statements, doing
so without reason makes ones code harder to read. When multiple statements
occur on the same line as an if, else, or elif, the situation is even further
confused.
3.2.2.1
Harmful
3.2.2.2
Idiomatic
if this_is_bad_code:
rewrite_code()
make_it_more_readable()
3.2. FORMATTING
43
Format
Example
Class
Camel case
class StringManipulator():
Variable
Words joined by _
joined_by_underscore = True
Function
Words joined by _
def multi_word_name(words):
Constant
All uppercase
SECRET_KEY = 42
Basically everything not listed should follow the variable/function naming conventions of Words joined by an underscore.
44
3.3
Executable Scripts
3.3.1
Python scripts should be good shell citizens. Its tempting to jam a bunch of code
after the if __name__ == '__main__' statement and not return anything. Avoid
this temptation.
Create a main function that contains the code to be run as a script. Use
sys.exit in main to return error codes if something goes wrong or zero if everything runs to completion. The only code under the if __name__ == '__main__'
statement should call sys.exit with the return value of your main function as
the parameter.
By doing this, we allow the script to be used in Unix pipelines, to be monitored
for failure without needing custom rules, and to be called by other programs safely.
3.3.1.1
Harmful
if __name__ == '__main__':
import sys
# What happens if no argument is passed on the
# command line?
if len(sys.argv) > 1:
argument = sys.argv[1]
result = do_stuff(argument)
# Again, what if this is False? How would other
# programs know?
if result:
do_stuff_with_result(result)
3.3.1.2
Idiomatic
def main():
import sys
if len(sys.argv) < 2:
# Calling sys.exit with a string automatically
# prints the string to stderr and exits with
# a value of '1' (error)
sys.exit('You forgot to pass an argument')
argument = sys.argv[1]
result = do_stuff(argument)
if not result:
sys.exit(1)
# We can also exit with just the return code
do_stuff_with_result(result)
# Optional, since the return value without this return
# statment would default to None, which sys.exit treats
# as 'exit with 0'
return 0
# The three lines below are the canonical script entry
# point lines. You'll see them often in other Python scripts
if __name__ == '__main__':
sys.exit(main())
45
46
3.3.2
Unlike the main() function available in some languages, Python has no built-in
notion of a main entry point. Rather, the interpreter immediately begins executing
statements upon loading a Python source le. If you want a le to function both
as an importable Python module and a stand-alone script, use the if __name__
== '__main__' idiom.
3.3.2.1
Harmful
import sys
import os
FIRST_NUMBER = float(sys.argv[1])
SECOND_NUMBER = float(sys.argv[2])
def divide(a, b):
return a/b
# I can't import this file (for the super
# useful 'divide' function) without the following
# code being executed.
if SECOND_NUMBER != 0:
print(divide(FIRST_NUMBER, SECOND_NUMBER))
3.3.2.2
Idiomatic
import sys
import os
def divide(a, b):
return a/b
# Will only run if script is executed directly,
# not when the file is imported as a module
if __name__ == '__main__':
first_number = float(sys.argv[1])
second_number = float(sys.argv[2])
if second_number != 0:
print(divide(first_number, second_number))
3.4. IMPORTS
47
3.4 Imports
3.4.1 Prefer absolute imports to relative imports
When importing a module, you have two choices of the import style to use:
absolute imports or relative imports. absolute imports specify a modules location (like <package>.<module>.<submodule>) from a location which is
reachable from sys.path.
Relative imports specify a module relative to the current modules location on
the le system. If you are the module package.sub_package.module and need
to import package.other_module, you can do so using the dotted relative
import syntax: from ..other_module import foo. A single . represents the
current package a module is contained in (like in a le system). Each additional .
is taken to mean the parent package of, one level per dot. Note that relative
imports must use the from ... import ... style. import foo is always treated
as an absolute import.
Alternatively, using an absolute import you would write
import package.other_module (possibly with an as clause to alias the module to a shorter name.
Why, then, should you prefer absolute imports to relative? Relative imports
clutter a modules namespace. By writing from foo import bar, youve bound
the name bar in your modules namespace. To those reading your code, it will
not be clear where bar came from, especially if used in a complicated function or
large module. foo.bar, however, makes it perfectly clear where bar is dened.
The Python programming FAQ goes so far as to say, Never use relative package
imports.
3.4.1.1 Harmful
# My location is package.sub_package.module
# and I want to import package.other_module.
# The following should be avoided:
from ...package import other_module
3.4.1.2 Idiomatic
# My location is package.sub_package.another_sub_package.module
# and I want to import package.other_module.
# Either of the following are acceptable:
import package.other_module
import package.other_module as other
48
3.4.2
Considering the previous idiom, this one should be obvious. Using an asterisk in
an import (as in from foo import *) is an easy way to clutter your namespace.
This may even cause issues if there are clashes between names you dene and
those dened in the package.
But what if you have to import a number of names from the foo package?
Simple. Make use of the fact that parenthesis can be used to group import statements. You wont have to write 10 lines of import statements from the same
module, and your namespace remains (relatively) clean.
Better yet, simply use absolute imports. If the package/module name is too
long, use an as clause to shorten it.
3.4.2.1
Harmful
3.4.2.2
Idiomatic
3.4. IMPORTS
49
50
3.4.3.2
Idiomatic
Chapter 4
General Advice
4.1 Avoid Reinventing the Wheel
4.1.1 Learn the Contents of the Python Standard Library
Part of writing idiomatic code is making liberal use of the standard library. Code
that unknowingly reimplements functionality in the standard library is perhaps
the clearest signal of a novice Python programmer. Python is commonly said to
come with batteries included for a good reason. The standard library contains
packages covering a wide range of domains.
Making use of the standard library has two primary benets. Most obviously,
you save yourself a good deal of time when you dont have to implement a piece
of functionality from scratch. Just as important is the fact that those who read
or maintain your code will have a much easier time doing so if you use packages
familiar to them.
Remember, the purpose of learning and writing idiomatic Python is to write
clear, maintainable, and bug-free code. Nothing ensures those qualities in your
code more easily than reusing code written and maintained by core Python developers. As bugs are found and xed in the standard library, your code improves
with each Python release without you lifting a nger.
51
52
4.1.2
53
54
4.2.2
When writing simple command-line scripts, new Python programmers often perform herculean feats of string manipulation to deal with le paths. Python has
an entire module dedicated to functions on path names: os.path. Using os.path
reduces the risk of common errors, makes your code portable, and makes your
code much easier to understand.
4.2.2.1
Harmful
4.2.2.2
Idiomatic
Chapter 5
Contributors
I actively solicit feedback on bugs, typos, grammatical and spelling errors, and
unclear portions of the text. The following awesome individuals have greatly
helped improve the quality of this text.
R. Daneel Olivaw
Jonathon Capps
Michael G. Lerner
Daniel Smith
Arne Sellmann
Nick Van Hoogenstyn
55
Index
.join, 20
* operator, 16
**kwargs, 11
*args, 11
+, 19
==, 2
_, 36
__enter__, 34
__exit__, 34
__len__, 1
__nonzero__, 1
__str__, 25
_eq, 2
default parameters, 9
desctructuring bind, 35
dict, 18, 26, 29, 39, 40
dict comprehension, 18
dict.get, 17
elif, 5, 42
else, 5, 8, 42
enumerate, 6
equality, 2
False, 1, 2
for, 8, 26, 29
format, 19
generator, 31, 33
generator expression, 15, 33
bar, 47
break, 8
class, 39
classes, 39
collections.defaultdict, 17
comprehension, 18
constants, 41
Container, 29
context manager, 34
context managers, 34
contextlib, 34
identity, 2
if, 1, 2, 4, 5, 42
if __name__ == __main__, 44,
46
import, 40, 47, 49
import *, 48
in, 7, 29
is, 2
Iterable, 26, 29
iterable, 4, 7, 31
itertools, 53
default, 17
keyword parameters, 11
57
58
INDEX