Idiomatic Python code is often referred to as being Pythonic.
If you know the length of a list or tuple, you can assign names to its elements with unpacking:
for index, item in enumerate(some_list):
# do something with index and item
You can use this to swap variables, as well:
a, b = b, a
If you need to assign something (for instance, in unpacking-ref
) but will not need that variable, use _
:
filename = 'foobar.txt'
basename, _, ext = filename.rpartition()
Note
"_
" is commonly used as an alias for the ~gettext.gettext
function. If your application uses (or may someday use) gettext
, you may want to avoid using _
for ignored variables, as you may accidentally shadow ~gettext.gettext
.
Use the Python list *
operator:
four_nones = [None] * 4
Because lists are mutable, the *
operator (as above) will create a list of N references to the same list, which is not likely what you want. Instead, use a list comprehension:
four_lists = [[] for _ in xrange(4)]
Also known as PEP 20, the guiding principles for Python's design.
>>> 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.
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!
For some examples of good Python style, see this Stack Overflow question or these slides from a Python user group.
PEP 8 is the de-facto code style guide for Python.
Conforming your Python code to PEP 8 is generally a good idea and helps make code more consistent when working on projects with other developers. There exists a command-line program, pep8, that can check your code for conformance. Install it by running the following command in your Terminal:
$ pip install pep8
Then run it on a file or series of files to get a report of any violations.
$ pep8 optparse.py
optparse.py:69:11: E401 multiple imports on one line
optparse.py:77:1: E302 expected 2 blank lines, found 1
optparse.py:88:5: E301 expected 1 blank line, found 0
optparse.py:222:34: W602 deprecated form of raising exception
optparse.py:347:31: E211 whitespace before '('
optparse.py:357:17: E201 whitespace after '{'
optparse.py:472:29: E221 multiple spaces before operator
optparse.py:544:21: W601 .has_key() is deprecated, use 'in'
Here are some conventions you should follow to make your code easier to read.
You don't need to explicitly compare a value to True, or None, or 0 - you can just add it to the if statement.
Bad:
if attr == True:
print 'True!'
if attr == None:
print 'attr is None!'
Good:
# Just check the value
if attr:
print 'True!'
# or check for the opposite
if not attr:
print 'attr is None!'
Don't use the has_key
function. Instead use x in d
syntax, or pass a default argument to get
.
Bad:
d = {'hello': 'world'}
if d.has_key('hello'):
print d['hello'] # prints 'world'
else:
print 'default_value'
Good:
d = {'hello': 'world'}
print d.get('hello', 'default_value') # prints 'world'
print d.get('thingy', 'default_value') # prints 'default_value'
# Or:
if 'hello' in d:
print d['hello']
List comprehensions provide a powerful, concise way to work with lists. Also, the map and filter functions can perform operations on lists using a different concise syntax.
Bad:
# Filter elements less than 5
a = [3, 4, 5]
b = []
for i in a:
if a > 4:
b.append(a)
Good:
b = [i for i in a if i > 4]
b = filter(lambda x: x > 4, a)
Bad:
# Add three to all list members.
a = [3, 4, 5]
count = 0
for i in a:
a[count] = i + 3
count = count + 1
Good:
a = [3, 4, 5]
a = [i + 3 for i in a]
# Or:
a = map(lambda i: i + 3, a)
Use enumerate to keep a count of your place in the list.
for i, item in enumerate(a):
print i + ", " + item
# prints
# 0, 3
# 1, 4
# 2, 5
Use the with open
syntax to read from files. This will automatically close files for you.
Bad:
f = open('file.txt')
a = f.read()
print a
f.close()
Good:
with open('file.txt') as f:
for line in f:
print line
Python supports returning multiple values from a function as a comma-separated list, so you don't have to create an object or dictionary and pack multiple values in before you return
Bad:
def math_func(a):
return {'square': a ** 2, 'cube': a ** 3}
d = math_func(3)
s = d['square']
c = d['cube']
Good:
def math_func(a):
return a ** 2, a ** 3
square, cube = math_func(3)