blob: 071eb2a1cc1d10607b99c65fee6cab86ea6ec93c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001************************************
Georg Brandl48310cd2009-01-03 21:18:54 +00002 Idioms and Anti-Idioms in Python
Georg Brandl116aa622007-08-15 14:28:22 +00003************************************
4
5:Author: Moshe Zadka
6
Christian Heimesdae2a892008-04-19 00:55:37 +00007This document is placed in the public domain.
Georg Brandl116aa622007-08-15 14:28:22 +00008
9
10.. topic:: Abstract
11
12 This document can be considered a companion to the tutorial. It shows how to use
13 Python, and even more importantly, how *not* to use Python.
14
15
16Language Constructs You Should Not Use
17======================================
18
19While Python has relatively few gotchas compared to other languages, it still
20has some constructs which are only useful in corner cases, or are plain
21dangerous.
22
23
24from module import \*
25---------------------
26
27
28Inside Function Definitions
29^^^^^^^^^^^^^^^^^^^^^^^^^^^
30
31``from module import *`` is *invalid* inside function definitions. While many
32versions of Python do not check for the invalidity, it does not make it more
Georg Brandlfe5f4092009-05-22 10:44:31 +000033valid, no more than having a smart lawyer makes a man innocent. Do not use it
Georg Brandl116aa622007-08-15 14:28:22 +000034like that ever. Even in versions where it was accepted, it made the function
35execution slower, because the compiler could not be certain which names are
36local and which are global. In Python 2.1 this construct causes warnings, and
37sometimes even errors.
38
39
40At Module Level
41^^^^^^^^^^^^^^^
42
43While it is valid to use ``from module import *`` at module level it is usually
44a bad idea. For one, this loses an important property Python otherwise has ---
45you can know where each toplevel name is defined by a simple "search" function
46in your favourite editor. You also open yourself to trouble in the future, if
47some module grows additional functions or classes.
48
49One of the most awful question asked on the newsgroup is why this code::
50
51 f = open("www")
52 f.read()
53
54does not work. Of course, it works just fine (assuming you have a file called
Georg Brandlc4a55fc2010-02-06 18:46:57 +000055"www".) But it does not work if somewhere in the module, the statement ``from
56os import *`` is present. The :mod:`os` module has a function called
57:func:`open` which returns an integer. While it is very useful, shadowing a
58builtin is one of its least useful properties.
Georg Brandl116aa622007-08-15 14:28:22 +000059
60Remember, you can never know for sure what names a module exports, so either
61take what you need --- ``from module import name1, name2``, or keep them in the
Georg Brandl6911e3c2007-09-04 07:15:32 +000062module and access on a per-need basis --- ``import module; print(module.name)``.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
65When It Is Just Fine
66^^^^^^^^^^^^^^^^^^^^
67
68There are situations in which ``from module import *`` is just fine:
69
70* The interactive prompt. For example, ``from math import *`` makes Python an
71 amazing scientific calculator.
72
73* When extending a module in C with a module in Python.
74
75* When the module advertises itself as ``from import *`` safe.
76
77
Georg Brandl116aa622007-08-15 14:28:22 +000078from module import name1, name2
79-------------------------------
80
Georg Brandlfe5f4092009-05-22 10:44:31 +000081This is a "don't" which is much weaker than the previous "don't"s but is still
Georg Brandl116aa622007-08-15 14:28:22 +000082something you should not do if you don't have good reasons to do that. The
83reason it is usually bad idea is because you suddenly have an object which lives
Christian Heimesc3f30c42008-02-22 16:37:40 +000084in two separate namespaces. When the binding in one namespace changes, the
Georg Brandl116aa622007-08-15 14:28:22 +000085binding in the other will not, so there will be a discrepancy between them. This
86happens when, for example, one module is reloaded, or changes the definition of
87a function at runtime.
88
89Bad example::
90
91 # foo.py
92 a = 1
93
94 # bar.py
95 from foo import a
96 if something():
Georg Brandl48310cd2009-01-03 21:18:54 +000097 a = 2 # danger: foo.a != a
Georg Brandl116aa622007-08-15 14:28:22 +000098
99Good example::
100
101 # foo.py
102 a = 1
103
104 # bar.py
105 import foo
106 if something():
107 foo.a = 2
108
109
110except:
111-------
112
113Python has the ``except:`` clause, which catches all exceptions. Since *every*
114error in Python raises an exception, this makes many programming errors look
115like runtime problems, and hinders the debugging process.
116
117The following code shows a great example::
118
119 try:
120 foo = opne("file") # misspelled "open"
121 except:
122 sys.exit("could not open file!")
123
124The second line triggers a :exc:`NameError` which is caught by the except
125clause. The program will exit, and you will have no idea that this has nothing
126to do with the readability of ``"file"``.
127
128The example above is better written ::
129
130 try:
131 foo = opne("file") # will be changed to "open" as soon as we run it
132 except IOError:
133 sys.exit("could not open file")
134
135There are some situations in which the ``except:`` clause is useful: for
136example, in a framework when running callbacks, it is good not to let any
137callback disturb the framework.
138
139
140Exceptions
141==========
142
143Exceptions are a useful feature of Python. You should learn to raise them
144whenever something unexpected occurs, and catch them only where you can do
145something about them.
146
147The following is a very popular anti-idiom ::
148
149 def get_status(file):
150 if not os.path.exists(file):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000151 print("file not found")
Georg Brandl116aa622007-08-15 14:28:22 +0000152 sys.exit(1)
153 return open(file).readline()
154
155Consider the case the file gets deleted between the time the call to
156:func:`os.path.exists` is made and the time :func:`open` is called. That means
157the last line will throw an :exc:`IOError`. The same would happen if *file*
158exists but has no read permission. Since testing this on a normal machine on
159existing and non-existing files make it seem bugless, that means in testing the
160results will seem fine, and the code will get shipped. Then an unhandled
161:exc:`IOError` escapes to the user, who has to watch the ugly traceback.
162
163Here is a better way to do it. ::
164
165 def get_status(file):
166 try:
167 return open(file).readline()
168 except (IOError, OSError):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000169 print("file not found")
Georg Brandl116aa622007-08-15 14:28:22 +0000170 sys.exit(1)
171
172In this version, \*either\* the file gets opened and the line is read (so it
173works even on flaky NFS or SMB connections), or the message is printed and the
174application aborted.
175
176Still, :func:`get_status` makes too many assumptions --- that it will only be
177used in a short running script, and not, say, in a long running server. Sure,
178the caller could do something like ::
179
180 try:
181 status = get_status(log)
182 except SystemExit:
183 status = None
184
185So, try to make as few ``except`` clauses in your code --- those will usually be
186a catch-all in the :func:`main`, or inside calls which should always succeed.
187
188So, the best version is probably ::
189
190 def get_status(file):
191 return open(file).readline()
192
193The caller can deal with the exception if it wants (for example, if it tries
194several files in a loop), or just let the exception filter upwards to *its*
195caller.
196
197The last version is not very good either --- due to implementation details, the
198file would not be closed when an exception is raised until the handler finishes,
199and perhaps not at all in non-C implementations (e.g., Jython). ::
200
201 def get_status(file):
Georg Brandl386bc6d2010-04-25 10:19:53 +0000202 with open(file) as fp:
Georg Brandl116aa622007-08-15 14:28:22 +0000203 return fp.readline()
Georg Brandl116aa622007-08-15 14:28:22 +0000204
205
206Using the Batteries
207===================
208
209Every so often, people seem to be writing stuff in the Python library again,
210usually poorly. While the occasional module has a poor interface, it is usually
211much better to use the rich standard library and data types that come with
Georg Brandlfe5f4092009-05-22 10:44:31 +0000212Python than inventing your own.
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214A useful module very few people know about is :mod:`os.path`. It always has the
215correct path arithmetic for your operating system, and will usually be much
Georg Brandlfe5f4092009-05-22 10:44:31 +0000216better than whatever you come up with yourself.
Georg Brandl116aa622007-08-15 14:28:22 +0000217
218Compare::
219
220 # ugh!
221 return dir+"/"+file
222 # better
223 return os.path.join(dir, file)
224
225More useful functions in :mod:`os.path`: :func:`basename`, :func:`dirname` and
226:func:`splitext`.
227
Georg Brandl22b34312009-07-26 14:54:51 +0000228There are also many useful built-in functions people seem not to be aware of for
Georg Brandl116aa622007-08-15 14:28:22 +0000229some reason: :func:`min` and :func:`max` can find the minimum/maximum of any
230sequence with comparable semantics, for example, yet many people write their own
Georg Brandl6911e3c2007-09-04 07:15:32 +0000231:func:`max`/:func:`min`. Another highly useful function is
232:func:`functools.reduce`. A classical use of :func:`reduce` is something like
233::
Georg Brandl116aa622007-08-15 14:28:22 +0000234
Georg Brandl6911e3c2007-09-04 07:15:32 +0000235 import sys, operator, functools
Benjamin Peterson09310422008-09-23 13:44:44 +0000236 nums = list(map(float, sys.argv[1:]))
Georg Brandl6911e3c2007-09-04 07:15:32 +0000237 print(functools.reduce(operator.add, nums) / len(nums))
Georg Brandl116aa622007-08-15 14:28:22 +0000238
239This cute little script prints the average of all numbers given on the command
240line. The :func:`reduce` adds up all the numbers, and the rest is just some
241pre- and postprocessing.
242
Georg Brandl5c106642007-11-29 17:41:05 +0000243On the same note, note that :func:`float` and :func:`int` accept arguments of
244type string, and so are suited to parsing --- assuming you are ready to deal
245with the :exc:`ValueError` they raise.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247
248Using Backslash to Continue Statements
249======================================
250
251Since Python treats a newline as a statement terminator, and since statements
Georg Brandlfe5f4092009-05-22 10:44:31 +0000252are often more than is comfortable to put in one line, many people do::
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254 if foo.bar()['first'][0] == baz.quux(1, 2)[5:9] and \
255 calculate_number(10, 20) != forbulate(500, 360):
256 pass
257
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000258You should realize that this is dangerous: a stray space after the ``\`` would
Georg Brandl116aa622007-08-15 14:28:22 +0000259make this line wrong, and stray spaces are notoriously hard to see in editors.
260In this case, at least it would be a syntax error, but if the code was::
261
262 value = foo.bar()['first'][0]*baz.quux(1, 2)[5:9] \
263 + calculate_number(10, 20)*forbulate(500, 360)
264
265then it would just be subtly wrong.
266
267It is usually much better to use the implicit continuation inside parenthesis:
268
269This version is bulletproof::
270
Georg Brandl48310cd2009-01-03 21:18:54 +0000271 value = (foo.bar()['first'][0]*baz.quux(1, 2)[5:9]
Georg Brandl116aa622007-08-15 14:28:22 +0000272 + calculate_number(10, 20)*forbulate(500, 360))
273