blob: 588066dbc5e275e3e30dd9e443c8e6e86ab41cfa [file] [log] [blame]
Georg Brandl6728c5a2009-10-11 18:31:23 +00001:tocdepth: 2
2
3===============
4Programming FAQ
5===============
6
Georg Brandl44ea77b2013-03-28 13:28:44 +01007.. only:: html
8
9 .. contents::
Georg Brandl6728c5a2009-10-11 18:31:23 +000010
11General Questions
12=================
13
14Is there a source code level debugger with breakpoints, single-stepping, etc.?
15------------------------------------------------------------------------------
16
17Yes.
18
19The pdb module is a simple but adequate console-mode debugger for Python. It is
20part of the standard Python library, and is :mod:`documented in the Library
21Reference Manual <pdb>`. You can also write your own debugger by using the code
22for pdb as an example.
23
24The IDLE interactive development environment, which is part of the standard
25Python distribution (normally available as Tools/scripts/idle), includes a
Georg Brandl66e624b2014-10-29 08:55:14 +010026graphical debugger.
Georg Brandl6728c5a2009-10-11 18:31:23 +000027
28PythonWin is a Python IDE that includes a GUI debugger based on pdb. The
29Pythonwin debugger colors breakpoints and has quite a few cool features such as
30debugging non-Pythonwin programs. Pythonwin is available as part of the `Python
31for Windows Extensions <http://sourceforge.net/projects/pywin32/>`__ project and
32as a part of the ActivePython distribution (see
Georg Brandlfa55a312014-10-29 09:24:54 +010033http://www.activestate.com/activepython\ ).
Georg Brandl6728c5a2009-10-11 18:31:23 +000034
35`Boa Constructor <http://boa-constructor.sourceforge.net/>`_ is an IDE and GUI
36builder that uses wxWidgets. It offers visual frame creation and manipulation,
37an object inspector, many views on the source like object browsers, inheritance
38hierarchies, doc string generated html documentation, an advanced debugger,
39integrated help, and Zope support.
40
Georg Brandlfa55a312014-10-29 09:24:54 +010041`Eric <http://eric-ide.python-projects.org/>`_ is an IDE built on PyQt
Georg Brandl6728c5a2009-10-11 18:31:23 +000042and the Scintilla editing component.
43
44Pydb is a version of the standard Python debugger pdb, modified for use with DDD
45(Data Display Debugger), a popular graphical debugger front end. Pydb can be
46found at http://bashdb.sourceforge.net/pydb/ and DDD can be found at
47http://www.gnu.org/software/ddd.
48
49There are a number of commercial Python IDEs that include graphical debuggers.
50They include:
51
52* Wing IDE (http://wingware.com/)
Georg Brandlfa55a312014-10-29 09:24:54 +010053* Komodo IDE (http://komodoide.com/)
Georg Brandl66e624b2014-10-29 08:55:14 +010054* PyCharm (https://www.jetbrains.com/pycharm/)
Georg Brandl6728c5a2009-10-11 18:31:23 +000055
56
57Is there a tool to help find bugs or perform static analysis?
58-------------------------------------------------------------
59
60Yes.
61
62PyChecker is a static analysis tool that finds bugs in Python source code and
63warns about code complexity and style. You can get PyChecker from
Georg Brandl0f5d6c02014-10-29 10:57:37 +010064http://pychecker.sourceforge.net/.
Georg Brandl6728c5a2009-10-11 18:31:23 +000065
66`Pylint <http://www.logilab.org/projects/pylint>`_ is another tool that checks
67if a module satisfies a coding standard, and also makes it possible to write
68plug-ins to add a custom feature. In addition to the bug checking that
69PyChecker performs, Pylint offers some additional features such as checking line
70length, whether variable names are well-formed according to your coding
71standard, whether declared interfaces are fully implemented, and more.
Georg Brandlfa55a312014-10-29 09:24:54 +010072http://docs.pylint.org/ provides a full list of Pylint's features.
Georg Brandl6728c5a2009-10-11 18:31:23 +000073
74
75How can I create a stand-alone binary from a Python script?
76-----------------------------------------------------------
77
78You don't need the ability to compile Python to C code if all you want is a
79stand-alone program that users can download and run without having to install
80the Python distribution first. There are a number of tools that determine the
81set of modules required by a program and bind these modules together with a
82Python binary to produce a single executable.
83
84One is to use the freeze tool, which is included in the Python source tree as
85``Tools/freeze``. It converts Python byte code to C arrays; a C compiler you can
86embed all your modules into a new program, which is then linked with the
87standard Python modules.
88
89It works by scanning your source recursively for import statements (in both
90forms) and looking for the modules in the standard Python path as well as in the
91source directory (for built-in modules). It then turns the bytecode for modules
92written in Python into C code (array initializers that can be turned into code
93objects using the marshal module) and creates a custom-made config file that
94only contains those built-in modules which are actually used in the program. It
95then compiles the generated C code and links it with the rest of the Python
96interpreter to form a self-contained binary which acts exactly like your script.
97
98Obviously, freeze requires a C compiler. There are several other utilities
99which don't. One is Thomas Heller's py2exe (Windows only) at
100
101 http://www.py2exe.org/
102
Georg Brandlfa55a312014-10-29 09:24:54 +0100103Another tool is Anthony Tuininga's `cx_Freeze <http://cx-freeze.sourceforge.net/>`_.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000104
105
106Are there coding standards or a style guide for Python programs?
107----------------------------------------------------------------
108
109Yes. The coding style required for standard library modules is documented as
110:pep:`8`.
111
112
113My program is too slow. How do I speed it up?
114---------------------------------------------
115
116That's a tough one, in general. There are many tricks to speed up Python code;
117consider rewriting parts in C as a last resort.
118
119In some cases it's possible to automatically translate Python to C or x86
120assembly language, meaning that you don't have to modify your code to gain
121increased speed.
122
123.. XXX seems to have overlap with other questions!
124
125`Pyrex <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_ can compile a
126slightly modified version of Python code into a C extension, and can be used on
127many different platforms.
128
129`Psyco <http://psyco.sourceforge.net>`_ is a just-in-time compiler that
130translates Python code into x86 assembly language. If you can use it, Psyco can
131provide dramatic speedups for critical functions.
132
133The rest of this answer will discuss various tricks for squeezing a bit more
134speed out of Python code. *Never* apply any optimization tricks unless you know
135you need them, after profiling has indicated that a particular function is the
136heavily executed hot spot in the code. Optimizations almost always make the
137code less clear, and you shouldn't pay the costs of reduced clarity (increased
138development time, greater likelihood of bugs) unless the resulting performance
139benefit is worth it.
140
141There is a page on the wiki devoted to `performance tips
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100142<https://wiki.python.org/moin/PythonSpeed/PerformanceTips>`_.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000143
144Guido van Rossum has written up an anecdote related to optimization at
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100145https://www.python.org/doc/essays/list2str.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000146
147One thing to notice is that function and (especially) method calls are rather
148expensive; if you have designed a purely OO interface with lots of tiny
149functions that don't do much more than get or set an instance variable or call
150another method, you might consider using a more direct way such as directly
151accessing instance variables. Also see the standard module :mod:`profile` which
152makes it possible to find out where your program is spending most of its time
153(if you have some patience -- the profiling itself can slow your program down by
154an order of magnitude).
155
156Remember that many standard optimization heuristics you may know from other
157programming experience may well apply to Python. For example it may be faster
158to send output to output devices using larger writes rather than smaller ones in
159order to reduce the overhead of kernel system calls. Thus CGI scripts that
160write all output in "one shot" may be faster than those that write lots of small
161pieces of output.
162
163Also, be sure to use Python's core features where appropriate. For example,
164slicing allows programs to chop up lists and other sequence objects in a single
165tick of the interpreter's mainloop using highly optimized C implementations.
166Thus to get the same effect as::
167
168 L2 = []
Georg Brandleacada82011-08-25 11:52:26 +0200169 for i in range(3):
Georg Brandl6728c5a2009-10-11 18:31:23 +0000170 L2.append(L1[i])
171
172it is much shorter and far faster to use ::
173
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000174 L2 = list(L1[:3]) # "list" is redundant if L1 is a list.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000175
Georg Brandl6f82cd32010-02-06 18:44:44 +0000176Note that the functionally-oriented built-in functions such as :func:`map`,
177:func:`zip`, and friends can be a convenient accelerator for loops that
178perform a single task. For example to pair the elements of two lists
179together::
Georg Brandl6728c5a2009-10-11 18:31:23 +0000180
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000181 >>> zip([1, 2, 3], [4, 5, 6])
Georg Brandl6728c5a2009-10-11 18:31:23 +0000182 [(1, 4), (2, 5), (3, 6)]
183
184or to compute a number of sines::
185
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000186 >>> map(math.sin, (1, 2, 3, 4))
187 [0.841470984808, 0.909297426826, 0.14112000806, -0.756802495308]
Georg Brandl6728c5a2009-10-11 18:31:23 +0000188
189The operation completes very quickly in such cases.
190
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000191Other examples include the ``join()`` and ``split()`` :ref:`methods
192of string objects <string-methods>`.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000193For example if s1..s7 are large (10K+) strings then
194``"".join([s1,s2,s3,s4,s5,s6,s7])`` may be far faster than the more obvious
195``s1+s2+s3+s4+s5+s6+s7``, since the "summation" will compute many
196subexpressions, whereas ``join()`` does all the copying in one pass. For
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000197manipulating strings, use the ``replace()`` and the ``format()`` :ref:`methods
198on string objects <string-methods>`. Use regular expressions only when you're
199not dealing with constant string patterns. You may still use :ref:`the old %
200operations <string-formatting>` ``string % tuple`` and ``string % dictionary``.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000201
Georg Brandl6f82cd32010-02-06 18:44:44 +0000202Be sure to use the :meth:`list.sort` built-in method to do sorting, and see the
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100203`sorting mini-HOWTO <https://wiki.python.org/moin/HowTo/Sorting>`_ for examples
Georg Brandl6728c5a2009-10-11 18:31:23 +0000204of moderately advanced usage. :meth:`list.sort` beats other techniques for
205sorting in all but the most extreme circumstances.
206
207Another common trick is to "push loops into functions or methods." For example
208suppose you have a program that runs slowly and you use the profiler to
209determine that a Python function ``ff()`` is being called lots of times. If you
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000210notice that ``ff()``::
Georg Brandl6728c5a2009-10-11 18:31:23 +0000211
212 def ff(x):
213 ... # do something with x computing result...
214 return result
215
216tends to be called in loops like::
217
218 list = map(ff, oldlist)
219
220or::
221
222 for x in sequence:
223 value = ff(x)
224 ... # do something with value...
225
226then you can often eliminate function call overhead by rewriting ``ff()`` to::
227
228 def ffseq(seq):
229 resultseq = []
230 for x in seq:
231 ... # do something with x computing result...
232 resultseq.append(result)
233 return resultseq
234
235and rewrite the two examples to ``list = ffseq(oldlist)`` and to::
236
237 for value in ffseq(sequence):
238 ... # do something with value...
239
240Single calls to ``ff(x)`` translate to ``ffseq([x])[0]`` with little penalty.
241Of course this technique is not always appropriate and there are other variants
242which you can figure out.
243
244You can gain some performance by explicitly storing the results of a function or
245method lookup into a local variable. A loop like::
246
247 for key in token:
248 dict[key] = dict.get(key, 0) + 1
249
250resolves ``dict.get`` every iteration. If the method isn't going to change, a
251slightly faster implementation is::
252
253 dict_get = dict.get # look up the method once
254 for key in token:
255 dict[key] = dict_get(key, 0) + 1
256
257Default arguments can be used to determine values once, at compile time instead
258of at run time. This can only be done for functions or objects which will not
259be changed during program execution, such as replacing ::
260
261 def degree_sin(deg):
262 return math.sin(deg * math.pi / 180.0)
263
264with ::
265
266 def degree_sin(deg, factor=math.pi/180.0, sin=math.sin):
267 return sin(deg * factor)
268
269Because this trick uses default arguments for terms which should not be changed,
270it should only be used when you are not concerned with presenting a possibly
271confusing API to your users.
272
273
274Core Language
275=============
276
R. David Murray89064382009-11-10 18:58:02 +0000277Why am I getting an UnboundLocalError when the variable has a value?
278--------------------------------------------------------------------
Georg Brandl6728c5a2009-10-11 18:31:23 +0000279
R. David Murray89064382009-11-10 18:58:02 +0000280It can be a surprise to get the UnboundLocalError in previously working
281code when it is modified by adding an assignment statement somewhere in
282the body of a function.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000283
R. David Murray89064382009-11-10 18:58:02 +0000284This code:
Georg Brandl6728c5a2009-10-11 18:31:23 +0000285
R. David Murray89064382009-11-10 18:58:02 +0000286 >>> x = 10
287 >>> def bar():
288 ... print x
289 >>> bar()
290 10
Georg Brandl6728c5a2009-10-11 18:31:23 +0000291
R. David Murray89064382009-11-10 18:58:02 +0000292works, but this code:
Georg Brandl6728c5a2009-10-11 18:31:23 +0000293
R. David Murray89064382009-11-10 18:58:02 +0000294 >>> x = 10
295 >>> def foo():
296 ... print x
297 ... x += 1
Georg Brandl6728c5a2009-10-11 18:31:23 +0000298
R. David Murray89064382009-11-10 18:58:02 +0000299results in an UnboundLocalError:
Georg Brandl6728c5a2009-10-11 18:31:23 +0000300
R. David Murray89064382009-11-10 18:58:02 +0000301 >>> foo()
302 Traceback (most recent call last):
303 ...
304 UnboundLocalError: local variable 'x' referenced before assignment
305
306This is because when you make an assignment to a variable in a scope, that
307variable becomes local to that scope and shadows any similarly named variable
308in the outer scope. Since the last statement in foo assigns a new value to
309``x``, the compiler recognizes it as a local variable. Consequently when the
310earlier ``print x`` attempts to print the uninitialized local variable and
311an error results.
312
313In the example above you can access the outer scope variable by declaring it
314global:
315
316 >>> x = 10
317 >>> def foobar():
318 ... global x
319 ... print x
320 ... x += 1
321 >>> foobar()
322 10
323
324This explicit declaration is required in order to remind you that (unlike the
325superficially analogous situation with class and instance variables) you are
326actually modifying the value of the variable in the outer scope:
327
328 >>> print x
329 11
330
Georg Brandl6728c5a2009-10-11 18:31:23 +0000331
332What are the rules for local and global variables in Python?
333------------------------------------------------------------
334
335In Python, variables that are only referenced inside a function are implicitly
336global. If a variable is assigned a new value anywhere within the function's
337body, it's assumed to be a local. If a variable is ever assigned a new value
338inside the function, the variable is implicitly local, and you need to
339explicitly declare it as 'global'.
340
341Though a bit surprising at first, a moment's consideration explains this. On
342one hand, requiring :keyword:`global` for assigned variables provides a bar
343against unintended side-effects. On the other hand, if ``global`` was required
344for all global references, you'd be using ``global`` all the time. You'd have
Georg Brandl6f82cd32010-02-06 18:44:44 +0000345to declare as global every reference to a built-in function or to a component of
Georg Brandl6728c5a2009-10-11 18:31:23 +0000346an imported module. This clutter would defeat the usefulness of the ``global``
347declaration for identifying side-effects.
348
349
Ezio Melotti58abc5b2013-01-05 00:49:48 +0200350Why do lambdas defined in a loop with different values all return the same result?
351----------------------------------------------------------------------------------
352
353Assume you use a for loop to define a few different lambdas (or even plain
354functions), e.g.::
355
R David Murrayff229842013-06-19 17:00:43 -0400356 >>> squares = []
357 >>> for x in range(5):
358 ... squares.append(lambda: x**2)
Ezio Melotti58abc5b2013-01-05 00:49:48 +0200359
360This gives you a list that contains 5 lambdas that calculate ``x**2``. You
361might expect that, when called, they would return, respectively, ``0``, ``1``,
362``4``, ``9``, and ``16``. However, when you actually try you will see that
363they all return ``16``::
364
365 >>> squares[2]()
366 16
367 >>> squares[4]()
368 16
369
370This happens because ``x`` is not local to the lambdas, but is defined in
371the outer scope, and it is accessed when the lambda is called --- not when it
372is defined. At the end of the loop, the value of ``x`` is ``4``, so all the
373functions now return ``4**2``, i.e. ``16``. You can also verify this by
374changing the value of ``x`` and see how the results of the lambdas change::
375
376 >>> x = 8
377 >>> squares[2]()
378 64
379
380In order to avoid this, you need to save the values in variables local to the
381lambdas, so that they don't rely on the value of the global ``x``::
382
R David Murrayff229842013-06-19 17:00:43 -0400383 >>> squares = []
384 >>> for x in range(5):
385 ... squares.append(lambda n=x: n**2)
Ezio Melotti58abc5b2013-01-05 00:49:48 +0200386
387Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed
388when the lambda is defined so that it has the same value that ``x`` had at
389that point in the loop. This means that the value of ``n`` will be ``0``
390in the first lambda, ``1`` in the second, ``2`` in the third, and so on.
391Therefore each lambda will now return the correct result::
392
393 >>> squares[2]()
394 4
395 >>> squares[4]()
396 16
397
398Note that this behaviour is not peculiar to lambdas, but applies to regular
399functions too.
400
401
Georg Brandl6728c5a2009-10-11 18:31:23 +0000402How do I share global variables across modules?
403------------------------------------------------
404
405The canonical way to share information across modules within a single program is
406to create a special module (often called config or cfg). Just import the config
407module in all modules of your application; the module then becomes available as
408a global name. Because there is only one instance of each module, any changes
409made to the module object get reflected everywhere. For example:
410
411config.py::
412
413 x = 0 # Default value of the 'x' configuration setting
414
415mod.py::
416
417 import config
418 config.x = 1
419
420main.py::
421
422 import config
423 import mod
424 print config.x
425
426Note that using a module is also the basis for implementing the Singleton design
427pattern, for the same reason.
428
429
430What are the "best practices" for using import in a module?
431-----------------------------------------------------------
432
433In general, don't use ``from modulename import *``. Doing so clutters the
Georg Brandl8b14dd32014-10-06 16:21:08 +0200434importer's namespace, and makes it much harder for linters to detect undefined
435names.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000436
437Import modules at the top of a file. Doing so makes it clear what other modules
438your code requires and avoids questions of whether the module name is in scope.
439Using one import per line makes it easy to add and delete module imports, but
440using multiple imports per line uses less screen space.
441
442It's good practice if you import modules in the following order:
443
Georg Brandl0cedb4b2009-12-20 14:20:16 +00004441. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re``
Georg Brandl6728c5a2009-10-11 18:31:23 +00004452. third-party library modules (anything installed in Python's site-packages
446 directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc.
4473. locally-developed modules
448
Georg Brandl8b14dd32014-10-06 16:21:08 +0200449Only use explicit relative package imports. If you're writing code that's in
450the ``package.sub.m1`` module and want to import ``package.sub.m2``, do not just
Georg Brandl6728c5a2009-10-11 18:31:23 +0000451write ``import m2``, even though it's legal. Write ``from package.sub import
Georg Brandl8b14dd32014-10-06 16:21:08 +0200452m2`` or ``from . import m2`` instead.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000453
454It is sometimes necessary to move imports to a function or class to avoid
455problems with circular imports. Gordon McMillan says:
456
457 Circular imports are fine where both modules use the "import <module>" form
458 of import. They fail when the 2nd module wants to grab a name out of the
459 first ("from module import name") and the import is at the top level. That's
460 because names in the 1st are not yet available, because the first module is
461 busy importing the 2nd.
462
463In this case, if the second module is only used in one function, then the import
464can easily be moved into that function. By the time the import is called, the
465first module will have finished initializing, and the second module can do its
466import.
467
468It may also be necessary to move imports out of the top level of code if some of
469the modules are platform-specific. In that case, it may not even be possible to
470import all of the modules at the top of the file. In this case, importing the
471correct modules in the corresponding platform-specific code is a good option.
472
473Only move imports into a local scope, such as inside a function definition, if
474it's necessary to solve a problem such as avoiding a circular import or are
475trying to reduce the initialization time of a module. This technique is
476especially helpful if many of the imports are unnecessary depending on how the
477program executes. You may also want to move imports into a function if the
478modules are only ever used in that function. Note that loading a module the
479first time may be expensive because of the one time initialization of the
480module, but loading a module multiple times is virtually free, costing only a
481couple of dictionary lookups. Even if the module name has gone out of scope,
482the module is probably available in :data:`sys.modules`.
483
Georg Brandl6728c5a2009-10-11 18:31:23 +0000484
Ezio Melotti4f7e09a2014-07-06 20:53:27 +0300485Why are default values shared between objects?
486----------------------------------------------
487
488This type of bug commonly bites neophyte programmers. Consider this function::
489
490 def foo(mydict={}): # Danger: shared reference to one dict for all calls
491 ... compute something ...
492 mydict[key] = value
493 return mydict
494
495The first time you call this function, ``mydict`` contains a single item. The
496second time, ``mydict`` contains two items because when ``foo()`` begins
497executing, ``mydict`` starts out with an item already in it.
498
499It is often expected that a function call creates new objects for default
500values. This is not what happens. Default values are created exactly once, when
501the function is defined. If that object is changed, like the dictionary in this
502example, subsequent calls to the function will refer to this changed object.
503
504By definition, immutable objects such as numbers, strings, tuples, and ``None``,
505are safe from change. Changes to mutable objects such as dictionaries, lists,
506and class instances can lead to confusion.
507
508Because of this feature, it is good programming practice to not use mutable
509objects as default values. Instead, use ``None`` as the default value and
510inside the function, check if the parameter is ``None`` and create a new
511list/dictionary/whatever if it is. For example, don't write::
512
513 def foo(mydict={}):
514 ...
515
516but::
517
518 def foo(mydict=None):
519 if mydict is None:
520 mydict = {} # create a new dict for local namespace
521
522This feature can be useful. When you have a function that's time-consuming to
523compute, a common technique is to cache the parameters and the resulting value
524of each call to the function, and return the cached value if the same value is
525requested again. This is called "memoizing", and can be implemented like this::
526
527 # Callers will never provide a third parameter for this function.
528 def expensive(arg1, arg2, _cache={}):
529 if (arg1, arg2) in _cache:
530 return _cache[(arg1, arg2)]
531
532 # Calculate the value
533 result = ... expensive computation ...
R David Murray276a0a52014-09-29 10:23:43 -0400534 _cache[(arg1, arg2)] = result # Store result in the cache
Ezio Melotti4f7e09a2014-07-06 20:53:27 +0300535 return result
536
537You could use a global variable containing a dictionary instead of the default
538value; it's a matter of taste.
539
540
Georg Brandl6728c5a2009-10-11 18:31:23 +0000541How can I pass optional or keyword parameters from one function to another?
542---------------------------------------------------------------------------
543
544Collect the arguments using the ``*`` and ``**`` specifiers in the function's
545parameter list; this gives you the positional arguments as a tuple and the
546keyword arguments as a dictionary. You can then pass these arguments when
547calling another function by using ``*`` and ``**``::
548
549 def f(x, *args, **kwargs):
550 ...
551 kwargs['width'] = '14.3c'
552 ...
553 g(x, *args, **kwargs)
554
555In the unlikely case that you care about Python versions older than 2.0, use
556:func:`apply`::
557
558 def f(x, *args, **kwargs):
559 ...
560 kwargs['width'] = '14.3c'
561 ...
562 apply(g, (x,)+args, kwargs)
563
564
Chris Jerdonekcf4710c2012-12-25 14:50:21 -0800565.. index::
566 single: argument; difference from parameter
567 single: parameter; difference from argument
568
Chris Jerdonek8da82682012-11-29 19:03:37 -0800569.. _faq-argument-vs-parameter:
570
571What is the difference between arguments and parameters?
572--------------------------------------------------------
573
574:term:`Parameters <parameter>` are defined by the names that appear in a
575function definition, whereas :term:`arguments <argument>` are the values
576actually passed to a function when calling it. Parameters define what types of
577arguments a function can accept. For example, given the function definition::
578
579 def func(foo, bar=None, **kwargs):
580 pass
581
582*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling
583``func``, for example::
584
585 func(42, bar=314, extra=somevar)
586
587the values ``42``, ``314``, and ``somevar`` are arguments.
588
589
R David Murray276a0a52014-09-29 10:23:43 -0400590Why did changing list 'y' also change list 'x'?
591------------------------------------------------
592
593If you wrote code like::
594
595 >>> x = []
596 >>> y = x
597 >>> y.append(10)
598 >>> y
599 [10]
600 >>> x
601 [10]
602
603you might be wondering why appending an element to ``y`` changed ``x`` too.
604
605There are two factors that produce this result:
606
6071) Variables are simply names that refer to objects. Doing ``y = x`` doesn't
608 create a copy of the list -- it creates a new variable ``y`` that refers to
609 the same object ``x`` refers to. This means that there is only one object
610 (the list), and both ``x`` and ``y`` refer to it.
6112) Lists are :term:`mutable`, which means that you can change their content.
612
613After the call to :meth:`~list.append`, the content of the mutable object has
614changed from ``[]`` to ``[10]``. Since both the variables refer to the same
615object, using either name accesses the modified value ``[10]``.
616
617If we instead assign an immutable object to ``x``::
618
619 >>> x = 5 # ints are immutable
620 >>> y = x
621 >>> x = x + 1 # 5 can't be mutated, we are creating a new object here
622 >>> x
623 6
624 >>> y
625 5
626
627we can see that in this case ``x`` and ``y`` are not equal anymore. This is
628because integers are :term:`immutable`, and when we do ``x = x + 1`` we are not
629mutating the int ``5`` by incrementing its value; instead, we are creating a
630new object (the int ``6``) and assigning it to ``x`` (that is, changing which
631object ``x`` refers to). After this assignment we have two objects (the ints
632``6`` and ``5``) and two variables that refer to them (``x`` now refers to
633``6`` but ``y`` still refers to ``5``).
634
635Some operations (for example ``y.append(10)`` and ``y.sort()``) mutate the
636object, whereas superficially similar operations (for example ``y = y + [10]``
637and ``sorted(y)``) create a new object. In general in Python (and in all cases
638in the standard library) a method that mutates an object will return ``None``
639to help avoid getting the two types of operations confused. So if you
640mistakenly write ``y.sort()`` thinking it will give you a sorted copy of ``y``,
641you'll instead end up with ``None``, which will likely cause your program to
642generate an easily diagnosed error.
643
644However, there is one class of operations where the same operation sometimes
645has different behaviors with different types: the augmented assignment
646operators. For example, ``+=`` mutates lists but not tuples or ints (``a_list
647+= [1, 2, 3]`` is equivalent to ``a_list.extend([1, 2, 3])`` and mutates
648``a_list``, whereas ``some_tuple += (1, 2, 3)`` and ``some_int += 1`` create
649new objects).
650
651In other words:
652
653* If we have a mutable object (:class:`list`, :class:`dict`, :class:`set`,
654 etc.), we can use some specific operations to mutate it and all the variables
655 that refer to it will see the change.
656* If we have an immutable object (:class:`str`, :class:`int`, :class:`tuple`,
657 etc.), all the variables that refer to it will always see the same value,
658 but operations that transform that value into a new value always return a new
659 object.
660
661If you want to know if two variables refer to the same object or not, you can
662use the :keyword:`is` operator, or the built-in function :func:`id`.
663
664
Georg Brandl6728c5a2009-10-11 18:31:23 +0000665How do I write a function with output parameters (call by reference)?
666---------------------------------------------------------------------
667
668Remember that arguments are passed by assignment in Python. Since assignment
669just creates references to objects, there's no alias between an argument name in
670the caller and callee, and so no call-by-reference per se. You can achieve the
671desired effect in a number of ways.
672
6731) By returning a tuple of the results::
674
675 def func2(a, b):
676 a = 'new-value' # a and b are local names
677 b = b + 1 # assigned to new objects
678 return a, b # return new values
679
680 x, y = 'old-value', 99
681 x, y = func2(x, y)
682 print x, y # output: new-value 100
683
684 This is almost always the clearest solution.
685
6862) By using global variables. This isn't thread-safe, and is not recommended.
687
6883) By passing a mutable (changeable in-place) object::
689
690 def func1(a):
691 a[0] = 'new-value' # 'a' references a mutable list
692 a[1] = a[1] + 1 # changes a shared object
693
694 args = ['old-value', 99]
695 func1(args)
696 print args[0], args[1] # output: new-value 100
697
6984) By passing in a dictionary that gets mutated::
699
700 def func3(args):
701 args['a'] = 'new-value' # args is a mutable dictionary
702 args['b'] = args['b'] + 1 # change it in-place
703
704 args = {'a':' old-value', 'b': 99}
705 func3(args)
706 print args['a'], args['b']
707
7085) Or bundle up values in a class instance::
709
710 class callByRef:
711 def __init__(self, **args):
712 for (key, value) in args.items():
713 setattr(self, key, value)
714
715 def func4(args):
716 args.a = 'new-value' # args is a mutable callByRef
717 args.b = args.b + 1 # change object in-place
718
719 args = callByRef(a='old-value', b=99)
720 func4(args)
721 print args.a, args.b
722
723
724 There's almost never a good reason to get this complicated.
725
726Your best choice is to return a tuple containing the multiple results.
727
728
729How do you make a higher order function in Python?
730--------------------------------------------------
731
732You have two choices: you can use nested scopes or you can use callable objects.
733For example, suppose you wanted to define ``linear(a,b)`` which returns a
734function ``f(x)`` that computes the value ``a*x+b``. Using nested scopes::
735
736 def linear(a, b):
737 def result(x):
738 return a * x + b
739 return result
740
741Or using a callable object::
742
743 class linear:
744
745 def __init__(self, a, b):
746 self.a, self.b = a, b
747
748 def __call__(self, x):
749 return self.a * x + self.b
750
751In both cases, ::
752
753 taxes = linear(0.3, 2)
754
755gives a callable object where ``taxes(10e6) == 0.3 * 10e6 + 2``.
756
757The callable object approach has the disadvantage that it is a bit slower and
758results in slightly longer code. However, note that a collection of callables
759can share their signature via inheritance::
760
761 class exponential(linear):
762 # __init__ inherited
763 def __call__(self, x):
764 return self.a * (x ** self.b)
765
766Object can encapsulate state for several methods::
767
768 class counter:
769
770 value = 0
771
772 def set(self, x):
773 self.value = x
774
775 def up(self):
776 self.value = self.value + 1
777
778 def down(self):
779 self.value = self.value - 1
780
781 count = counter()
782 inc, dec, reset = count.up, count.down, count.set
783
784Here ``inc()``, ``dec()`` and ``reset()`` act like functions which share the
785same counting variable.
786
787
788How do I copy an object in Python?
789----------------------------------
790
791In general, try :func:`copy.copy` or :func:`copy.deepcopy` for the general case.
792Not all objects can be copied, but most can.
793
794Some objects can be copied more easily. Dictionaries have a :meth:`~dict.copy`
795method::
796
797 newdict = olddict.copy()
798
799Sequences can be copied by slicing::
800
801 new_l = l[:]
802
803
804How can I find the methods or attributes of an object?
805------------------------------------------------------
806
807For an instance x of a user-defined class, ``dir(x)`` returns an alphabetized
808list of the names containing the instance attributes and methods and attributes
809defined by its class.
810
811
812How can my code discover the name of an object?
813-----------------------------------------------
814
815Generally speaking, it can't, because objects don't really have names.
816Essentially, assignment always binds a name to a value; The same is true of
817``def`` and ``class`` statements, but in that case the value is a
818callable. Consider the following code::
819
820 class A:
821 pass
822
823 B = A
824
825 a = B()
826 b = a
827 print b
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000828 <__main__.A instance at 0x16D07CC>
Georg Brandl6728c5a2009-10-11 18:31:23 +0000829 print a
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000830 <__main__.A instance at 0x16D07CC>
Georg Brandl6728c5a2009-10-11 18:31:23 +0000831
832Arguably the class has a name: even though it is bound to two names and invoked
833through the name B the created instance is still reported as an instance of
834class A. However, it is impossible to say whether the instance's name is a or
835b, since both names are bound to the same value.
836
837Generally speaking it should not be necessary for your code to "know the names"
838of particular values. Unless you are deliberately writing introspective
839programs, this is usually an indication that a change of approach might be
840beneficial.
841
842In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to
843this question:
844
845 The same way as you get the name of that cat you found on your porch: the cat
846 (object) itself cannot tell you its name, and it doesn't really care -- so
847 the only way to find out what it's called is to ask all your neighbours
848 (namespaces) if it's their cat (object)...
849
850 ....and don't be surprised if you'll find that it's known by many names, or
851 no name at all!
852
853
854What's up with the comma operator's precedence?
855-----------------------------------------------
856
857Comma is not an operator in Python. Consider this session::
858
859 >>> "a" in "b", "a"
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000860 (False, 'a')
Georg Brandl6728c5a2009-10-11 18:31:23 +0000861
862Since the comma is not an operator, but a separator between expressions the
863above is evaluated as if you had entered::
864
R David Murrayff229842013-06-19 17:00:43 -0400865 ("a" in "b"), "a"
Georg Brandl6728c5a2009-10-11 18:31:23 +0000866
867not::
868
R David Murrayff229842013-06-19 17:00:43 -0400869 "a" in ("b", "a")
Georg Brandl6728c5a2009-10-11 18:31:23 +0000870
871The same is true of the various assignment operators (``=``, ``+=`` etc). They
872are not truly operators but syntactic delimiters in assignment statements.
873
874
875Is there an equivalent of C's "?:" ternary operator?
876----------------------------------------------------
877
878Yes, this feature was added in Python 2.5. The syntax would be as follows::
879
880 [on_true] if [expression] else [on_false]
881
882 x, y = 50, 25
883
884 small = x if x < y else y
885
886For versions previous to 2.5 the answer would be 'No'.
887
Georg Brandl6728c5a2009-10-11 18:31:23 +0000888
889Is it possible to write obfuscated one-liners in Python?
890--------------------------------------------------------
891
892Yes. Usually this is done by nesting :keyword:`lambda` within
893:keyword:`lambda`. See the following three examples, due to Ulf Bartelt::
894
895 # Primes < 1000
896 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
897 map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
898
899 # First 10 Fibonacci numbers
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000900 print map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1: f(x,f),
Georg Brandl6728c5a2009-10-11 18:31:23 +0000901 range(10))
902
903 # Mandelbrot set
904 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
905 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
906 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
907 i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
908 >=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
909 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
910 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
911 # \___ ___/ \___ ___/ | | |__ lines on screen
912 # V V | |______ columns on screen
913 # | | |__________ maximum of "iterations"
914 # | |_________________ range on y axis
915 # |____________________________ range on x axis
916
917Don't try this at home, kids!
918
919
920Numbers and strings
921===================
922
923How do I specify hexadecimal and octal integers?
924------------------------------------------------
925
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000926To specify an octal digit, precede the octal value with a zero, and then a lower
927or uppercase "o". For example, to set the variable "a" to the octal value "10"
928(8 in decimal), type::
Georg Brandl6728c5a2009-10-11 18:31:23 +0000929
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000930 >>> a = 0o10
Georg Brandl6728c5a2009-10-11 18:31:23 +0000931 >>> a
932 8
933
934Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,
935and then a lower or uppercase "x". Hexadecimal digits can be specified in lower
936or uppercase. For example, in the Python interpreter::
937
938 >>> a = 0xa5
939 >>> a
940 165
941 >>> b = 0XB2
942 >>> b
943 178
944
945
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000946Why does -22 // 10 return -3?
947-----------------------------
Georg Brandl6728c5a2009-10-11 18:31:23 +0000948
949It's primarily driven by the desire that ``i % j`` have the same sign as ``j``.
950If you want that, and also want::
951
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000952 i == (i // j) * j + (i % j)
Georg Brandl6728c5a2009-10-11 18:31:23 +0000953
954then integer division has to return the floor. C also requires that identity to
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000955hold, and then compilers that truncate ``i // j`` need to make ``i % j`` have
956the same sign as ``i``.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000957
958There are few real use cases for ``i % j`` when ``j`` is negative. When ``j``
959is positive, there are many, and in virtually all of them it's more useful for
960``i % j`` to be ``>= 0``. If the clock says 10 now, what did it say 200 hours
961ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to
962bite.
963
Georg Brandl0cedb4b2009-12-20 14:20:16 +0000964.. note::
965
966 On Python 2, ``a / b`` returns the same as ``a // b`` if
967 ``__future__.division`` is not in effect. This is also known as "classic"
968 division.
969
Georg Brandl6728c5a2009-10-11 18:31:23 +0000970
971How do I convert a string to a number?
972--------------------------------------
973
974For integers, use the built-in :func:`int` type constructor, e.g. ``int('144')
975== 144``. Similarly, :func:`float` converts to floating-point,
976e.g. ``float('144') == 144.0``.
977
978By default, these interpret the number as decimal, so that ``int('0144') ==
979144`` and ``int('0x144')`` raises :exc:`ValueError`. ``int(string, base)`` takes
980the base to convert from as a second optional argument, so ``int('0x144', 16) ==
981324``. If the base is specified as 0, the number is interpreted using Python's
982rules: a leading '0' indicates octal, and '0x' indicates a hex number.
983
984Do not use the built-in function :func:`eval` if all you need is to convert
985strings to numbers. :func:`eval` will be significantly slower and it presents a
986security risk: someone could pass you a Python expression that might have
987unwanted side effects. For example, someone could pass
988``__import__('os').system("rm -rf $HOME")`` which would erase your home
989directory.
990
991:func:`eval` also has the effect of interpreting numbers as Python expressions,
992so that e.g. ``eval('09')`` gives a syntax error because Python regards numbers
993starting with '0' as octal (base 8).
994
995
996How do I convert a number to a string?
997--------------------------------------
998
999To convert, e.g., the number 144 to the string '144', use the built-in type
1000constructor :func:`str`. If you want a hexadecimal or octal representation, use
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001001the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see
1002the :ref:`formatstrings` section, e.g. ``"{:04d}".format(144)`` yields
1003``'0144'`` and ``"{:.3f}".format(1/3)`` yields ``'0.333'``. You may also use
1004:ref:`the % operator <string-formatting>` on strings. See the library reference
1005manual for details.
Georg Brandl6728c5a2009-10-11 18:31:23 +00001006
1007
1008How do I modify a string in place?
1009----------------------------------
1010
1011You can't, because strings are immutable. If you need an object with this
1012ability, try converting the string to a list or use the array module::
1013
R David Murrayff229842013-06-19 17:00:43 -04001014 >>> import io
Georg Brandl6728c5a2009-10-11 18:31:23 +00001015 >>> s = "Hello, world"
1016 >>> a = list(s)
1017 >>> print a
1018 ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
1019 >>> a[7:] = list("there!")
1020 >>> ''.join(a)
1021 'Hello, there!'
1022
1023 >>> import array
1024 >>> a = array.array('c', s)
1025 >>> print a
1026 array('c', 'Hello, world')
Serhiy Storchakab7128732013-12-24 11:04:06 +02001027 >>> a[0] = 'y'; print a
R David Murrayff229842013-06-19 17:00:43 -04001028 array('c', 'yello, world')
Georg Brandl6728c5a2009-10-11 18:31:23 +00001029 >>> a.tostring()
1030 'yello, world'
1031
1032
1033How do I use strings to call functions/methods?
1034-----------------------------------------------
1035
1036There are various techniques.
1037
1038* The best is to use a dictionary that maps strings to functions. The primary
1039 advantage of this technique is that the strings do not need to match the names
1040 of the functions. This is also the primary technique used to emulate a case
1041 construct::
1042
1043 def a():
1044 pass
1045
1046 def b():
1047 pass
1048
1049 dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
1050
1051 dispatch[get_input()]() # Note trailing parens to call function
1052
1053* Use the built-in function :func:`getattr`::
1054
1055 import foo
1056 getattr(foo, 'bar')()
1057
1058 Note that :func:`getattr` works on any object, including classes, class
1059 instances, modules, and so on.
1060
1061 This is used in several places in the standard library, like this::
1062
1063 class Foo:
1064 def do_foo(self):
1065 ...
1066
1067 def do_bar(self):
1068 ...
1069
1070 f = getattr(foo_instance, 'do_' + opname)
1071 f()
1072
1073
1074* Use :func:`locals` or :func:`eval` to resolve the function name::
1075
1076 def myFunc():
1077 print "hello"
1078
1079 fname = "myFunc"
1080
1081 f = locals()[fname]
1082 f()
1083
1084 f = eval(fname)
1085 f()
1086
1087 Note: Using :func:`eval` is slow and dangerous. If you don't have absolute
1088 control over the contents of the string, someone could pass a string that
1089 resulted in an arbitrary function being executed.
1090
1091Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
1092-------------------------------------------------------------------------------------
1093
1094Starting with Python 2.2, you can use ``S.rstrip("\r\n")`` to remove all
Georg Brandl09302282010-10-06 09:32:48 +00001095occurrences of any line terminator from the end of the string ``S`` without
Georg Brandl6728c5a2009-10-11 18:31:23 +00001096removing other trailing whitespace. If the string ``S`` represents more than
1097one line, with several empty lines at the end, the line terminators for all the
1098blank lines will be removed::
1099
1100 >>> lines = ("line 1 \r\n"
1101 ... "\r\n"
1102 ... "\r\n")
1103 >>> lines.rstrip("\n\r")
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001104 'line 1 '
Georg Brandl6728c5a2009-10-11 18:31:23 +00001105
1106Since this is typically only desired when reading text one line at a time, using
1107``S.rstrip()`` this way works well.
1108
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001109For older versions of Python, there are two partial substitutes:
Georg Brandl6728c5a2009-10-11 18:31:23 +00001110
1111- If you want to remove all trailing whitespace, use the ``rstrip()`` method of
1112 string objects. This removes all trailing whitespace, not just a single
1113 newline.
1114
1115- Otherwise, if there is only one line in the string ``S``, use
1116 ``S.splitlines()[0]``.
1117
1118
1119Is there a scanf() or sscanf() equivalent?
1120------------------------------------------
1121
1122Not as such.
1123
1124For simple input parsing, the easiest approach is usually to split the line into
1125whitespace-delimited words using the :meth:`~str.split` method of string objects
1126and then convert decimal strings to numeric values using :func:`int` or
1127:func:`float`. ``split()`` supports an optional "sep" parameter which is useful
1128if the line uses something other than whitespace as a separator.
1129
Brian Curtine49aefc2010-09-23 13:48:06 +00001130For more complicated input parsing, regular expressions are more powerful
Sandro Tosi98ed08f2012-01-14 16:42:02 +01001131than C's :c:func:`sscanf` and better suited for the task.
Georg Brandl6728c5a2009-10-11 18:31:23 +00001132
1133
1134What does 'UnicodeError: ASCII [decoding,encoding] error: ordinal not in range(128)' mean?
1135------------------------------------------------------------------------------------------
1136
1137This error indicates that your Python installation can handle only 7-bit ASCII
1138strings. There are a couple ways to fix or work around the problem.
1139
1140If your programs must handle data in arbitrary character set encodings, the
1141environment the application runs in will generally identify the encoding of the
1142data it is handing you. You need to convert the input to Unicode data using
1143that encoding. For example, a program that handles email or web input will
1144typically find character set encoding information in Content-Type headers. This
1145can then be used to properly convert input data to Unicode. Assuming the string
1146referred to by ``value`` is encoded as UTF-8::
1147
1148 value = unicode(value, "utf-8")
1149
1150will return a Unicode object. If the data is not correctly encoded as UTF-8,
1151the above call will raise a :exc:`UnicodeError` exception.
1152
1153If you only want strings converted to Unicode which have non-ASCII data, you can
1154try converting them first assuming an ASCII encoding, and then generate Unicode
1155objects if that fails::
1156
1157 try:
1158 x = unicode(value, "ascii")
1159 except UnicodeError:
1160 value = unicode(value, "utf-8")
1161 else:
1162 # value was valid ASCII data
1163 pass
1164
1165It's possible to set a default encoding in a file called ``sitecustomize.py``
1166that's part of the Python library. However, this isn't recommended because
1167changing the Python-wide default encoding may cause third-party extension
1168modules to fail.
1169
1170Note that on Windows, there is an encoding known as "mbcs", which uses an
1171encoding specific to your current locale. In many cases, and particularly when
1172working with COM, this may be an appropriate default encoding to use.
1173
1174
1175Sequences (Tuples/Lists)
1176========================
1177
1178How do I convert between tuples and lists?
1179------------------------------------------
1180
1181The type constructor ``tuple(seq)`` converts any sequence (actually, any
1182iterable) into a tuple with the same items in the same order.
1183
1184For example, ``tuple([1, 2, 3])`` yields ``(1, 2, 3)`` and ``tuple('abc')``
1185yields ``('a', 'b', 'c')``. If the argument is a tuple, it does not make a copy
1186but returns the same object, so it is cheap to call :func:`tuple` when you
1187aren't sure that an object is already a tuple.
1188
1189The type constructor ``list(seq)`` converts any sequence or iterable into a list
1190with the same items in the same order. For example, ``list((1, 2, 3))`` yields
1191``[1, 2, 3]`` and ``list('abc')`` yields ``['a', 'b', 'c']``. If the argument
1192is a list, it makes a copy just like ``seq[:]`` would.
1193
1194
1195What's a negative index?
1196------------------------
1197
1198Python sequences are indexed with positive numbers and negative numbers. For
1199positive numbers 0 is the first index 1 is the second index and so forth. For
1200negative indices -1 is the last index and -2 is the penultimate (next to last)
1201index and so forth. Think of ``seq[-n]`` as the same as ``seq[len(seq)-n]``.
1202
1203Using negative indices can be very convenient. For example ``S[:-1]`` is all of
1204the string except for its last character, which is useful for removing the
1205trailing newline from a string.
1206
1207
1208How do I iterate over a sequence in reverse order?
1209--------------------------------------------------
1210
Georg Brandl6f82cd32010-02-06 18:44:44 +00001211Use the :func:`reversed` built-in function, which is new in Python 2.4::
Georg Brandl6728c5a2009-10-11 18:31:23 +00001212
1213 for x in reversed(sequence):
1214 ... # do something with x...
1215
1216This won't touch your original sequence, but build a new copy with reversed
1217order to iterate over.
1218
1219With Python 2.3, you can use an extended slice syntax::
1220
1221 for x in sequence[::-1]:
1222 ... # do something with x...
1223
1224
1225How do you remove duplicates from a list?
1226-----------------------------------------
1227
1228See the Python Cookbook for a long discussion of many ways to do this:
1229
Georg Brandlfa55a312014-10-29 09:24:54 +01001230 http://code.activestate.com/recipes/52560/
Georg Brandl6728c5a2009-10-11 18:31:23 +00001231
1232If you don't mind reordering the list, sort it and then scan from the end of the
1233list, deleting duplicates as you go::
1234
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001235 if mylist:
1236 mylist.sort()
1237 last = mylist[-1]
1238 for i in range(len(mylist)-2, -1, -1):
1239 if last == mylist[i]:
1240 del mylist[i]
Georg Brandl6728c5a2009-10-11 18:31:23 +00001241 else:
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001242 last = mylist[i]
Georg Brandl6728c5a2009-10-11 18:31:23 +00001243
1244If all elements of the list may be used as dictionary keys (i.e. they are all
1245hashable) this is often faster ::
1246
1247 d = {}
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001248 for x in mylist:
1249 d[x] = 1
1250 mylist = list(d.keys())
Georg Brandl6728c5a2009-10-11 18:31:23 +00001251
1252In Python 2.5 and later, the following is possible instead::
1253
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001254 mylist = list(set(mylist))
Georg Brandl6728c5a2009-10-11 18:31:23 +00001255
1256This converts the list into a set, thereby removing duplicates, and then back
1257into a list.
1258
1259
1260How do you make an array in Python?
1261-----------------------------------
1262
1263Use a list::
1264
1265 ["this", 1, "is", "an", "array"]
1266
1267Lists are equivalent to C or Pascal arrays in their time complexity; the primary
1268difference is that a Python list can contain objects of many different types.
1269
1270The ``array`` module also provides methods for creating arrays of fixed types
1271with compact representations, but they are slower to index than lists. Also
1272note that the Numeric extensions and others define array-like structures with
1273various characteristics as well.
1274
1275To get Lisp-style linked lists, you can emulate cons cells using tuples::
1276
1277 lisp_list = ("like", ("this", ("example", None) ) )
1278
1279If mutability is desired, you could use lists instead of tuples. Here the
1280analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is
1281``lisp_list[1]``. Only do this if you're sure you really need to, because it's
1282usually a lot slower than using Python lists.
1283
1284
1285How do I create a multidimensional list?
1286----------------------------------------
1287
1288You probably tried to make a multidimensional array like this::
1289
R David Murrayff229842013-06-19 17:00:43 -04001290 >>> A = [[None] * 2] * 3
Georg Brandl6728c5a2009-10-11 18:31:23 +00001291
1292This looks correct if you print it::
1293
1294 >>> A
1295 [[None, None], [None, None], [None, None]]
1296
1297But when you assign a value, it shows up in multiple places:
1298
1299 >>> A[0][0] = 5
1300 >>> A
1301 [[5, None], [5, None], [5, None]]
1302
1303The reason is that replicating a list with ``*`` doesn't create copies, it only
1304creates references to the existing objects. The ``*3`` creates a list
1305containing 3 references to the same list of length two. Changes to one row will
1306show in all rows, which is almost certainly not what you want.
1307
1308The suggested approach is to create a list of the desired length first and then
1309fill in each element with a newly created list::
1310
1311 A = [None] * 3
1312 for i in range(3):
1313 A[i] = [None] * 2
1314
1315This generates a list containing 3 different lists of length two. You can also
1316use a list comprehension::
1317
1318 w, h = 2, 3
1319 A = [[None] * w for i in range(h)]
1320
1321Or, you can use an extension that provides a matrix datatype; `Numeric Python
Ezio Melottic49805e2013-06-09 01:04:21 +03001322<http://www.numpy.org/>`_ is the best known.
Georg Brandl6728c5a2009-10-11 18:31:23 +00001323
1324
1325How do I apply a method to a sequence of objects?
1326-------------------------------------------------
1327
1328Use a list comprehension::
1329
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001330 result = [obj.method() for obj in mylist]
Georg Brandl6728c5a2009-10-11 18:31:23 +00001331
1332More generically, you can try the following function::
1333
1334 def method_map(objects, method, arguments):
1335 """method_map([a,b], "meth", (1,2)) gives [a.meth(1,2), b.meth(1,2)]"""
1336 nobjects = len(objects)
1337 methods = map(getattr, objects, [method]*nobjects)
1338 return map(apply, methods, [arguments]*nobjects)
1339
1340
R David Murrayed983ab2013-05-20 10:34:58 -04001341Why does a_tuple[i] += ['item'] raise an exception when the addition works?
1342---------------------------------------------------------------------------
1343
1344This is because of a combination of the fact that augmented assignment
1345operators are *assignment* operators, and the difference between mutable and
1346immutable objects in Python.
1347
1348This discussion applies in general when augmented assignment operators are
1349applied to elements of a tuple that point to mutable objects, but we'll use
1350a ``list`` and ``+=`` as our exemplar.
1351
1352If you wrote::
1353
1354 >>> a_tuple = (1, 2)
1355 >>> a_tuple[0] += 1
1356 Traceback (most recent call last):
1357 ...
1358 TypeError: 'tuple' object does not support item assignment
1359
1360The reason for the exception should be immediately clear: ``1`` is added to the
1361object ``a_tuple[0]`` points to (``1``), producing the result object, ``2``,
1362but when we attempt to assign the result of the computation, ``2``, to element
1363``0`` of the tuple, we get an error because we can't change what an element of
1364a tuple points to.
1365
1366Under the covers, what this augmented assignment statement is doing is
1367approximately this::
1368
R David Murraye6f2e6c2013-05-21 11:46:18 -04001369 >>> result = a_tuple[0] + 1
R David Murrayed983ab2013-05-20 10:34:58 -04001370 >>> a_tuple[0] = result
1371 Traceback (most recent call last):
1372 ...
1373 TypeError: 'tuple' object does not support item assignment
1374
1375It is the assignment part of the operation that produces the error, since a
1376tuple is immutable.
1377
1378When you write something like::
1379
1380 >>> a_tuple = (['foo'], 'bar')
1381 >>> a_tuple[0] += ['item']
1382 Traceback (most recent call last):
1383 ...
1384 TypeError: 'tuple' object does not support item assignment
1385
1386The exception is a bit more surprising, and even more surprising is the fact
1387that even though there was an error, the append worked::
1388
1389 >>> a_tuple[0]
1390 ['foo', 'item']
1391
R David Murraye6f2e6c2013-05-21 11:46:18 -04001392To see why this happens, you need to know that (a) if an object implements an
1393``__iadd__`` magic method, it gets called when the ``+=`` augmented assignment
1394is executed, and its return value is what gets used in the assignment statement;
1395and (b) for lists, ``__iadd__`` is equivalent to calling ``extend`` on the list
1396and returning the list. That's why we say that for lists, ``+=`` is a
1397"shorthand" for ``list.extend``::
R David Murrayed983ab2013-05-20 10:34:58 -04001398
1399 >>> a_list = []
1400 >>> a_list += [1]
1401 >>> a_list
1402 [1]
1403
R David Murraye6f2e6c2013-05-21 11:46:18 -04001404This is equivalent to::
R David Murrayed983ab2013-05-20 10:34:58 -04001405
1406 >>> result = a_list.__iadd__([1])
1407 >>> a_list = result
1408
1409The object pointed to by a_list has been mutated, and the pointer to the
1410mutated object is assigned back to ``a_list``. The end result of the
1411assignment is a no-op, since it is a pointer to the same object that ``a_list``
1412was previously pointing to, but the assignment still happens.
1413
1414Thus, in our tuple example what is happening is equivalent to::
1415
1416 >>> result = a_tuple[0].__iadd__(['item'])
1417 >>> a_tuple[0] = result
1418 Traceback (most recent call last):
1419 ...
1420 TypeError: 'tuple' object does not support item assignment
1421
1422The ``__iadd__`` succeeds, and thus the list is extended, but even though
1423``result`` points to the same object that ``a_tuple[0]`` already points to,
1424that final assignment still results in an error, because tuples are immutable.
1425
1426
Georg Brandl6728c5a2009-10-11 18:31:23 +00001427Dictionaries
1428============
1429
1430How can I get a dictionary to display its keys in a consistent order?
1431---------------------------------------------------------------------
1432
1433You can't. Dictionaries store their keys in an unpredictable order, so the
1434display order of a dictionary's elements will be similarly unpredictable.
1435
1436This can be frustrating if you want to save a printable version to a file, make
1437some changes and then compare it with some other printed dictionary. In this
1438case, use the ``pprint`` module to pretty-print the dictionary; the items will
1439be presented in order sorted by the key.
1440
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001441A more complicated solution is to subclass ``dict`` to create a
Georg Brandl6728c5a2009-10-11 18:31:23 +00001442``SortedDict`` class that prints itself in a predictable order. Here's one
1443simpleminded implementation of such a class::
1444
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001445 class SortedDict(dict):
Georg Brandl6728c5a2009-10-11 18:31:23 +00001446 def __repr__(self):
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001447 keys = sorted(self.keys())
1448 result = ("{!r}: {!r}".format(k, self[k]) for k in keys)
1449 return "{{{}}}".format(", ".join(result))
Georg Brandl6728c5a2009-10-11 18:31:23 +00001450
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001451 __str__ = __repr__
Georg Brandl6728c5a2009-10-11 18:31:23 +00001452
1453This will work for many common situations you might encounter, though it's far
1454from a perfect solution. The largest flaw is that if some values in the
1455dictionary are also dictionaries, their values won't be presented in any
1456particular order.
1457
1458
1459I want to do a complicated sort: can you do a Schwartzian Transform in Python?
1460------------------------------------------------------------------------------
1461
1462The technique, attributed to Randal Schwartz of the Perl community, sorts the
1463elements of a list by a metric which maps each element to its "sort value". In
1464Python, just use the ``key`` argument for the ``sort()`` method::
1465
1466 Isorted = L[:]
1467 Isorted.sort(key=lambda s: int(s[10:15]))
1468
1469The ``key`` argument is new in Python 2.4, for older versions this kind of
1470sorting is quite simple to do with list comprehensions. To sort a list of
1471strings by their uppercase values::
1472
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001473 tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform
Georg Brandl6728c5a2009-10-11 18:31:23 +00001474 tmp1.sort()
1475 Usorted = [x[1] for x in tmp1]
1476
1477To sort by the integer value of a subfield extending from positions 10-15 in
1478each string::
1479
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001480 tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform
Georg Brandl6728c5a2009-10-11 18:31:23 +00001481 tmp2.sort()
1482 Isorted = [x[1] for x in tmp2]
1483
1484Note that Isorted may also be computed by ::
1485
1486 def intfield(s):
1487 return int(s[10:15])
1488
1489 def Icmp(s1, s2):
1490 return cmp(intfield(s1), intfield(s2))
1491
1492 Isorted = L[:]
1493 Isorted.sort(Icmp)
1494
1495but since this method calls ``intfield()`` many times for each element of L, it
1496is slower than the Schwartzian Transform.
1497
1498
1499How can I sort one list by values from another list?
1500----------------------------------------------------
1501
1502Merge them into a single list of tuples, sort the resulting list, and then pick
1503out the element you want. ::
1504
1505 >>> list1 = ["what", "I'm", "sorting", "by"]
1506 >>> list2 = ["something", "else", "to", "sort"]
1507 >>> pairs = zip(list1, list2)
1508 >>> pairs
1509 [('what', 'something'), ("I'm", 'else'), ('sorting', 'to'), ('by', 'sort')]
1510 >>> pairs.sort()
1511 >>> result = [ x[1] for x in pairs ]
1512 >>> result
1513 ['else', 'sort', 'to', 'something']
1514
1515An alternative for the last step is::
1516
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001517 >>> result = []
1518 >>> for p in pairs: result.append(p[1])
Georg Brandl6728c5a2009-10-11 18:31:23 +00001519
1520If you find this more legible, you might prefer to use this instead of the final
1521list comprehension. However, it is almost twice as slow for long lists. Why?
1522First, the ``append()`` operation has to reallocate memory, and while it uses
1523some tricks to avoid doing that each time, it still has to do it occasionally,
1524and that costs quite a bit. Second, the expression "result.append" requires an
1525extra attribute lookup, and third, there's a speed reduction from having to make
1526all those function calls.
1527
1528
1529Objects
1530=======
1531
1532What is a class?
1533----------------
1534
1535A class is the particular object type created by executing a class statement.
1536Class objects are used as templates to create instance objects, which embody
1537both the data (attributes) and code (methods) specific to a datatype.
1538
1539A class can be based on one or more other classes, called its base class(es). It
1540then inherits the attributes and methods of its base classes. This allows an
1541object model to be successively refined by inheritance. You might have a
1542generic ``Mailbox`` class that provides basic accessor methods for a mailbox,
1543and subclasses such as ``MboxMailbox``, ``MaildirMailbox``, ``OutlookMailbox``
1544that handle various specific mailbox formats.
1545
1546
1547What is a method?
1548-----------------
1549
1550A method is a function on some object ``x`` that you normally call as
1551``x.name(arguments...)``. Methods are defined as functions inside the class
1552definition::
1553
1554 class C:
1555 def meth (self, arg):
1556 return arg * 2 + self.attribute
1557
1558
1559What is self?
1560-------------
1561
1562Self is merely a conventional name for the first argument of a method. A method
1563defined as ``meth(self, a, b, c)`` should be called as ``x.meth(a, b, c)`` for
1564some instance ``x`` of the class in which the definition occurs; the called
1565method will think it is called as ``meth(x, a, b, c)``.
1566
1567See also :ref:`why-self`.
1568
1569
1570How do I check if an object is an instance of a given class or of a subclass of it?
1571-----------------------------------------------------------------------------------
1572
1573Use the built-in function ``isinstance(obj, cls)``. You can check if an object
1574is an instance of any of a number of classes by providing a tuple instead of a
1575single class, e.g. ``isinstance(obj, (class1, class2, ...))``, and can also
1576check whether an object is one of Python's built-in types, e.g.
1577``isinstance(obj, str)`` or ``isinstance(obj, (int, long, float, complex))``.
1578
1579Note that most programs do not use :func:`isinstance` on user-defined classes
1580very often. If you are developing the classes yourself, a more proper
1581object-oriented style is to define methods on the classes that encapsulate a
1582particular behaviour, instead of checking the object's class and doing a
1583different thing based on what class it is. For example, if you have a function
1584that does something::
1585
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001586 def search(obj):
Georg Brandl6728c5a2009-10-11 18:31:23 +00001587 if isinstance(obj, Mailbox):
1588 # ... code to search a mailbox
1589 elif isinstance(obj, Document):
1590 # ... code to search a document
1591 elif ...
1592
1593A better approach is to define a ``search()`` method on all the classes and just
1594call it::
1595
1596 class Mailbox:
1597 def search(self):
1598 # ... code to search a mailbox
1599
1600 class Document:
1601 def search(self):
1602 # ... code to search a document
1603
1604 obj.search()
1605
1606
1607What is delegation?
1608-------------------
1609
1610Delegation is an object oriented technique (also called a design pattern).
1611Let's say you have an object ``x`` and want to change the behaviour of just one
1612of its methods. You can create a new class that provides a new implementation
1613of the method you're interested in changing and delegates all other methods to
1614the corresponding method of ``x``.
1615
1616Python programmers can easily implement delegation. For example, the following
1617class implements a class that behaves like a file but converts all written data
1618to uppercase::
1619
1620 class UpperOut:
1621
1622 def __init__(self, outfile):
1623 self._outfile = outfile
1624
1625 def write(self, s):
1626 self._outfile.write(s.upper())
1627
1628 def __getattr__(self, name):
1629 return getattr(self._outfile, name)
1630
1631Here the ``UpperOut`` class redefines the ``write()`` method to convert the
1632argument string to uppercase before calling the underlying
1633``self.__outfile.write()`` method. All other methods are delegated to the
1634underlying ``self.__outfile`` object. The delegation is accomplished via the
1635``__getattr__`` method; consult :ref:`the language reference <attribute-access>`
1636for more information about controlling attribute access.
1637
1638Note that for more general cases delegation can get trickier. When attributes
1639must be set as well as retrieved, the class must define a :meth:`__setattr__`
1640method too, and it must do so carefully. The basic implementation of
1641:meth:`__setattr__` is roughly equivalent to the following::
1642
1643 class X:
1644 ...
1645 def __setattr__(self, name, value):
1646 self.__dict__[name] = value
1647 ...
1648
1649Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store
1650local state for self without causing an infinite recursion.
1651
1652
1653How do I call a method defined in a base class from a derived class that overrides it?
1654--------------------------------------------------------------------------------------
1655
1656If you're using new-style classes, use the built-in :func:`super` function::
1657
1658 class Derived(Base):
1659 def meth (self):
1660 super(Derived, self).meth()
1661
1662If you're using classic classes: For a class definition such as ``class
1663Derived(Base): ...`` you can call method ``meth()`` defined in ``Base`` (or one
1664of ``Base``'s base classes) as ``Base.meth(self, arguments...)``. Here,
1665``Base.meth`` is an unbound method, so you need to provide the ``self``
1666argument.
1667
1668
1669How can I organize my code to make it easier to change the base class?
1670----------------------------------------------------------------------
1671
1672You could define an alias for the base class, assign the real base class to it
1673before your class definition, and use the alias throughout your class. Then all
1674you have to change is the value assigned to the alias. Incidentally, this trick
1675is also handy if you want to decide dynamically (e.g. depending on availability
1676of resources) which base class to use. Example::
1677
1678 BaseAlias = <real base class>
1679
1680 class Derived(BaseAlias):
1681 def meth(self):
1682 BaseAlias.meth(self)
1683 ...
1684
1685
1686How do I create static class data and static class methods?
1687-----------------------------------------------------------
1688
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001689Both static data and static methods (in the sense of C++ or Java) are supported
1690in Python.
Georg Brandl6728c5a2009-10-11 18:31:23 +00001691
1692For static data, simply define a class attribute. To assign a new value to the
1693attribute, you have to explicitly use the class name in the assignment::
1694
1695 class C:
1696 count = 0 # number of times C.__init__ called
1697
1698 def __init__(self):
1699 C.count = C.count + 1
1700
1701 def getcount(self):
1702 return C.count # or return self.count
1703
1704``c.count`` also refers to ``C.count`` for any ``c`` such that ``isinstance(c,
1705C)`` holds, unless overridden by ``c`` itself or by some class on the base-class
1706search path from ``c.__class__`` back to ``C``.
1707
1708Caution: within a method of C, an assignment like ``self.count = 42`` creates a
Georg Brandl0cedb4b2009-12-20 14:20:16 +00001709new and unrelated instance named "count" in ``self``'s own dict. Rebinding of a
1710class-static data name must always specify the class whether inside a method or
1711not::
Georg Brandl6728c5a2009-10-11 18:31:23 +00001712
1713 C.count = 314
1714
1715Static methods are possible since Python 2.2::
1716
1717 class C:
1718 def static(arg1, arg2, arg3):
1719 # No 'self' parameter!
1720 ...
1721 static = staticmethod(static)
1722
1723With Python 2.4's decorators, this can also be written as ::
1724
1725 class C:
1726 @staticmethod
1727 def static(arg1, arg2, arg3):
1728 # No 'self' parameter!
1729 ...
1730
1731However, a far more straightforward way to get the effect of a static method is
1732via a simple module-level function::
1733
1734 def getcount():
1735 return C.count
1736
1737If your code is structured so as to define one class (or tightly related class
1738hierarchy) per module, this supplies the desired encapsulation.
1739
1740
1741How can I overload constructors (or methods) in Python?
1742-------------------------------------------------------
1743
1744This answer actually applies to all methods, but the question usually comes up
1745first in the context of constructors.
1746
1747In C++ you'd write
1748
1749.. code-block:: c
1750
1751 class C {
1752 C() { cout << "No arguments\n"; }
1753 C(int i) { cout << "Argument is " << i << "\n"; }
1754 }
1755
1756In Python you have to write a single constructor that catches all cases using
1757default arguments. For example::
1758
1759 class C:
1760 def __init__(self, i=None):
1761 if i is None:
1762 print "No arguments"
1763 else:
1764 print "Argument is", i
1765
1766This is not entirely equivalent, but close enough in practice.
1767
1768You could also try a variable-length argument list, e.g. ::
1769
1770 def __init__(self, *args):
1771 ...
1772
1773The same approach works for all method definitions.
1774
1775
1776I try to use __spam and I get an error about _SomeClassName__spam.
1777------------------------------------------------------------------
1778
1779Variable names with double leading underscores are "mangled" to provide a simple
1780but effective way to define class private variables. Any identifier of the form
1781``__spam`` (at least two leading underscores, at most one trailing underscore)
1782is textually replaced with ``_classname__spam``, where ``classname`` is the
1783current class name with any leading underscores stripped.
1784
1785This doesn't guarantee privacy: an outside user can still deliberately access
1786the "_classname__spam" attribute, and private values are visible in the object's
1787``__dict__``. Many Python programmers never bother to use private variable
1788names at all.
1789
1790
1791My class defines __del__ but it is not called when I delete the object.
1792-----------------------------------------------------------------------
1793
1794There are several possible reasons for this.
1795
1796The del statement does not necessarily call :meth:`__del__` -- it simply
1797decrements the object's reference count, and if this reaches zero
1798:meth:`__del__` is called.
1799
1800If your data structures contain circular links (e.g. a tree where each child has
1801a parent reference and each parent has a list of children) the reference counts
1802will never go back to zero. Once in a while Python runs an algorithm to detect
1803such cycles, but the garbage collector might run some time after the last
1804reference to your data structure vanishes, so your :meth:`__del__` method may be
1805called at an inconvenient and random time. This is inconvenient if you're trying
1806to reproduce a problem. Worse, the order in which object's :meth:`__del__`
1807methods are executed is arbitrary. You can run :func:`gc.collect` to force a
1808collection, but there *are* pathological cases where objects will never be
1809collected.
1810
1811Despite the cycle collector, it's still a good idea to define an explicit
1812``close()`` method on objects to be called whenever you're done with them. The
1813``close()`` method can then remove attributes that refer to subobjecs. Don't
1814call :meth:`__del__` directly -- :meth:`__del__` should call ``close()`` and
1815``close()`` should make sure that it can be called more than once for the same
1816object.
1817
1818Another way to avoid cyclical references is to use the :mod:`weakref` module,
1819which allows you to point to objects without incrementing their reference count.
1820Tree data structures, for instance, should use weak references for their parent
1821and sibling references (if they need them!).
1822
1823If the object has ever been a local variable in a function that caught an
1824expression in an except clause, chances are that a reference to the object still
1825exists in that function's stack frame as contained in the stack trace.
1826Normally, calling :func:`sys.exc_clear` will take care of this by clearing the
1827last recorded exception.
1828
1829Finally, if your :meth:`__del__` method raises an exception, a warning message
1830is printed to :data:`sys.stderr`.
1831
1832
1833How do I get a list of all instances of a given class?
1834------------------------------------------------------
1835
1836Python does not keep track of all instances of a class (or of a built-in type).
1837You can program the class's constructor to keep track of all instances by
1838keeping a list of weak references to each instance.
1839
1840
Georg Brandl0f79cac2013-10-12 18:14:25 +02001841Why does the result of ``id()`` appear to be not unique?
1842--------------------------------------------------------
1843
1844The :func:`id` builtin returns an integer that is guaranteed to be unique during
1845the lifetime of the object. Since in CPython, this is the object's memory
1846address, it happens frequently that after an object is deleted from memory, the
1847next freshly created object is allocated at the same position in memory. This
1848is illustrated by this example:
1849
1850>>> id(1000)
185113901272
1852>>> id(2000)
185313901272
1854
1855The two ids belong to different integer objects that are created before, and
1856deleted immediately after execution of the ``id()`` call. To be sure that
1857objects whose id you want to examine are still alive, create another reference
1858to the object:
1859
1860>>> a = 1000; b = 2000
1861>>> id(a)
186213901272
1863>>> id(b)
186413891296
1865
1866
Georg Brandl6728c5a2009-10-11 18:31:23 +00001867Modules
1868=======
1869
1870How do I create a .pyc file?
1871----------------------------
1872
1873When a module is imported for the first time (or when the source is more recent
1874than the current compiled file) a ``.pyc`` file containing the compiled code
1875should be created in the same directory as the ``.py`` file.
1876
1877One reason that a ``.pyc`` file may not be created is permissions problems with
1878the directory. This can happen, for example, if you develop as one user but run
1879as another, such as if you are testing with a web server. Creation of a .pyc
1880file is automatic if you're importing a module and Python has the ability
1881(permissions, free space, etc...) to write the compiled module back to the
1882directory.
1883
R David Murrayff229842013-06-19 17:00:43 -04001884Running Python on a top level script is not considered an import and no
1885``.pyc`` will be created. For example, if you have a top-level module
1886``foo.py`` that imports another module ``xyz.py``, when you run ``foo``,
1887``xyz.pyc`` will be created since ``xyz`` is imported, but no ``foo.pyc`` file
1888will be created since ``foo.py`` isn't being imported.
Georg Brandl6728c5a2009-10-11 18:31:23 +00001889
R David Murrayff229842013-06-19 17:00:43 -04001890If you need to create ``foo.pyc`` -- that is, to create a ``.pyc`` file for a module
Georg Brandl6728c5a2009-10-11 18:31:23 +00001891that is not imported -- you can, using the :mod:`py_compile` and
1892:mod:`compileall` modules.
1893
1894The :mod:`py_compile` module can manually compile any module. One way is to use
1895the ``compile()`` function in that module interactively::
1896
1897 >>> import py_compile
R David Murrayff229842013-06-19 17:00:43 -04001898 >>> py_compile.compile('foo.py') # doctest: +SKIP
Georg Brandl6728c5a2009-10-11 18:31:23 +00001899
R David Murrayff229842013-06-19 17:00:43 -04001900This will write the ``.pyc`` to the same location as ``foo.py`` (or you can
Georg Brandl6728c5a2009-10-11 18:31:23 +00001901override that with the optional parameter ``cfile``).
1902
1903You can also automatically compile all files in a directory or directories using
1904the :mod:`compileall` module. You can do it from the shell prompt by running
1905``compileall.py`` and providing the path of a directory containing Python files
1906to compile::
1907
1908 python -m compileall .
1909
1910
1911How do I find the current module name?
1912--------------------------------------
1913
1914A module can find out its own module name by looking at the predefined global
1915variable ``__name__``. If this has the value ``'__main__'``, the program is
1916running as a script. Many modules that are usually used by importing them also
1917provide a command-line interface or a self-test, and only execute this code
1918after checking ``__name__``::
1919
1920 def main():
1921 print 'Running test...'
1922 ...
1923
1924 if __name__ == '__main__':
1925 main()
1926
1927
1928How can I have modules that mutually import each other?
1929-------------------------------------------------------
1930
1931Suppose you have the following modules:
1932
1933foo.py::
1934
1935 from bar import bar_var
1936 foo_var = 1
1937
1938bar.py::
1939
1940 from foo import foo_var
1941 bar_var = 2
1942
1943The problem is that the interpreter will perform the following steps:
1944
1945* main imports foo
1946* Empty globals for foo are created
1947* foo is compiled and starts executing
1948* foo imports bar
1949* Empty globals for bar are created
1950* bar is compiled and starts executing
1951* bar imports foo (which is a no-op since there already is a module named foo)
1952* bar.foo_var = foo.foo_var
1953
1954The last step fails, because Python isn't done with interpreting ``foo`` yet and
1955the global symbol dictionary for ``foo`` is still empty.
1956
1957The same thing happens when you use ``import foo``, and then try to access
1958``foo.foo_var`` in global code.
1959
1960There are (at least) three possible workarounds for this problem.
1961
1962Guido van Rossum recommends avoiding all uses of ``from <module> import ...``,
1963and placing all code inside functions. Initializations of global variables and
1964class variables should use constants or built-in functions only. This means
1965everything from an imported module is referenced as ``<module>.<name>``.
1966
1967Jim Roskind suggests performing steps in the following order in each module:
1968
1969* exports (globals, functions, and classes that don't need imported base
1970 classes)
1971* ``import`` statements
1972* active code (including globals that are initialized from imported values).
1973
1974van Rossum doesn't like this approach much because the imports appear in a
1975strange place, but it does work.
1976
1977Matthias Urlichs recommends restructuring your code so that the recursive import
1978is not necessary in the first place.
1979
1980These solutions are not mutually exclusive.
1981
1982
1983__import__('x.y.z') returns <module 'x'>; how do I get z?
1984---------------------------------------------------------
1985
Ezio Melottic468aba2014-08-04 19:34:29 +03001986Consider using the convenience function :func:`~importlib.import_module` from
1987:mod:`importlib` instead::
Georg Brandl6728c5a2009-10-11 18:31:23 +00001988
Ezio Melottic468aba2014-08-04 19:34:29 +03001989 z = importlib.import_module('x.y.z')
Georg Brandl6728c5a2009-10-11 18:31:23 +00001990
1991
1992When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
1993-------------------------------------------------------------------------------------------------
1994
1995For reasons of efficiency as well as consistency, Python only reads the module
1996file on the first time a module is imported. If it didn't, in a program
1997consisting of many modules where each one imports the same basic module, the
1998basic module would be parsed and re-parsed many times. To force rereading of a
1999changed module, do this::
2000
2001 import modname
2002 reload(modname)
2003
2004Warning: this technique is not 100% fool-proof. In particular, modules
2005containing statements like ::
2006
2007 from modname import some_objects
2008
2009will continue to work with the old version of the imported objects. If the
2010module contains class definitions, existing class instances will *not* be
2011updated to use the new class definition. This can result in the following
2012paradoxical behaviour:
2013
2014 >>> import cls
2015 >>> c = cls.C() # Create an instance of C
2016 >>> reload(cls)
2017 <module 'cls' from 'cls.pyc'>
2018 >>> isinstance(c, cls.C) # isinstance is false?!?
2019 False
2020
2021The nature of the problem is made clear if you print out the class objects:
2022
2023 >>> c.__class__
2024 <class cls.C at 0x7352a0>
2025 >>> cls.C
2026 <class cls.C at 0x4198d0>
2027