blob: d85563fee1135a67069d95d6fd4b170a91e08d11 [file] [log] [blame]
Georg Brandld7413152009-10-11 21:25:26 +00001:tocdepth: 2
2
3===============
4Programming FAQ
5===============
6
Georg Brandl44ea77b2013-03-28 13:28:44 +01007.. only:: html
8
9 .. contents::
Georg Brandld7413152009-10-11 21:25:26 +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
26graphical debugger. There is documentation for the IDLE debugger at
27http://www.python.org/idle/doc/idle2.html#Debugger.
28
29PythonWin is a Python IDE that includes a GUI debugger based on pdb. The
30Pythonwin debugger colors breakpoints and has quite a few cool features such as
31debugging non-Pythonwin programs. Pythonwin is available as part of the `Python
32for Windows Extensions <http://sourceforge.net/projects/pywin32/>`__ project and
33as a part of the ActivePython distribution (see
34http://www.activestate.com/Products/ActivePython/index.html).
35
36`Boa Constructor <http://boa-constructor.sourceforge.net/>`_ is an IDE and GUI
37builder that uses wxWidgets. It offers visual frame creation and manipulation,
38an object inspector, many views on the source like object browsers, inheritance
39hierarchies, doc string generated html documentation, an advanced debugger,
40integrated help, and Zope support.
41
42`Eric <http://www.die-offenbachs.de/eric/index.html>`_ is an IDE built on PyQt
43and the Scintilla editing component.
44
45Pydb is a version of the standard Python debugger pdb, modified for use with DDD
46(Data Display Debugger), a popular graphical debugger front end. Pydb can be
47found at http://bashdb.sourceforge.net/pydb/ and DDD can be found at
48http://www.gnu.org/software/ddd.
49
50There are a number of commercial Python IDEs that include graphical debuggers.
51They include:
52
53* Wing IDE (http://wingware.com/)
54* Komodo IDE (http://www.activestate.com/Products/Komodo)
55
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
64http://pychecker.sf.net.
65
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 Brandl495f7b52009-10-27 15:28:25 +000072http://www.logilab.org/card/pylint_manual provides a full list of Pylint's
73features.
Georg Brandld7413152009-10-11 21:25:26 +000074
75
76How can I create a stand-alone binary from a Python script?
77-----------------------------------------------------------
78
79You don't need the ability to compile Python to C code if all you want is a
80stand-alone program that users can download and run without having to install
81the Python distribution first. There are a number of tools that determine the
82set of modules required by a program and bind these modules together with a
83Python binary to produce a single executable.
84
85One is to use the freeze tool, which is included in the Python source tree as
86``Tools/freeze``. It converts Python byte code to C arrays; a C compiler you can
87embed all your modules into a new program, which is then linked with the
88standard Python modules.
89
90It works by scanning your source recursively for import statements (in both
91forms) and looking for the modules in the standard Python path as well as in the
92source directory (for built-in modules). It then turns the bytecode for modules
93written in Python into C code (array initializers that can be turned into code
94objects using the marshal module) and creates a custom-made config file that
95only contains those built-in modules which are actually used in the program. It
96then compiles the generated C code and links it with the rest of the Python
97interpreter to form a self-contained binary which acts exactly like your script.
98
99Obviously, freeze requires a C compiler. There are several other utilities
100which don't. One is Thomas Heller's py2exe (Windows only) at
101
102 http://www.py2exe.org/
103
104Another is Christian Tismer's `SQFREEZE <http://starship.python.net/crew/pirx>`_
105which appends the byte code to a specially-prepared Python interpreter that can
106find the byte code in the executable.
107
108Other tools include Fredrik Lundh's `Squeeze
109<http://www.pythonware.com/products/python/squeeze>`_ and Anthony Tuininga's
110`cx_Freeze <http://starship.python.net/crew/atuining/cx_Freeze/index.html>`_.
111
112
113Are there coding standards or a style guide for Python programs?
114----------------------------------------------------------------
115
116Yes. The coding style required for standard library modules is documented as
117:pep:`8`.
118
119
Georg Brandld7413152009-10-11 21:25:26 +0000120Core Language
121=============
122
R. David Murrayc04a6942009-11-14 22:21:32 +0000123Why am I getting an UnboundLocalError when the variable has a value?
124--------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000125
R. David Murrayc04a6942009-11-14 22:21:32 +0000126It can be a surprise to get the UnboundLocalError in previously working
127code when it is modified by adding an assignment statement somewhere in
128the body of a function.
Georg Brandld7413152009-10-11 21:25:26 +0000129
R. David Murrayc04a6942009-11-14 22:21:32 +0000130This code:
Georg Brandld7413152009-10-11 21:25:26 +0000131
R. David Murrayc04a6942009-11-14 22:21:32 +0000132 >>> x = 10
133 >>> def bar():
134 ... print(x)
135 >>> bar()
136 10
Georg Brandld7413152009-10-11 21:25:26 +0000137
R. David Murrayc04a6942009-11-14 22:21:32 +0000138works, but this code:
Georg Brandld7413152009-10-11 21:25:26 +0000139
R. David Murrayc04a6942009-11-14 22:21:32 +0000140 >>> x = 10
141 >>> def foo():
142 ... print(x)
143 ... x += 1
Georg Brandld7413152009-10-11 21:25:26 +0000144
R. David Murrayc04a6942009-11-14 22:21:32 +0000145results in an UnboundLocalError:
Georg Brandld7413152009-10-11 21:25:26 +0000146
R. David Murrayc04a6942009-11-14 22:21:32 +0000147 >>> foo()
148 Traceback (most recent call last):
149 ...
150 UnboundLocalError: local variable 'x' referenced before assignment
151
152This is because when you make an assignment to a variable in a scope, that
153variable becomes local to that scope and shadows any similarly named variable
154in the outer scope. Since the last statement in foo assigns a new value to
155``x``, the compiler recognizes it as a local variable. Consequently when the
R. David Murray18163c32009-11-14 22:27:22 +0000156earlier ``print(x)`` attempts to print the uninitialized local variable and
R. David Murrayc04a6942009-11-14 22:21:32 +0000157an error results.
158
159In the example above you can access the outer scope variable by declaring it
160global:
161
162 >>> x = 10
163 >>> def foobar():
164 ... global x
165 ... print(x)
166 ... x += 1
167 >>> foobar()
168 10
169
170This explicit declaration is required in order to remind you that (unlike the
171superficially analogous situation with class and instance variables) you are
172actually modifying the value of the variable in the outer scope:
173
174 >>> print(x)
175 11
176
177You can do a similar thing in a nested scope using the :keyword:`nonlocal`
178keyword:
179
180 >>> def foo():
181 ... x = 10
182 ... def bar():
183 ... nonlocal x
184 ... print(x)
185 ... x += 1
186 ... bar()
187 ... print(x)
188 >>> foo()
189 10
190 11
Georg Brandld7413152009-10-11 21:25:26 +0000191
192
193What are the rules for local and global variables in Python?
194------------------------------------------------------------
195
196In Python, variables that are only referenced inside a function are implicitly
197global. If a variable is assigned a new value anywhere within the function's
198body, it's assumed to be a local. If a variable is ever assigned a new value
199inside the function, the variable is implicitly local, and you need to
200explicitly declare it as 'global'.
201
202Though a bit surprising at first, a moment's consideration explains this. On
203one hand, requiring :keyword:`global` for assigned variables provides a bar
204against unintended side-effects. On the other hand, if ``global`` was required
205for all global references, you'd be using ``global`` all the time. You'd have
Georg Brandlc4a55fc2010-02-06 18:46:57 +0000206to declare as global every reference to a built-in function or to a component of
Georg Brandld7413152009-10-11 21:25:26 +0000207an imported module. This clutter would defeat the usefulness of the ``global``
208declaration for identifying side-effects.
209
210
Ezio Melotticad8b0f2013-01-05 00:50:46 +0200211Why do lambdas defined in a loop with different values all return the same result?
212----------------------------------------------------------------------------------
213
214Assume you use a for loop to define a few different lambdas (or even plain
215functions), e.g.::
216
R David Murrayfdf95032013-06-19 16:58:26 -0400217 >>> squares = []
218 >>> for x in range(5):
219 ... squares.append(lambda: x**2)
Ezio Melotticad8b0f2013-01-05 00:50:46 +0200220
221This gives you a list that contains 5 lambdas that calculate ``x**2``. You
222might expect that, when called, they would return, respectively, ``0``, ``1``,
223``4``, ``9``, and ``16``. However, when you actually try you will see that
224they all return ``16``::
225
226 >>> squares[2]()
227 16
228 >>> squares[4]()
229 16
230
231This happens because ``x`` is not local to the lambdas, but is defined in
232the outer scope, and it is accessed when the lambda is called --- not when it
233is defined. At the end of the loop, the value of ``x`` is ``4``, so all the
234functions now return ``4**2``, i.e. ``16``. You can also verify this by
235changing the value of ``x`` and see how the results of the lambdas change::
236
237 >>> x = 8
238 >>> squares[2]()
239 64
240
241In order to avoid this, you need to save the values in variables local to the
242lambdas, so that they don't rely on the value of the global ``x``::
243
R David Murrayfdf95032013-06-19 16:58:26 -0400244 >>> squares = []
245 >>> for x in range(5):
246 ... squares.append(lambda n=x: n**2)
Ezio Melotticad8b0f2013-01-05 00:50:46 +0200247
248Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed
249when the lambda is defined so that it has the same value that ``x`` had at
250that point in the loop. This means that the value of ``n`` will be ``0``
251in the first lambda, ``1`` in the second, ``2`` in the third, and so on.
252Therefore each lambda will now return the correct result::
253
254 >>> squares[2]()
255 4
256 >>> squares[4]()
257 16
258
259Note that this behaviour is not peculiar to lambdas, but applies to regular
260functions too.
261
262
Georg Brandld7413152009-10-11 21:25:26 +0000263How do I share global variables across modules?
264------------------------------------------------
265
266The canonical way to share information across modules within a single program is
267to create a special module (often called config or cfg). Just import the config
268module in all modules of your application; the module then becomes available as
269a global name. Because there is only one instance of each module, any changes
270made to the module object get reflected everywhere. For example:
271
272config.py::
273
274 x = 0 # Default value of the 'x' configuration setting
275
276mod.py::
277
278 import config
279 config.x = 1
280
281main.py::
282
283 import config
284 import mod
Georg Brandl62eaaf62009-12-19 17:51:41 +0000285 print(config.x)
Georg Brandld7413152009-10-11 21:25:26 +0000286
287Note that using a module is also the basis for implementing the Singleton design
288pattern, for the same reason.
289
290
291What are the "best practices" for using import in a module?
292-----------------------------------------------------------
293
294In general, don't use ``from modulename import *``. Doing so clutters the
295importer's namespace. Some people avoid this idiom even with the few modules
296that were designed to be imported in this manner. Modules designed in this
Georg Brandld404fa62009-10-13 16:55:12 +0000297manner include :mod:`tkinter`, and :mod:`threading`.
Georg Brandld7413152009-10-11 21:25:26 +0000298
299Import modules at the top of a file. Doing so makes it clear what other modules
300your code requires and avoids questions of whether the module name is in scope.
301Using one import per line makes it easy to add and delete module imports, but
302using multiple imports per line uses less screen space.
303
304It's good practice if you import modules in the following order:
305
Georg Brandl62eaaf62009-12-19 17:51:41 +00003061. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re``
Georg Brandld7413152009-10-11 21:25:26 +00003072. third-party library modules (anything installed in Python's site-packages
308 directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc.
3093. locally-developed modules
310
311Never use relative package imports. If you're writing code that's in the
312``package.sub.m1`` module and want to import ``package.sub.m2``, do not just
Georg Brandl11b63622009-12-20 14:21:27 +0000313write ``from . import m2``, even though it's legal. Write ``from package.sub
314import m2`` instead. See :pep:`328` for details.
Georg Brandld7413152009-10-11 21:25:26 +0000315
316It is sometimes necessary to move imports to a function or class to avoid
317problems with circular imports. Gordon McMillan says:
318
319 Circular imports are fine where both modules use the "import <module>" form
320 of import. They fail when the 2nd module wants to grab a name out of the
321 first ("from module import name") and the import is at the top level. That's
322 because names in the 1st are not yet available, because the first module is
323 busy importing the 2nd.
324
325In this case, if the second module is only used in one function, then the import
326can easily be moved into that function. By the time the import is called, the
327first module will have finished initializing, and the second module can do its
328import.
329
330It may also be necessary to move imports out of the top level of code if some of
331the modules are platform-specific. In that case, it may not even be possible to
332import all of the modules at the top of the file. In this case, importing the
333correct modules in the corresponding platform-specific code is a good option.
334
335Only move imports into a local scope, such as inside a function definition, if
336it's necessary to solve a problem such as avoiding a circular import or are
337trying to reduce the initialization time of a module. This technique is
338especially helpful if many of the imports are unnecessary depending on how the
339program executes. You may also want to move imports into a function if the
340modules are only ever used in that function. Note that loading a module the
341first time may be expensive because of the one time initialization of the
342module, but loading a module multiple times is virtually free, costing only a
343couple of dictionary lookups. Even if the module name has gone out of scope,
344the module is probably available in :data:`sys.modules`.
345
346If only instances of a specific class use a module, then it is reasonable to
347import the module in the class's ``__init__`` method and then assign the module
348to an instance variable so that the module is always available (via that
349instance variable) during the life of the object. Note that to delay an import
350until the class is instantiated, the import must be inside a method. Putting
351the import inside the class but outside of any method still causes the import to
352occur when the module is initialized.
353
354
Ezio Melotti898eb822014-07-06 20:53:27 +0300355Why are default values shared between objects?
356----------------------------------------------
357
358This type of bug commonly bites neophyte programmers. Consider this function::
359
360 def foo(mydict={}): # Danger: shared reference to one dict for all calls
361 ... compute something ...
362 mydict[key] = value
363 return mydict
364
365The first time you call this function, ``mydict`` contains a single item. The
366second time, ``mydict`` contains two items because when ``foo()`` begins
367executing, ``mydict`` starts out with an item already in it.
368
369It is often expected that a function call creates new objects for default
370values. This is not what happens. Default values are created exactly once, when
371the function is defined. If that object is changed, like the dictionary in this
372example, subsequent calls to the function will refer to this changed object.
373
374By definition, immutable objects such as numbers, strings, tuples, and ``None``,
375are safe from change. Changes to mutable objects such as dictionaries, lists,
376and class instances can lead to confusion.
377
378Because of this feature, it is good programming practice to not use mutable
379objects as default values. Instead, use ``None`` as the default value and
380inside the function, check if the parameter is ``None`` and create a new
381list/dictionary/whatever if it is. For example, don't write::
382
383 def foo(mydict={}):
384 ...
385
386but::
387
388 def foo(mydict=None):
389 if mydict is None:
390 mydict = {} # create a new dict for local namespace
391
392This feature can be useful. When you have a function that's time-consuming to
393compute, a common technique is to cache the parameters and the resulting value
394of each call to the function, and return the cached value if the same value is
395requested again. This is called "memoizing", and can be implemented like this::
396
397 # Callers will never provide a third parameter for this function.
398 def expensive(arg1, arg2, _cache={}):
399 if (arg1, arg2) in _cache:
400 return _cache[(arg1, arg2)]
401
402 # Calculate the value
403 result = ... expensive computation ...
404 _cache[(arg1, arg2)] = result # Store result in the cache
405 return result
406
407You could use a global variable containing a dictionary instead of the default
408value; it's a matter of taste.
409
410
Georg Brandld7413152009-10-11 21:25:26 +0000411How can I pass optional or keyword parameters from one function to another?
412---------------------------------------------------------------------------
413
414Collect the arguments using the ``*`` and ``**`` specifiers in the function's
415parameter list; this gives you the positional arguments as a tuple and the
416keyword arguments as a dictionary. You can then pass these arguments when
417calling another function by using ``*`` and ``**``::
418
419 def f(x, *args, **kwargs):
420 ...
421 kwargs['width'] = '14.3c'
422 ...
423 g(x, *args, **kwargs)
424
Georg Brandld7413152009-10-11 21:25:26 +0000425
Chris Jerdonekb4309942012-12-25 14:54:44 -0800426.. index::
427 single: argument; difference from parameter
428 single: parameter; difference from argument
429
Chris Jerdonekc2a7fd62012-11-28 02:29:33 -0800430.. _faq-argument-vs-parameter:
431
432What is the difference between arguments and parameters?
433--------------------------------------------------------
434
435:term:`Parameters <parameter>` are defined by the names that appear in a
436function definition, whereas :term:`arguments <argument>` are the values
437actually passed to a function when calling it. Parameters define what types of
438arguments a function can accept. For example, given the function definition::
439
440 def func(foo, bar=None, **kwargs):
441 pass
442
443*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling
444``func``, for example::
445
446 func(42, bar=314, extra=somevar)
447
448the values ``42``, ``314``, and ``somevar`` are arguments.
449
450
Georg Brandld7413152009-10-11 21:25:26 +0000451How do I write a function with output parameters (call by reference)?
452---------------------------------------------------------------------
453
454Remember that arguments are passed by assignment in Python. Since assignment
455just creates references to objects, there's no alias between an argument name in
456the caller and callee, and so no call-by-reference per se. You can achieve the
457desired effect in a number of ways.
458
4591) By returning a tuple of the results::
460
461 def func2(a, b):
462 a = 'new-value' # a and b are local names
463 b = b + 1 # assigned to new objects
464 return a, b # return new values
465
466 x, y = 'old-value', 99
467 x, y = func2(x, y)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000468 print(x, y) # output: new-value 100
Georg Brandld7413152009-10-11 21:25:26 +0000469
470 This is almost always the clearest solution.
471
4722) By using global variables. This isn't thread-safe, and is not recommended.
473
4743) By passing a mutable (changeable in-place) object::
475
476 def func1(a):
477 a[0] = 'new-value' # 'a' references a mutable list
478 a[1] = a[1] + 1 # changes a shared object
479
480 args = ['old-value', 99]
481 func1(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000482 print(args[0], args[1]) # output: new-value 100
Georg Brandld7413152009-10-11 21:25:26 +0000483
4844) By passing in a dictionary that gets mutated::
485
486 def func3(args):
487 args['a'] = 'new-value' # args is a mutable dictionary
488 args['b'] = args['b'] + 1 # change it in-place
489
490 args = {'a':' old-value', 'b': 99}
491 func3(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000492 print(args['a'], args['b'])
Georg Brandld7413152009-10-11 21:25:26 +0000493
4945) Or bundle up values in a class instance::
495
496 class callByRef:
497 def __init__(self, **args):
498 for (key, value) in args.items():
499 setattr(self, key, value)
500
501 def func4(args):
502 args.a = 'new-value' # args is a mutable callByRef
503 args.b = args.b + 1 # change object in-place
504
505 args = callByRef(a='old-value', b=99)
506 func4(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000507 print(args.a, args.b)
Georg Brandld7413152009-10-11 21:25:26 +0000508
509
510 There's almost never a good reason to get this complicated.
511
512Your best choice is to return a tuple containing the multiple results.
513
514
515How do you make a higher order function in Python?
516--------------------------------------------------
517
518You have two choices: you can use nested scopes or you can use callable objects.
519For example, suppose you wanted to define ``linear(a,b)`` which returns a
520function ``f(x)`` that computes the value ``a*x+b``. Using nested scopes::
521
522 def linear(a, b):
523 def result(x):
524 return a * x + b
525 return result
526
527Or using a callable object::
528
529 class linear:
530
531 def __init__(self, a, b):
532 self.a, self.b = a, b
533
534 def __call__(self, x):
535 return self.a * x + self.b
536
537In both cases, ::
538
539 taxes = linear(0.3, 2)
540
541gives a callable object where ``taxes(10e6) == 0.3 * 10e6 + 2``.
542
543The callable object approach has the disadvantage that it is a bit slower and
544results in slightly longer code. However, note that a collection of callables
545can share their signature via inheritance::
546
547 class exponential(linear):
548 # __init__ inherited
549 def __call__(self, x):
550 return self.a * (x ** self.b)
551
552Object can encapsulate state for several methods::
553
554 class counter:
555
556 value = 0
557
558 def set(self, x):
559 self.value = x
560
561 def up(self):
562 self.value = self.value + 1
563
564 def down(self):
565 self.value = self.value - 1
566
567 count = counter()
568 inc, dec, reset = count.up, count.down, count.set
569
570Here ``inc()``, ``dec()`` and ``reset()`` act like functions which share the
571same counting variable.
572
573
574How do I copy an object in Python?
575----------------------------------
576
577In general, try :func:`copy.copy` or :func:`copy.deepcopy` for the general case.
578Not all objects can be copied, but most can.
579
580Some objects can be copied more easily. Dictionaries have a :meth:`~dict.copy`
581method::
582
583 newdict = olddict.copy()
584
585Sequences can be copied by slicing::
586
587 new_l = l[:]
588
589
590How can I find the methods or attributes of an object?
591------------------------------------------------------
592
593For an instance x of a user-defined class, ``dir(x)`` returns an alphabetized
594list of the names containing the instance attributes and methods and attributes
595defined by its class.
596
597
598How can my code discover the name of an object?
599-----------------------------------------------
600
601Generally speaking, it can't, because objects don't really have names.
602Essentially, assignment always binds a name to a value; The same is true of
603``def`` and ``class`` statements, but in that case the value is a
604callable. Consider the following code::
605
606 class A:
607 pass
608
609 B = A
610
611 a = B()
612 b = a
Georg Brandl62eaaf62009-12-19 17:51:41 +0000613 print(b)
614 <__main__.A object at 0x16D07CC>
615 print(a)
616 <__main__.A object at 0x16D07CC>
Georg Brandld7413152009-10-11 21:25:26 +0000617
618Arguably the class has a name: even though it is bound to two names and invoked
619through the name B the created instance is still reported as an instance of
620class A. However, it is impossible to say whether the instance's name is a or
621b, since both names are bound to the same value.
622
623Generally speaking it should not be necessary for your code to "know the names"
624of particular values. Unless you are deliberately writing introspective
625programs, this is usually an indication that a change of approach might be
626beneficial.
627
628In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to
629this question:
630
631 The same way as you get the name of that cat you found on your porch: the cat
632 (object) itself cannot tell you its name, and it doesn't really care -- so
633 the only way to find out what it's called is to ask all your neighbours
634 (namespaces) if it's their cat (object)...
635
636 ....and don't be surprised if you'll find that it's known by many names, or
637 no name at all!
638
639
640What's up with the comma operator's precedence?
641-----------------------------------------------
642
643Comma is not an operator in Python. Consider this session::
644
645 >>> "a" in "b", "a"
Georg Brandl62eaaf62009-12-19 17:51:41 +0000646 (False, 'a')
Georg Brandld7413152009-10-11 21:25:26 +0000647
648Since the comma is not an operator, but a separator between expressions the
649above is evaluated as if you had entered::
650
R David Murrayfdf95032013-06-19 16:58:26 -0400651 ("a" in "b"), "a"
Georg Brandld7413152009-10-11 21:25:26 +0000652
653not::
654
R David Murrayfdf95032013-06-19 16:58:26 -0400655 "a" in ("b", "a")
Georg Brandld7413152009-10-11 21:25:26 +0000656
657The same is true of the various assignment operators (``=``, ``+=`` etc). They
658are not truly operators but syntactic delimiters in assignment statements.
659
660
661Is there an equivalent of C's "?:" ternary operator?
662----------------------------------------------------
663
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100664Yes, there is. The syntax is as follows::
Georg Brandld7413152009-10-11 21:25:26 +0000665
666 [on_true] if [expression] else [on_false]
667
668 x, y = 50, 25
Georg Brandld7413152009-10-11 21:25:26 +0000669 small = x if x < y else y
670
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100671Before this syntax was introduced in Python 2.5, a common idiom was to use
672logical operators::
Georg Brandld7413152009-10-11 21:25:26 +0000673
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100674 [expression] and [on_true] or [on_false]
Georg Brandld7413152009-10-11 21:25:26 +0000675
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100676However, this idiom is unsafe, as it can give wrong results when *on_true*
677has a false boolean value. Therefore, it is always better to use
678the ``... if ... else ...`` form.
Georg Brandld7413152009-10-11 21:25:26 +0000679
680
681Is it possible to write obfuscated one-liners in Python?
682--------------------------------------------------------
683
684Yes. Usually this is done by nesting :keyword:`lambda` within
685:keyword:`lambda`. See the following three examples, due to Ulf Bartelt::
686
Georg Brandl62eaaf62009-12-19 17:51:41 +0000687 from functools import reduce
688
Georg Brandld7413152009-10-11 21:25:26 +0000689 # Primes < 1000
Georg Brandl62eaaf62009-12-19 17:51:41 +0000690 print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
691 map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))))
Georg Brandld7413152009-10-11 21:25:26 +0000692
693 # First 10 Fibonacci numbers
Georg Brandl62eaaf62009-12-19 17:51:41 +0000694 print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1:
695 f(x,f), range(10))))
Georg Brandld7413152009-10-11 21:25:26 +0000696
697 # Mandelbrot set
Georg Brandl62eaaf62009-12-19 17:51:41 +0000698 print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
Georg Brandld7413152009-10-11 21:25:26 +0000699 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
700 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
701 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
702 >=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(
703 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
Georg Brandl62eaaf62009-12-19 17:51:41 +0000704 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24))
Georg Brandld7413152009-10-11 21:25:26 +0000705 # \___ ___/ \___ ___/ | | |__ lines on screen
706 # V V | |______ columns on screen
707 # | | |__________ maximum of "iterations"
708 # | |_________________ range on y axis
709 # |____________________________ range on x axis
710
711Don't try this at home, kids!
712
713
714Numbers and strings
715===================
716
717How do I specify hexadecimal and octal integers?
718------------------------------------------------
719
Georg Brandl62eaaf62009-12-19 17:51:41 +0000720To specify an octal digit, precede the octal value with a zero, and then a lower
721or uppercase "o". For example, to set the variable "a" to the octal value "10"
722(8 in decimal), type::
Georg Brandld7413152009-10-11 21:25:26 +0000723
Georg Brandl62eaaf62009-12-19 17:51:41 +0000724 >>> a = 0o10
Georg Brandld7413152009-10-11 21:25:26 +0000725 >>> a
726 8
727
728Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,
729and then a lower or uppercase "x". Hexadecimal digits can be specified in lower
730or uppercase. For example, in the Python interpreter::
731
732 >>> a = 0xa5
733 >>> a
734 165
735 >>> b = 0XB2
736 >>> b
737 178
738
739
Georg Brandl62eaaf62009-12-19 17:51:41 +0000740Why does -22 // 10 return -3?
741-----------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000742
743It's primarily driven by the desire that ``i % j`` have the same sign as ``j``.
744If you want that, and also want::
745
Georg Brandl62eaaf62009-12-19 17:51:41 +0000746 i == (i // j) * j + (i % j)
Georg Brandld7413152009-10-11 21:25:26 +0000747
748then integer division has to return the floor. C also requires that identity to
Georg Brandl62eaaf62009-12-19 17:51:41 +0000749hold, and then compilers that truncate ``i // j`` need to make ``i % j`` have
750the same sign as ``i``.
Georg Brandld7413152009-10-11 21:25:26 +0000751
752There are few real use cases for ``i % j`` when ``j`` is negative. When ``j``
753is positive, there are many, and in virtually all of them it's more useful for
754``i % j`` to be ``>= 0``. If the clock says 10 now, what did it say 200 hours
755ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to
756bite.
757
758
759How do I convert a string to a number?
760--------------------------------------
761
762For integers, use the built-in :func:`int` type constructor, e.g. ``int('144')
763== 144``. Similarly, :func:`float` converts to floating-point,
764e.g. ``float('144') == 144.0``.
765
766By default, these interpret the number as decimal, so that ``int('0144') ==
767144`` and ``int('0x144')`` raises :exc:`ValueError`. ``int(string, base)`` takes
768the base to convert from as a second optional argument, so ``int('0x144', 16) ==
769324``. If the base is specified as 0, the number is interpreted using Python's
Eric V. Smithfc9a4d82014-04-14 07:41:52 -0400770rules: a leading '0o' indicates octal, and '0x' indicates a hex number.
Georg Brandld7413152009-10-11 21:25:26 +0000771
772Do not use the built-in function :func:`eval` if all you need is to convert
773strings to numbers. :func:`eval` will be significantly slower and it presents a
774security risk: someone could pass you a Python expression that might have
775unwanted side effects. For example, someone could pass
776``__import__('os').system("rm -rf $HOME")`` which would erase your home
777directory.
778
779:func:`eval` also has the effect of interpreting numbers as Python expressions,
Georg Brandl62eaaf62009-12-19 17:51:41 +0000780so that e.g. ``eval('09')`` gives a syntax error because Python does not allow
781leading '0' in a decimal number (except '0').
Georg Brandld7413152009-10-11 21:25:26 +0000782
783
784How do I convert a number to a string?
785--------------------------------------
786
787To convert, e.g., the number 144 to the string '144', use the built-in type
788constructor :func:`str`. If you want a hexadecimal or octal representation, use
Georg Brandl62eaaf62009-12-19 17:51:41 +0000789the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see
790the :ref:`string-formatting` section, e.g. ``"{:04d}".format(144)`` yields
Eric V. Smith04d8a242014-04-14 07:52:53 -0400791``'0144'`` and ``"{:.3f}".format(1.0/3.0)`` yields ``'0.333'``.
Georg Brandld7413152009-10-11 21:25:26 +0000792
793
794How do I modify a string in place?
795----------------------------------
796
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100797You can't, because strings are immutable. In most situations, you should
798simply construct a new string from the various parts you want to assemble
799it from. However, if you need an object with the ability to modify in-place
800unicode data, try using a :class:`io.StringIO` object or the :mod:`array`
801module::
Georg Brandld7413152009-10-11 21:25:26 +0000802
R David Murrayfdf95032013-06-19 16:58:26 -0400803 >>> import io
Georg Brandld7413152009-10-11 21:25:26 +0000804 >>> s = "Hello, world"
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100805 >>> sio = io.StringIO(s)
806 >>> sio.getvalue()
807 'Hello, world'
808 >>> sio.seek(7)
809 7
810 >>> sio.write("there!")
811 6
812 >>> sio.getvalue()
Georg Brandld7413152009-10-11 21:25:26 +0000813 'Hello, there!'
814
815 >>> import array
Georg Brandl62eaaf62009-12-19 17:51:41 +0000816 >>> a = array.array('u', s)
817 >>> print(a)
818 array('u', 'Hello, world')
819 >>> a[0] = 'y'
820 >>> print(a)
R David Murrayfdf95032013-06-19 16:58:26 -0400821 array('u', 'yello, world')
Georg Brandl62eaaf62009-12-19 17:51:41 +0000822 >>> a.tounicode()
Georg Brandld7413152009-10-11 21:25:26 +0000823 'yello, world'
824
825
826How do I use strings to call functions/methods?
827-----------------------------------------------
828
829There are various techniques.
830
831* The best is to use a dictionary that maps strings to functions. The primary
832 advantage of this technique is that the strings do not need to match the names
833 of the functions. This is also the primary technique used to emulate a case
834 construct::
835
836 def a():
837 pass
838
839 def b():
840 pass
841
842 dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
843
844 dispatch[get_input()]() # Note trailing parens to call function
845
846* Use the built-in function :func:`getattr`::
847
848 import foo
849 getattr(foo, 'bar')()
850
851 Note that :func:`getattr` works on any object, including classes, class
852 instances, modules, and so on.
853
854 This is used in several places in the standard library, like this::
855
856 class Foo:
857 def do_foo(self):
858 ...
859
860 def do_bar(self):
861 ...
862
863 f = getattr(foo_instance, 'do_' + opname)
864 f()
865
866
867* Use :func:`locals` or :func:`eval` to resolve the function name::
868
869 def myFunc():
Georg Brandl62eaaf62009-12-19 17:51:41 +0000870 print("hello")
Georg Brandld7413152009-10-11 21:25:26 +0000871
872 fname = "myFunc"
873
874 f = locals()[fname]
875 f()
876
877 f = eval(fname)
878 f()
879
880 Note: Using :func:`eval` is slow and dangerous. If you don't have absolute
881 control over the contents of the string, someone could pass a string that
882 resulted in an arbitrary function being executed.
883
884Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
885-------------------------------------------------------------------------------------
886
Antoine Pitrouf3520402011-12-03 22:19:55 +0100887You can use ``S.rstrip("\r\n")`` to remove all occurrences of any line
888terminator from the end of the string ``S`` without removing other trailing
889whitespace. If the string ``S`` represents more than one line, with several
890empty lines at the end, the line terminators for all the blank lines will
891be removed::
Georg Brandld7413152009-10-11 21:25:26 +0000892
893 >>> lines = ("line 1 \r\n"
894 ... "\r\n"
895 ... "\r\n")
896 >>> lines.rstrip("\n\r")
Georg Brandl62eaaf62009-12-19 17:51:41 +0000897 'line 1 '
Georg Brandld7413152009-10-11 21:25:26 +0000898
899Since this is typically only desired when reading text one line at a time, using
900``S.rstrip()`` this way works well.
901
Georg Brandld7413152009-10-11 21:25:26 +0000902
903Is there a scanf() or sscanf() equivalent?
904------------------------------------------
905
906Not as such.
907
908For simple input parsing, the easiest approach is usually to split the line into
909whitespace-delimited words using the :meth:`~str.split` method of string objects
910and then convert decimal strings to numeric values using :func:`int` or
911:func:`float`. ``split()`` supports an optional "sep" parameter which is useful
912if the line uses something other than whitespace as a separator.
913
Brian Curtin5a7a52f2010-09-23 13:45:21 +0000914For more complicated input parsing, regular expressions are more powerful
Georg Brandl60203b42010-10-06 10:11:56 +0000915than C's :c:func:`sscanf` and better suited for the task.
Georg Brandld7413152009-10-11 21:25:26 +0000916
917
Georg Brandl62eaaf62009-12-19 17:51:41 +0000918What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?
919-------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000920
Georg Brandl62eaaf62009-12-19 17:51:41 +0000921See the :ref:`unicode-howto`.
Georg Brandld7413152009-10-11 21:25:26 +0000922
923
Antoine Pitrou432259f2011-12-09 23:10:31 +0100924Performance
925===========
926
927My program is too slow. How do I speed it up?
928---------------------------------------------
929
930That's a tough one, in general. First, here are a list of things to
931remember before diving further:
932
Georg Brandl300a6912012-03-14 22:40:08 +0100933* Performance characteristics vary across Python implementations. This FAQ
Antoine Pitrou432259f2011-12-09 23:10:31 +0100934 focusses on :term:`CPython`.
Georg Brandl300a6912012-03-14 22:40:08 +0100935* Behaviour can vary across operating systems, especially when talking about
Antoine Pitrou432259f2011-12-09 23:10:31 +0100936 I/O or multi-threading.
937* You should always find the hot spots in your program *before* attempting to
938 optimize any code (see the :mod:`profile` module).
939* Writing benchmark scripts will allow you to iterate quickly when searching
940 for improvements (see the :mod:`timeit` module).
941* It is highly recommended to have good code coverage (through unit testing
942 or any other technique) before potentially introducing regressions hidden
943 in sophisticated optimizations.
944
945That being said, there are many tricks to speed up Python code. Here are
946some general principles which go a long way towards reaching acceptable
947performance levels:
948
949* Making your algorithms faster (or changing to faster ones) can yield
950 much larger benefits than trying to sprinkle micro-optimization tricks
951 all over your code.
952
953* Use the right data structures. Study documentation for the :ref:`bltin-types`
954 and the :mod:`collections` module.
955
956* When the standard library provides a primitive for doing something, it is
957 likely (although not guaranteed) to be faster than any alternative you
958 may come up with. This is doubly true for primitives written in C, such
959 as builtins and some extension types. For example, be sure to use
960 either the :meth:`list.sort` built-in method or the related :func:`sorted`
961 function to do sorting (and see the
962 `sorting mini-HOWTO <http://wiki.python.org/moin/HowTo/Sorting>`_ for examples
963 of moderately advanced usage).
964
965* Abstractions tend to create indirections and force the interpreter to work
966 more. If the levels of indirection outweigh the amount of useful work
967 done, your program will be slower. You should avoid excessive abstraction,
968 especially under the form of tiny functions or methods (which are also often
969 detrimental to readability).
970
971If you have reached the limit of what pure Python can allow, there are tools
972to take you further away. For example, `Cython <http://cython.org>`_ can
973compile a slightly modified version of Python code into a C extension, and
974can be used on many different platforms. Cython can take advantage of
975compilation (and optional type annotations) to make your code significantly
976faster than when interpreted. If you are confident in your C programming
977skills, you can also :ref:`write a C extension module <extending-index>`
978yourself.
979
980.. seealso::
981 The wiki page devoted to `performance tips
982 <http://wiki.python.org/moin/PythonSpeed/PerformanceTips>`_.
983
984.. _efficient_string_concatenation:
985
Antoine Pitroufd9ebd42011-11-25 16:33:53 +0100986What is the most efficient way to concatenate many strings together?
987--------------------------------------------------------------------
988
989:class:`str` and :class:`bytes` objects are immutable, therefore concatenating
990many strings together is inefficient as each concatenation creates a new
991object. In the general case, the total runtime cost is quadratic in the
992total string length.
993
994To accumulate many :class:`str` objects, the recommended idiom is to place
995them into a list and call :meth:`str.join` at the end::
996
997 chunks = []
998 for s in my_strings:
999 chunks.append(s)
1000 result = ''.join(chunks)
1001
1002(another reasonably efficient idiom is to use :class:`io.StringIO`)
1003
1004To accumulate many :class:`bytes` objects, the recommended idiom is to extend
1005a :class:`bytearray` object using in-place concatenation (the ``+=`` operator)::
1006
1007 result = bytearray()
1008 for b in my_bytes_objects:
1009 result += b
1010
1011
Georg Brandld7413152009-10-11 21:25:26 +00001012Sequences (Tuples/Lists)
1013========================
1014
1015How do I convert between tuples and lists?
1016------------------------------------------
1017
1018The type constructor ``tuple(seq)`` converts any sequence (actually, any
1019iterable) into a tuple with the same items in the same order.
1020
1021For example, ``tuple([1, 2, 3])`` yields ``(1, 2, 3)`` and ``tuple('abc')``
1022yields ``('a', 'b', 'c')``. If the argument is a tuple, it does not make a copy
1023but returns the same object, so it is cheap to call :func:`tuple` when you
1024aren't sure that an object is already a tuple.
1025
1026The type constructor ``list(seq)`` converts any sequence or iterable into a list
1027with the same items in the same order. For example, ``list((1, 2, 3))`` yields
1028``[1, 2, 3]`` and ``list('abc')`` yields ``['a', 'b', 'c']``. If the argument
1029is a list, it makes a copy just like ``seq[:]`` would.
1030
1031
1032What's a negative index?
1033------------------------
1034
1035Python sequences are indexed with positive numbers and negative numbers. For
1036positive numbers 0 is the first index 1 is the second index and so forth. For
1037negative indices -1 is the last index and -2 is the penultimate (next to last)
1038index and so forth. Think of ``seq[-n]`` as the same as ``seq[len(seq)-n]``.
1039
1040Using negative indices can be very convenient. For example ``S[:-1]`` is all of
1041the string except for its last character, which is useful for removing the
1042trailing newline from a string.
1043
1044
1045How do I iterate over a sequence in reverse order?
1046--------------------------------------------------
1047
Georg Brandlc4a55fc2010-02-06 18:46:57 +00001048Use the :func:`reversed` built-in function, which is new in Python 2.4::
Georg Brandld7413152009-10-11 21:25:26 +00001049
1050 for x in reversed(sequence):
1051 ... # do something with x...
1052
1053This won't touch your original sequence, but build a new copy with reversed
1054order to iterate over.
1055
1056With Python 2.3, you can use an extended slice syntax::
1057
1058 for x in sequence[::-1]:
1059 ... # do something with x...
1060
1061
1062How do you remove duplicates from a list?
1063-----------------------------------------
1064
1065See the Python Cookbook for a long discussion of many ways to do this:
1066
1067 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
1068
1069If you don't mind reordering the list, sort it and then scan from the end of the
1070list, deleting duplicates as you go::
1071
Georg Brandl62eaaf62009-12-19 17:51:41 +00001072 if mylist:
1073 mylist.sort()
1074 last = mylist[-1]
1075 for i in range(len(mylist)-2, -1, -1):
1076 if last == mylist[i]:
1077 del mylist[i]
Georg Brandld7413152009-10-11 21:25:26 +00001078 else:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001079 last = mylist[i]
Georg Brandld7413152009-10-11 21:25:26 +00001080
Antoine Pitrouf3520402011-12-03 22:19:55 +01001081If all elements of the list may be used as set keys (i.e. they are all
1082:term:`hashable`) this is often faster ::
Georg Brandld7413152009-10-11 21:25:26 +00001083
Georg Brandl62eaaf62009-12-19 17:51:41 +00001084 mylist = list(set(mylist))
Georg Brandld7413152009-10-11 21:25:26 +00001085
1086This converts the list into a set, thereby removing duplicates, and then back
1087into a list.
1088
1089
1090How do you make an array in Python?
1091-----------------------------------
1092
1093Use a list::
1094
1095 ["this", 1, "is", "an", "array"]
1096
1097Lists are equivalent to C or Pascal arrays in their time complexity; the primary
1098difference is that a Python list can contain objects of many different types.
1099
1100The ``array`` module also provides methods for creating arrays of fixed types
1101with compact representations, but they are slower to index than lists. Also
1102note that the Numeric extensions and others define array-like structures with
1103various characteristics as well.
1104
1105To get Lisp-style linked lists, you can emulate cons cells using tuples::
1106
1107 lisp_list = ("like", ("this", ("example", None) ) )
1108
1109If mutability is desired, you could use lists instead of tuples. Here the
1110analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is
1111``lisp_list[1]``. Only do this if you're sure you really need to, because it's
1112usually a lot slower than using Python lists.
1113
1114
1115How do I create a multidimensional list?
1116----------------------------------------
1117
1118You probably tried to make a multidimensional array like this::
1119
R David Murrayfdf95032013-06-19 16:58:26 -04001120 >>> A = [[None] * 2] * 3
Georg Brandld7413152009-10-11 21:25:26 +00001121
1122This looks correct if you print it::
1123
1124 >>> A
1125 [[None, None], [None, None], [None, None]]
1126
1127But when you assign a value, it shows up in multiple places:
1128
1129 >>> A[0][0] = 5
1130 >>> A
1131 [[5, None], [5, None], [5, None]]
1132
1133The reason is that replicating a list with ``*`` doesn't create copies, it only
1134creates references to the existing objects. The ``*3`` creates a list
1135containing 3 references to the same list of length two. Changes to one row will
1136show in all rows, which is almost certainly not what you want.
1137
1138The suggested approach is to create a list of the desired length first and then
1139fill in each element with a newly created list::
1140
1141 A = [None] * 3
1142 for i in range(3):
1143 A[i] = [None] * 2
1144
1145This generates a list containing 3 different lists of length two. You can also
1146use a list comprehension::
1147
1148 w, h = 2, 3
1149 A = [[None] * w for i in range(h)]
1150
1151Or, you can use an extension that provides a matrix datatype; `Numeric Python
Ezio Melottic1f58392013-06-09 01:04:21 +03001152<http://www.numpy.org/>`_ is the best known.
Georg Brandld7413152009-10-11 21:25:26 +00001153
1154
1155How do I apply a method to a sequence of objects?
1156-------------------------------------------------
1157
1158Use a list comprehension::
1159
Georg Brandl62eaaf62009-12-19 17:51:41 +00001160 result = [obj.method() for obj in mylist]
Georg Brandld7413152009-10-11 21:25:26 +00001161
Larry Hastings3732ed22014-03-15 21:13:56 -07001162.. _faq-augmented-assignment-tuple-error:
Georg Brandld7413152009-10-11 21:25:26 +00001163
R David Murraybcf06d32013-05-20 10:32:46 -04001164Why does a_tuple[i] += ['item'] raise an exception when the addition works?
1165---------------------------------------------------------------------------
1166
1167This is because of a combination of the fact that augmented assignment
1168operators are *assignment* operators, and the difference between mutable and
1169immutable objects in Python.
1170
1171This discussion applies in general when augmented assignment operators are
1172applied to elements of a tuple that point to mutable objects, but we'll use
1173a ``list`` and ``+=`` as our exemplar.
1174
1175If you wrote::
1176
1177 >>> a_tuple = (1, 2)
1178 >>> a_tuple[0] += 1
1179 Traceback (most recent call last):
1180 ...
1181 TypeError: 'tuple' object does not support item assignment
1182
1183The reason for the exception should be immediately clear: ``1`` is added to the
1184object ``a_tuple[0]`` points to (``1``), producing the result object, ``2``,
1185but when we attempt to assign the result of the computation, ``2``, to element
1186``0`` of the tuple, we get an error because we can't change what an element of
1187a tuple points to.
1188
1189Under the covers, what this augmented assignment statement is doing is
1190approximately this::
1191
R David Murray95ae9922013-05-21 11:44:41 -04001192 >>> result = a_tuple[0] + 1
R David Murraybcf06d32013-05-20 10:32:46 -04001193 >>> a_tuple[0] = result
1194 Traceback (most recent call last):
1195 ...
1196 TypeError: 'tuple' object does not support item assignment
1197
1198It is the assignment part of the operation that produces the error, since a
1199tuple is immutable.
1200
1201When you write something like::
1202
1203 >>> a_tuple = (['foo'], 'bar')
1204 >>> a_tuple[0] += ['item']
1205 Traceback (most recent call last):
1206 ...
1207 TypeError: 'tuple' object does not support item assignment
1208
1209The exception is a bit more surprising, and even more surprising is the fact
1210that even though there was an error, the append worked::
1211
1212 >>> a_tuple[0]
1213 ['foo', 'item']
1214
R David Murray95ae9922013-05-21 11:44:41 -04001215To see why this happens, you need to know that (a) if an object implements an
1216``__iadd__`` magic method, it gets called when the ``+=`` augmented assignment
1217is executed, and its return value is what gets used in the assignment statement;
1218and (b) for lists, ``__iadd__`` is equivalent to calling ``extend`` on the list
1219and returning the list. That's why we say that for lists, ``+=`` is a
1220"shorthand" for ``list.extend``::
R David Murraybcf06d32013-05-20 10:32:46 -04001221
1222 >>> a_list = []
1223 >>> a_list += [1]
1224 >>> a_list
1225 [1]
1226
R David Murray95ae9922013-05-21 11:44:41 -04001227This is equivalent to::
R David Murraybcf06d32013-05-20 10:32:46 -04001228
1229 >>> result = a_list.__iadd__([1])
1230 >>> a_list = result
1231
1232The object pointed to by a_list has been mutated, and the pointer to the
1233mutated object is assigned back to ``a_list``. The end result of the
1234assignment is a no-op, since it is a pointer to the same object that ``a_list``
1235was previously pointing to, but the assignment still happens.
1236
1237Thus, in our tuple example what is happening is equivalent to::
1238
1239 >>> result = a_tuple[0].__iadd__(['item'])
1240 >>> a_tuple[0] = result
1241 Traceback (most recent call last):
1242 ...
1243 TypeError: 'tuple' object does not support item assignment
1244
1245The ``__iadd__`` succeeds, and thus the list is extended, but even though
1246``result`` points to the same object that ``a_tuple[0]`` already points to,
1247that final assignment still results in an error, because tuples are immutable.
1248
1249
Georg Brandld7413152009-10-11 21:25:26 +00001250Dictionaries
1251============
1252
Benjamin Petersonb152e172013-11-26 23:05:25 -06001253How can I get a dictionary to store and display its keys in a consistent order?
1254-------------------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +00001255
Benjamin Petersonb152e172013-11-26 23:05:25 -06001256Use :class:`collections.OrderedDict`.
Georg Brandld7413152009-10-11 21:25:26 +00001257
1258I want to do a complicated sort: can you do a Schwartzian Transform in Python?
1259------------------------------------------------------------------------------
1260
1261The technique, attributed to Randal Schwartz of the Perl community, sorts the
1262elements of a list by a metric which maps each element to its "sort value". In
1263Python, just use the ``key`` argument for the ``sort()`` method::
1264
1265 Isorted = L[:]
1266 Isorted.sort(key=lambda s: int(s[10:15]))
1267
1268The ``key`` argument is new in Python 2.4, for older versions this kind of
1269sorting is quite simple to do with list comprehensions. To sort a list of
1270strings by their uppercase values::
1271
Georg Brandl62eaaf62009-12-19 17:51:41 +00001272 tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform
Georg Brandld7413152009-10-11 21:25:26 +00001273 tmp1.sort()
1274 Usorted = [x[1] for x in tmp1]
1275
1276To sort by the integer value of a subfield extending from positions 10-15 in
1277each string::
1278
Georg Brandl62eaaf62009-12-19 17:51:41 +00001279 tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform
Georg Brandld7413152009-10-11 21:25:26 +00001280 tmp2.sort()
1281 Isorted = [x[1] for x in tmp2]
1282
Georg Brandl62eaaf62009-12-19 17:51:41 +00001283For versions prior to 3.0, Isorted may also be computed by ::
Georg Brandld7413152009-10-11 21:25:26 +00001284
1285 def intfield(s):
1286 return int(s[10:15])
1287
1288 def Icmp(s1, s2):
1289 return cmp(intfield(s1), intfield(s2))
1290
1291 Isorted = L[:]
1292 Isorted.sort(Icmp)
1293
1294but since this method calls ``intfield()`` many times for each element of L, it
1295is slower than the Schwartzian Transform.
1296
1297
1298How can I sort one list by values from another list?
1299----------------------------------------------------
1300
Georg Brandl62eaaf62009-12-19 17:51:41 +00001301Merge them into an iterator of tuples, sort the resulting list, and then pick
Georg Brandld7413152009-10-11 21:25:26 +00001302out the element you want. ::
1303
1304 >>> list1 = ["what", "I'm", "sorting", "by"]
1305 >>> list2 = ["something", "else", "to", "sort"]
1306 >>> pairs = zip(list1, list2)
Georg Brandl62eaaf62009-12-19 17:51:41 +00001307 >>> pairs = sorted(pairs)
Georg Brandld7413152009-10-11 21:25:26 +00001308 >>> pairs
Georg Brandl62eaaf62009-12-19 17:51:41 +00001309 [("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]
1310 >>> result = [x[1] for x in pairs]
Georg Brandld7413152009-10-11 21:25:26 +00001311 >>> result
1312 ['else', 'sort', 'to', 'something']
1313
Georg Brandl62eaaf62009-12-19 17:51:41 +00001314
Georg Brandld7413152009-10-11 21:25:26 +00001315An alternative for the last step is::
1316
Georg Brandl62eaaf62009-12-19 17:51:41 +00001317 >>> result = []
1318 >>> for p in pairs: result.append(p[1])
Georg Brandld7413152009-10-11 21:25:26 +00001319
1320If you find this more legible, you might prefer to use this instead of the final
1321list comprehension. However, it is almost twice as slow for long lists. Why?
1322First, the ``append()`` operation has to reallocate memory, and while it uses
1323some tricks to avoid doing that each time, it still has to do it occasionally,
1324and that costs quite a bit. Second, the expression "result.append" requires an
1325extra attribute lookup, and third, there's a speed reduction from having to make
1326all those function calls.
1327
1328
1329Objects
1330=======
1331
1332What is a class?
1333----------------
1334
1335A class is the particular object type created by executing a class statement.
1336Class objects are used as templates to create instance objects, which embody
1337both the data (attributes) and code (methods) specific to a datatype.
1338
1339A class can be based on one or more other classes, called its base class(es). It
1340then inherits the attributes and methods of its base classes. This allows an
1341object model to be successively refined by inheritance. You might have a
1342generic ``Mailbox`` class that provides basic accessor methods for a mailbox,
1343and subclasses such as ``MboxMailbox``, ``MaildirMailbox``, ``OutlookMailbox``
1344that handle various specific mailbox formats.
1345
1346
1347What is a method?
1348-----------------
1349
1350A method is a function on some object ``x`` that you normally call as
1351``x.name(arguments...)``. Methods are defined as functions inside the class
1352definition::
1353
1354 class C:
1355 def meth (self, arg):
1356 return arg * 2 + self.attribute
1357
1358
1359What is self?
1360-------------
1361
1362Self is merely a conventional name for the first argument of a method. A method
1363defined as ``meth(self, a, b, c)`` should be called as ``x.meth(a, b, c)`` for
1364some instance ``x`` of the class in which the definition occurs; the called
1365method will think it is called as ``meth(x, a, b, c)``.
1366
1367See also :ref:`why-self`.
1368
1369
1370How do I check if an object is an instance of a given class or of a subclass of it?
1371-----------------------------------------------------------------------------------
1372
1373Use the built-in function ``isinstance(obj, cls)``. You can check if an object
1374is an instance of any of a number of classes by providing a tuple instead of a
1375single class, e.g. ``isinstance(obj, (class1, class2, ...))``, and can also
1376check whether an object is one of Python's built-in types, e.g.
Georg Brandl62eaaf62009-12-19 17:51:41 +00001377``isinstance(obj, str)`` or ``isinstance(obj, (int, float, complex))``.
Georg Brandld7413152009-10-11 21:25:26 +00001378
1379Note that most programs do not use :func:`isinstance` on user-defined classes
1380very often. If you are developing the classes yourself, a more proper
1381object-oriented style is to define methods on the classes that encapsulate a
1382particular behaviour, instead of checking the object's class and doing a
1383different thing based on what class it is. For example, if you have a function
1384that does something::
1385
Georg Brandl62eaaf62009-12-19 17:51:41 +00001386 def search(obj):
Georg Brandld7413152009-10-11 21:25:26 +00001387 if isinstance(obj, Mailbox):
1388 # ... code to search a mailbox
1389 elif isinstance(obj, Document):
1390 # ... code to search a document
1391 elif ...
1392
1393A better approach is to define a ``search()`` method on all the classes and just
1394call it::
1395
1396 class Mailbox:
1397 def search(self):
1398 # ... code to search a mailbox
1399
1400 class Document:
1401 def search(self):
1402 # ... code to search a document
1403
1404 obj.search()
1405
1406
1407What is delegation?
1408-------------------
1409
1410Delegation is an object oriented technique (also called a design pattern).
1411Let's say you have an object ``x`` and want to change the behaviour of just one
1412of its methods. You can create a new class that provides a new implementation
1413of the method you're interested in changing and delegates all other methods to
1414the corresponding method of ``x``.
1415
1416Python programmers can easily implement delegation. For example, the following
1417class implements a class that behaves like a file but converts all written data
1418to uppercase::
1419
1420 class UpperOut:
1421
1422 def __init__(self, outfile):
1423 self._outfile = outfile
1424
1425 def write(self, s):
1426 self._outfile.write(s.upper())
1427
1428 def __getattr__(self, name):
1429 return getattr(self._outfile, name)
1430
1431Here the ``UpperOut`` class redefines the ``write()`` method to convert the
1432argument string to uppercase before calling the underlying
1433``self.__outfile.write()`` method. All other methods are delegated to the
1434underlying ``self.__outfile`` object. The delegation is accomplished via the
1435``__getattr__`` method; consult :ref:`the language reference <attribute-access>`
1436for more information about controlling attribute access.
1437
1438Note that for more general cases delegation can get trickier. When attributes
1439must be set as well as retrieved, the class must define a :meth:`__setattr__`
1440method too, and it must do so carefully. The basic implementation of
1441:meth:`__setattr__` is roughly equivalent to the following::
1442
1443 class X:
1444 ...
1445 def __setattr__(self, name, value):
1446 self.__dict__[name] = value
1447 ...
1448
1449Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store
1450local state for self without causing an infinite recursion.
1451
1452
1453How do I call a method defined in a base class from a derived class that overrides it?
1454--------------------------------------------------------------------------------------
1455
Georg Brandl62eaaf62009-12-19 17:51:41 +00001456Use the built-in :func:`super` function::
Georg Brandld7413152009-10-11 21:25:26 +00001457
1458 class Derived(Base):
1459 def meth (self):
1460 super(Derived, self).meth()
1461
Georg Brandl62eaaf62009-12-19 17:51:41 +00001462For version prior to 3.0, you may be using classic classes: For a class
1463definition such as ``class Derived(Base): ...`` you can call method ``meth()``
1464defined in ``Base`` (or one of ``Base``'s base classes) as ``Base.meth(self,
1465arguments...)``. Here, ``Base.meth`` is an unbound method, so you need to
1466provide the ``self`` argument.
Georg Brandld7413152009-10-11 21:25:26 +00001467
1468
1469How can I organize my code to make it easier to change the base class?
1470----------------------------------------------------------------------
1471
1472You could define an alias for the base class, assign the real base class to it
1473before your class definition, and use the alias throughout your class. Then all
1474you have to change is the value assigned to the alias. Incidentally, this trick
1475is also handy if you want to decide dynamically (e.g. depending on availability
1476of resources) which base class to use. Example::
1477
1478 BaseAlias = <real base class>
1479
1480 class Derived(BaseAlias):
1481 def meth(self):
1482 BaseAlias.meth(self)
1483 ...
1484
1485
1486How do I create static class data and static class methods?
1487-----------------------------------------------------------
1488
Georg Brandl62eaaf62009-12-19 17:51:41 +00001489Both static data and static methods (in the sense of C++ or Java) are supported
1490in Python.
Georg Brandld7413152009-10-11 21:25:26 +00001491
1492For static data, simply define a class attribute. To assign a new value to the
1493attribute, you have to explicitly use the class name in the assignment::
1494
1495 class C:
1496 count = 0 # number of times C.__init__ called
1497
1498 def __init__(self):
1499 C.count = C.count + 1
1500
1501 def getcount(self):
1502 return C.count # or return self.count
1503
1504``c.count`` also refers to ``C.count`` for any ``c`` such that ``isinstance(c,
1505C)`` holds, unless overridden by ``c`` itself or by some class on the base-class
1506search path from ``c.__class__`` back to ``C``.
1507
1508Caution: within a method of C, an assignment like ``self.count = 42`` creates a
Georg Brandl62eaaf62009-12-19 17:51:41 +00001509new and unrelated instance named "count" in ``self``'s own dict. Rebinding of a
1510class-static data name must always specify the class whether inside a method or
1511not::
Georg Brandld7413152009-10-11 21:25:26 +00001512
1513 C.count = 314
1514
Antoine Pitrouf3520402011-12-03 22:19:55 +01001515Static methods are possible::
Georg Brandld7413152009-10-11 21:25:26 +00001516
1517 class C:
1518 @staticmethod
1519 def static(arg1, arg2, arg3):
1520 # No 'self' parameter!
1521 ...
1522
1523However, a far more straightforward way to get the effect of a static method is
1524via a simple module-level function::
1525
1526 def getcount():
1527 return C.count
1528
1529If your code is structured so as to define one class (or tightly related class
1530hierarchy) per module, this supplies the desired encapsulation.
1531
1532
1533How can I overload constructors (or methods) in Python?
1534-------------------------------------------------------
1535
1536This answer actually applies to all methods, but the question usually comes up
1537first in the context of constructors.
1538
1539In C++ you'd write
1540
1541.. code-block:: c
1542
1543 class C {
1544 C() { cout << "No arguments\n"; }
1545 C(int i) { cout << "Argument is " << i << "\n"; }
1546 }
1547
1548In Python you have to write a single constructor that catches all cases using
1549default arguments. For example::
1550
1551 class C:
1552 def __init__(self, i=None):
1553 if i is None:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001554 print("No arguments")
Georg Brandld7413152009-10-11 21:25:26 +00001555 else:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001556 print("Argument is", i)
Georg Brandld7413152009-10-11 21:25:26 +00001557
1558This is not entirely equivalent, but close enough in practice.
1559
1560You could also try a variable-length argument list, e.g. ::
1561
1562 def __init__(self, *args):
1563 ...
1564
1565The same approach works for all method definitions.
1566
1567
1568I try to use __spam and I get an error about _SomeClassName__spam.
1569------------------------------------------------------------------
1570
1571Variable names with double leading underscores are "mangled" to provide a simple
1572but effective way to define class private variables. Any identifier of the form
1573``__spam`` (at least two leading underscores, at most one trailing underscore)
1574is textually replaced with ``_classname__spam``, where ``classname`` is the
1575current class name with any leading underscores stripped.
1576
1577This doesn't guarantee privacy: an outside user can still deliberately access
1578the "_classname__spam" attribute, and private values are visible in the object's
1579``__dict__``. Many Python programmers never bother to use private variable
1580names at all.
1581
1582
1583My class defines __del__ but it is not called when I delete the object.
1584-----------------------------------------------------------------------
1585
1586There are several possible reasons for this.
1587
1588The del statement does not necessarily call :meth:`__del__` -- it simply
1589decrements the object's reference count, and if this reaches zero
1590:meth:`__del__` is called.
1591
1592If your data structures contain circular links (e.g. a tree where each child has
1593a parent reference and each parent has a list of children) the reference counts
1594will never go back to zero. Once in a while Python runs an algorithm to detect
1595such cycles, but the garbage collector might run some time after the last
1596reference to your data structure vanishes, so your :meth:`__del__` method may be
1597called at an inconvenient and random time. This is inconvenient if you're trying
1598to reproduce a problem. Worse, the order in which object's :meth:`__del__`
1599methods are executed is arbitrary. You can run :func:`gc.collect` to force a
1600collection, but there *are* pathological cases where objects will never be
1601collected.
1602
1603Despite the cycle collector, it's still a good idea to define an explicit
1604``close()`` method on objects to be called whenever you're done with them. The
1605``close()`` method can then remove attributes that refer to subobjecs. Don't
1606call :meth:`__del__` directly -- :meth:`__del__` should call ``close()`` and
1607``close()`` should make sure that it can be called more than once for the same
1608object.
1609
1610Another way to avoid cyclical references is to use the :mod:`weakref` module,
1611which allows you to point to objects without incrementing their reference count.
1612Tree data structures, for instance, should use weak references for their parent
1613and sibling references (if they need them!).
1614
Georg Brandl62eaaf62009-12-19 17:51:41 +00001615.. XXX relevant for Python 3?
1616
1617 If the object has ever been a local variable in a function that caught an
1618 expression in an except clause, chances are that a reference to the object
1619 still exists in that function's stack frame as contained in the stack trace.
1620 Normally, calling :func:`sys.exc_clear` will take care of this by clearing
1621 the last recorded exception.
Georg Brandld7413152009-10-11 21:25:26 +00001622
1623Finally, if your :meth:`__del__` method raises an exception, a warning message
1624is printed to :data:`sys.stderr`.
1625
1626
1627How do I get a list of all instances of a given class?
1628------------------------------------------------------
1629
1630Python does not keep track of all instances of a class (or of a built-in type).
1631You can program the class's constructor to keep track of all instances by
1632keeping a list of weak references to each instance.
1633
1634
Georg Brandld8ede4f2013-10-12 18:14:25 +02001635Why does the result of ``id()`` appear to be not unique?
1636--------------------------------------------------------
1637
1638The :func:`id` builtin returns an integer that is guaranteed to be unique during
1639the lifetime of the object. Since in CPython, this is the object's memory
1640address, it happens frequently that after an object is deleted from memory, the
1641next freshly created object is allocated at the same position in memory. This
1642is illustrated by this example:
1643
1644>>> id(1000)
164513901272
1646>>> id(2000)
164713901272
1648
1649The two ids belong to different integer objects that are created before, and
1650deleted immediately after execution of the ``id()`` call. To be sure that
1651objects whose id you want to examine are still alive, create another reference
1652to the object:
1653
1654>>> a = 1000; b = 2000
1655>>> id(a)
165613901272
1657>>> id(b)
165813891296
1659
1660
Georg Brandld7413152009-10-11 21:25:26 +00001661Modules
1662=======
1663
1664How do I create a .pyc file?
1665----------------------------
1666
R David Murrayd913d9d2013-12-13 12:29:29 -05001667When a module is imported for the first time (or when the source file has
1668changed since the current compiled file was created) a ``.pyc`` file containing
1669the compiled code should be created in a ``__pycache__`` subdirectory of the
1670directory containing the ``.py`` file. The ``.pyc`` file will have a
1671filename that starts with the same name as the ``.py`` file, and ends with
1672``.pyc``, with a middle component that depends on the particular ``python``
1673binary that created it. (See :pep:`3147` for details.)
Georg Brandld7413152009-10-11 21:25:26 +00001674
R David Murrayd913d9d2013-12-13 12:29:29 -05001675One reason that a ``.pyc`` file may not be created is a permissions problem
1676with the directory containing the source file, meaning that the ``__pycache__``
1677subdirectory cannot be created. This can happen, for example, if you develop as
1678one user but run as another, such as if you are testing with a web server.
1679
1680Unless the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable is set,
1681creation of a .pyc file is automatic if you're importing a module and Python
1682has the ability (permissions, free space, etc...) to create a ``__pycache__``
1683subdirectory and write the compiled module to that subdirectory.
Georg Brandld7413152009-10-11 21:25:26 +00001684
R David Murrayfdf95032013-06-19 16:58:26 -04001685Running Python on a top level script is not considered an import and no
1686``.pyc`` will be created. For example, if you have a top-level module
R David Murrayd913d9d2013-12-13 12:29:29 -05001687``foo.py`` that imports another module ``xyz.py``, when you run ``foo`` (by
1688typing ``python foo.py`` as a shell command), a ``.pyc`` will be created for
1689``xyz`` because ``xyz`` is imported, but no ``.pyc`` file will be created for
1690``foo`` since ``foo.py`` isn't being imported.
Georg Brandld7413152009-10-11 21:25:26 +00001691
R David Murrayd913d9d2013-12-13 12:29:29 -05001692If you need to create a ``.pyc`` file for ``foo`` -- that is, to create a
1693``.pyc`` file for a module that is not imported -- you can, using the
1694:mod:`py_compile` and :mod:`compileall` modules.
Georg Brandld7413152009-10-11 21:25:26 +00001695
1696The :mod:`py_compile` module can manually compile any module. One way is to use
1697the ``compile()`` function in that module interactively::
1698
1699 >>> import py_compile
R David Murrayfdf95032013-06-19 16:58:26 -04001700 >>> py_compile.compile('foo.py') # doctest: +SKIP
Georg Brandld7413152009-10-11 21:25:26 +00001701
R David Murrayd913d9d2013-12-13 12:29:29 -05001702This will write the ``.pyc`` to a ``__pycache__`` subdirectory in the same
1703location as ``foo.py`` (or you can override that with the optional parameter
1704``cfile``).
Georg Brandld7413152009-10-11 21:25:26 +00001705
1706You can also automatically compile all files in a directory or directories using
1707the :mod:`compileall` module. You can do it from the shell prompt by running
1708``compileall.py`` and providing the path of a directory containing Python files
1709to compile::
1710
1711 python -m compileall .
1712
1713
1714How do I find the current module name?
1715--------------------------------------
1716
1717A module can find out its own module name by looking at the predefined global
1718variable ``__name__``. If this has the value ``'__main__'``, the program is
1719running as a script. Many modules that are usually used by importing them also
1720provide a command-line interface or a self-test, and only execute this code
1721after checking ``__name__``::
1722
1723 def main():
Georg Brandl62eaaf62009-12-19 17:51:41 +00001724 print('Running test...')
Georg Brandld7413152009-10-11 21:25:26 +00001725 ...
1726
1727 if __name__ == '__main__':
1728 main()
1729
1730
1731How can I have modules that mutually import each other?
1732-------------------------------------------------------
1733
1734Suppose you have the following modules:
1735
1736foo.py::
1737
1738 from bar import bar_var
1739 foo_var = 1
1740
1741bar.py::
1742
1743 from foo import foo_var
1744 bar_var = 2
1745
1746The problem is that the interpreter will perform the following steps:
1747
1748* main imports foo
1749* Empty globals for foo are created
1750* foo is compiled and starts executing
1751* foo imports bar
1752* Empty globals for bar are created
1753* bar is compiled and starts executing
1754* bar imports foo (which is a no-op since there already is a module named foo)
1755* bar.foo_var = foo.foo_var
1756
1757The last step fails, because Python isn't done with interpreting ``foo`` yet and
1758the global symbol dictionary for ``foo`` is still empty.
1759
1760The same thing happens when you use ``import foo``, and then try to access
1761``foo.foo_var`` in global code.
1762
1763There are (at least) three possible workarounds for this problem.
1764
1765Guido van Rossum recommends avoiding all uses of ``from <module> import ...``,
1766and placing all code inside functions. Initializations of global variables and
1767class variables should use constants or built-in functions only. This means
1768everything from an imported module is referenced as ``<module>.<name>``.
1769
1770Jim Roskind suggests performing steps in the following order in each module:
1771
1772* exports (globals, functions, and classes that don't need imported base
1773 classes)
1774* ``import`` statements
1775* active code (including globals that are initialized from imported values).
1776
1777van Rossum doesn't like this approach much because the imports appear in a
1778strange place, but it does work.
1779
1780Matthias Urlichs recommends restructuring your code so that the recursive import
1781is not necessary in the first place.
1782
1783These solutions are not mutually exclusive.
1784
1785
1786__import__('x.y.z') returns <module 'x'>; how do I get z?
1787---------------------------------------------------------
1788
Ezio Melottie4aad5a2014-08-04 19:34:29 +03001789Consider using the convenience function :func:`~importlib.import_module` from
1790:mod:`importlib` instead::
Georg Brandld7413152009-10-11 21:25:26 +00001791
Ezio Melottie4aad5a2014-08-04 19:34:29 +03001792 z = importlib.import_module('x.y.z')
Georg Brandld7413152009-10-11 21:25:26 +00001793
1794
1795When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
1796-------------------------------------------------------------------------------------------------
1797
1798For reasons of efficiency as well as consistency, Python only reads the module
1799file on the first time a module is imported. If it didn't, in a program
1800consisting of many modules where each one imports the same basic module, the
Brett Cannon4f422e32013-06-14 22:49:00 -04001801basic module would be parsed and re-parsed many times. To force re-reading of a
Georg Brandld7413152009-10-11 21:25:26 +00001802changed module, do this::
1803
Brett Cannon4f422e32013-06-14 22:49:00 -04001804 import importlib
Georg Brandld7413152009-10-11 21:25:26 +00001805 import modname
Brett Cannon4f422e32013-06-14 22:49:00 -04001806 importlib.reload(modname)
Georg Brandld7413152009-10-11 21:25:26 +00001807
1808Warning: this technique is not 100% fool-proof. In particular, modules
1809containing statements like ::
1810
1811 from modname import some_objects
1812
1813will continue to work with the old version of the imported objects. If the
1814module contains class definitions, existing class instances will *not* be
1815updated to use the new class definition. This can result in the following
1816paradoxical behaviour:
1817
Brett Cannon4f422e32013-06-14 22:49:00 -04001818 >>> import importlib
Georg Brandld7413152009-10-11 21:25:26 +00001819 >>> import cls
1820 >>> c = cls.C() # Create an instance of C
Brett Cannon4f422e32013-06-14 22:49:00 -04001821 >>> importlib.reload(cls)
Georg Brandl62eaaf62009-12-19 17:51:41 +00001822 <module 'cls' from 'cls.py'>
Georg Brandld7413152009-10-11 21:25:26 +00001823 >>> isinstance(c, cls.C) # isinstance is false?!?
1824 False
1825
Georg Brandl62eaaf62009-12-19 17:51:41 +00001826The nature of the problem is made clear if you print out the "identity" of the
1827class objects:
Georg Brandld7413152009-10-11 21:25:26 +00001828
Georg Brandl62eaaf62009-12-19 17:51:41 +00001829 >>> hex(id(c.__class__))
1830 '0x7352a0'
1831 >>> hex(id(cls.C))
1832 '0x4198d0'