blob: 32123de16a50a6ebc700d9394910152f7e6e3350 [file] [log] [blame]
Georg Brandld7413152009-10-11 21:25:26 +00001:tocdepth: 2
2
3===============
4Programming FAQ
5===============
6
7.. contents::
8
9General Questions
10=================
11
12Is there a source code level debugger with breakpoints, single-stepping, etc.?
13------------------------------------------------------------------------------
14
15Yes.
16
17The pdb module is a simple but adequate console-mode debugger for Python. It is
18part of the standard Python library, and is :mod:`documented in the Library
19Reference Manual <pdb>`. You can also write your own debugger by using the code
20for pdb as an example.
21
22The IDLE interactive development environment, which is part of the standard
23Python distribution (normally available as Tools/scripts/idle), includes a
24graphical debugger. There is documentation for the IDLE debugger at
25http://www.python.org/idle/doc/idle2.html#Debugger.
26
27PythonWin is a Python IDE that includes a GUI debugger based on pdb. The
28Pythonwin debugger colors breakpoints and has quite a few cool features such as
29debugging non-Pythonwin programs. Pythonwin is available as part of the `Python
30for Windows Extensions <http://sourceforge.net/projects/pywin32/>`__ project and
31as a part of the ActivePython distribution (see
32http://www.activestate.com/Products/ActivePython/index.html).
33
34`Boa Constructor <http://boa-constructor.sourceforge.net/>`_ is an IDE and GUI
35builder that uses wxWidgets. It offers visual frame creation and manipulation,
36an object inspector, many views on the source like object browsers, inheritance
37hierarchies, doc string generated html documentation, an advanced debugger,
38integrated help, and Zope support.
39
40`Eric <http://www.die-offenbachs.de/eric/index.html>`_ is an IDE built on PyQt
41and the Scintilla editing component.
42
43Pydb is a version of the standard Python debugger pdb, modified for use with DDD
44(Data Display Debugger), a popular graphical debugger front end. Pydb can be
45found at http://bashdb.sourceforge.net/pydb/ and DDD can be found at
46http://www.gnu.org/software/ddd.
47
48There are a number of commercial Python IDEs that include graphical debuggers.
49They include:
50
51* Wing IDE (http://wingware.com/)
52* Komodo IDE (http://www.activestate.com/Products/Komodo)
53
54
55Is there a tool to help find bugs or perform static analysis?
56-------------------------------------------------------------
57
58Yes.
59
60PyChecker is a static analysis tool that finds bugs in Python source code and
61warns about code complexity and style. You can get PyChecker from
62http://pychecker.sf.net.
63
64`Pylint <http://www.logilab.org/projects/pylint>`_ is another tool that checks
65if a module satisfies a coding standard, and also makes it possible to write
66plug-ins to add a custom feature. In addition to the bug checking that
67PyChecker performs, Pylint offers some additional features such as checking line
68length, whether variable names are well-formed according to your coding
69standard, whether declared interfaces are fully implemented, and more.
Georg Brandl495f7b52009-10-27 15:28:25 +000070http://www.logilab.org/card/pylint_manual provides a full list of Pylint's
71features.
Georg Brandld7413152009-10-11 21:25:26 +000072
73
74How can I create a stand-alone binary from a Python script?
75-----------------------------------------------------------
76
77You don't need the ability to compile Python to C code if all you want is a
78stand-alone program that users can download and run without having to install
79the Python distribution first. There are a number of tools that determine the
80set of modules required by a program and bind these modules together with a
81Python binary to produce a single executable.
82
83One is to use the freeze tool, which is included in the Python source tree as
84``Tools/freeze``. It converts Python byte code to C arrays; a C compiler you can
85embed all your modules into a new program, which is then linked with the
86standard Python modules.
87
88It works by scanning your source recursively for import statements (in both
89forms) and looking for the modules in the standard Python path as well as in the
90source directory (for built-in modules). It then turns the bytecode for modules
91written in Python into C code (array initializers that can be turned into code
92objects using the marshal module) and creates a custom-made config file that
93only contains those built-in modules which are actually used in the program. It
94then compiles the generated C code and links it with the rest of the Python
95interpreter to form a self-contained binary which acts exactly like your script.
96
97Obviously, freeze requires a C compiler. There are several other utilities
98which don't. One is Thomas Heller's py2exe (Windows only) at
99
100 http://www.py2exe.org/
101
102Another is Christian Tismer's `SQFREEZE <http://starship.python.net/crew/pirx>`_
103which appends the byte code to a specially-prepared Python interpreter that can
104find the byte code in the executable.
105
106Other tools include Fredrik Lundh's `Squeeze
107<http://www.pythonware.com/products/python/squeeze>`_ and Anthony Tuininga's
108`cx_Freeze <http://starship.python.net/crew/atuining/cx_Freeze/index.html>`_.
109
110
111Are there coding standards or a style guide for Python programs?
112----------------------------------------------------------------
113
114Yes. The coding style required for standard library modules is documented as
115:pep:`8`.
116
117
Georg Brandld7413152009-10-11 21:25:26 +0000118Core Language
119=============
120
R. David Murrayc04a6942009-11-14 22:21:32 +0000121Why am I getting an UnboundLocalError when the variable has a value?
122--------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000123
R. David Murrayc04a6942009-11-14 22:21:32 +0000124It can be a surprise to get the UnboundLocalError in previously working
125code when it is modified by adding an assignment statement somewhere in
126the body of a function.
Georg Brandld7413152009-10-11 21:25:26 +0000127
R. David Murrayc04a6942009-11-14 22:21:32 +0000128This code:
Georg Brandld7413152009-10-11 21:25:26 +0000129
R. David Murrayc04a6942009-11-14 22:21:32 +0000130 >>> x = 10
131 >>> def bar():
132 ... print(x)
133 >>> bar()
134 10
Georg Brandld7413152009-10-11 21:25:26 +0000135
R. David Murrayc04a6942009-11-14 22:21:32 +0000136works, but this code:
Georg Brandld7413152009-10-11 21:25:26 +0000137
R. David Murrayc04a6942009-11-14 22:21:32 +0000138 >>> x = 10
139 >>> def foo():
140 ... print(x)
141 ... x += 1
Georg Brandld7413152009-10-11 21:25:26 +0000142
R. David Murrayc04a6942009-11-14 22:21:32 +0000143results in an UnboundLocalError:
Georg Brandld7413152009-10-11 21:25:26 +0000144
R. David Murrayc04a6942009-11-14 22:21:32 +0000145 >>> foo()
146 Traceback (most recent call last):
147 ...
148 UnboundLocalError: local variable 'x' referenced before assignment
149
150This is because when you make an assignment to a variable in a scope, that
151variable becomes local to that scope and shadows any similarly named variable
152in the outer scope. Since the last statement in foo assigns a new value to
153``x``, the compiler recognizes it as a local variable. Consequently when the
R. David Murray18163c32009-11-14 22:27:22 +0000154earlier ``print(x)`` attempts to print the uninitialized local variable and
R. David Murrayc04a6942009-11-14 22:21:32 +0000155an error results.
156
157In the example above you can access the outer scope variable by declaring it
158global:
159
160 >>> x = 10
161 >>> def foobar():
162 ... global x
163 ... print(x)
164 ... x += 1
165 >>> foobar()
166 10
167
168This explicit declaration is required in order to remind you that (unlike the
169superficially analogous situation with class and instance variables) you are
170actually modifying the value of the variable in the outer scope:
171
172 >>> print(x)
173 11
174
175You can do a similar thing in a nested scope using the :keyword:`nonlocal`
176keyword:
177
178 >>> def foo():
179 ... x = 10
180 ... def bar():
181 ... nonlocal x
182 ... print(x)
183 ... x += 1
184 ... bar()
185 ... print(x)
186 >>> foo()
187 10
188 11
Georg Brandld7413152009-10-11 21:25:26 +0000189
190
191What are the rules for local and global variables in Python?
192------------------------------------------------------------
193
194In Python, variables that are only referenced inside a function are implicitly
195global. If a variable is assigned a new value anywhere within the function's
196body, it's assumed to be a local. If a variable is ever assigned a new value
197inside the function, the variable is implicitly local, and you need to
198explicitly declare it as 'global'.
199
200Though a bit surprising at first, a moment's consideration explains this. On
201one hand, requiring :keyword:`global` for assigned variables provides a bar
202against unintended side-effects. On the other hand, if ``global`` was required
203for all global references, you'd be using ``global`` all the time. You'd have
Georg Brandlc4a55fc2010-02-06 18:46:57 +0000204to declare as global every reference to a built-in function or to a component of
Georg Brandld7413152009-10-11 21:25:26 +0000205an imported module. This clutter would defeat the usefulness of the ``global``
206declaration for identifying side-effects.
207
208
Ezio Melotticad8b0f2013-01-05 00:50:46 +0200209Why do lambdas defined in a loop with different values all return the same result?
210----------------------------------------------------------------------------------
211
212Assume you use a for loop to define a few different lambdas (or even plain
213functions), e.g.::
214
215 squares = []
216 for x in range(5):
217 squares.append(lambda: x**2)
218
219This gives you a list that contains 5 lambdas that calculate ``x**2``. You
220might expect that, when called, they would return, respectively, ``0``, ``1``,
221``4``, ``9``, and ``16``. However, when you actually try you will see that
222they all return ``16``::
223
224 >>> squares[2]()
225 16
226 >>> squares[4]()
227 16
228
229This happens because ``x`` is not local to the lambdas, but is defined in
230the outer scope, and it is accessed when the lambda is called --- not when it
231is defined. At the end of the loop, the value of ``x`` is ``4``, so all the
232functions now return ``4**2``, i.e. ``16``. You can also verify this by
233changing the value of ``x`` and see how the results of the lambdas change::
234
235 >>> x = 8
236 >>> squares[2]()
237 64
238
239In order to avoid this, you need to save the values in variables local to the
240lambdas, so that they don't rely on the value of the global ``x``::
241
242 squares = []
243 for x in range(5):
244 squares.append(lambda n=x: n**2)
245
246Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed
247when the lambda is defined so that it has the same value that ``x`` had at
248that point in the loop. This means that the value of ``n`` will be ``0``
249in the first lambda, ``1`` in the second, ``2`` in the third, and so on.
250Therefore each lambda will now return the correct result::
251
252 >>> squares[2]()
253 4
254 >>> squares[4]()
255 16
256
257Note that this behaviour is not peculiar to lambdas, but applies to regular
258functions too.
259
260
Georg Brandld7413152009-10-11 21:25:26 +0000261How do I share global variables across modules?
262------------------------------------------------
263
264The canonical way to share information across modules within a single program is
265to create a special module (often called config or cfg). Just import the config
266module in all modules of your application; the module then becomes available as
267a global name. Because there is only one instance of each module, any changes
268made to the module object get reflected everywhere. For example:
269
270config.py::
271
272 x = 0 # Default value of the 'x' configuration setting
273
274mod.py::
275
276 import config
277 config.x = 1
278
279main.py::
280
281 import config
282 import mod
Georg Brandl62eaaf62009-12-19 17:51:41 +0000283 print(config.x)
Georg Brandld7413152009-10-11 21:25:26 +0000284
285Note that using a module is also the basis for implementing the Singleton design
286pattern, for the same reason.
287
288
289What are the "best practices" for using import in a module?
290-----------------------------------------------------------
291
292In general, don't use ``from modulename import *``. Doing so clutters the
293importer's namespace. Some people avoid this idiom even with the few modules
294that were designed to be imported in this manner. Modules designed in this
Georg Brandld404fa62009-10-13 16:55:12 +0000295manner include :mod:`tkinter`, and :mod:`threading`.
Georg Brandld7413152009-10-11 21:25:26 +0000296
297Import modules at the top of a file. Doing so makes it clear what other modules
298your code requires and avoids questions of whether the module name is in scope.
299Using one import per line makes it easy to add and delete module imports, but
300using multiple imports per line uses less screen space.
301
302It's good practice if you import modules in the following order:
303
Georg Brandl62eaaf62009-12-19 17:51:41 +00003041. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re``
Georg Brandld7413152009-10-11 21:25:26 +00003052. third-party library modules (anything installed in Python's site-packages
306 directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc.
3073. locally-developed modules
308
309Never use relative package imports. If you're writing code that's in the
310``package.sub.m1`` module and want to import ``package.sub.m2``, do not just
Georg Brandl11b63622009-12-20 14:21:27 +0000311write ``from . import m2``, even though it's legal. Write ``from package.sub
312import m2`` instead. See :pep:`328` for details.
Georg Brandld7413152009-10-11 21:25:26 +0000313
314It is sometimes necessary to move imports to a function or class to avoid
315problems with circular imports. Gordon McMillan says:
316
317 Circular imports are fine where both modules use the "import <module>" form
318 of import. They fail when the 2nd module wants to grab a name out of the
319 first ("from module import name") and the import is at the top level. That's
320 because names in the 1st are not yet available, because the first module is
321 busy importing the 2nd.
322
323In this case, if the second module is only used in one function, then the import
324can easily be moved into that function. By the time the import is called, the
325first module will have finished initializing, and the second module can do its
326import.
327
328It may also be necessary to move imports out of the top level of code if some of
329the modules are platform-specific. In that case, it may not even be possible to
330import all of the modules at the top of the file. In this case, importing the
331correct modules in the corresponding platform-specific code is a good option.
332
333Only move imports into a local scope, such as inside a function definition, if
334it's necessary to solve a problem such as avoiding a circular import or are
335trying to reduce the initialization time of a module. This technique is
336especially helpful if many of the imports are unnecessary depending on how the
337program executes. You may also want to move imports into a function if the
338modules are only ever used in that function. Note that loading a module the
339first time may be expensive because of the one time initialization of the
340module, but loading a module multiple times is virtually free, costing only a
341couple of dictionary lookups. Even if the module name has gone out of scope,
342the module is probably available in :data:`sys.modules`.
343
344If only instances of a specific class use a module, then it is reasonable to
345import the module in the class's ``__init__`` method and then assign the module
346to an instance variable so that the module is always available (via that
347instance variable) during the life of the object. Note that to delay an import
348until the class is instantiated, the import must be inside a method. Putting
349the import inside the class but outside of any method still causes the import to
350occur when the module is initialized.
351
352
353How can I pass optional or keyword parameters from one function to another?
354---------------------------------------------------------------------------
355
356Collect the arguments using the ``*`` and ``**`` specifiers in the function's
357parameter list; this gives you the positional arguments as a tuple and the
358keyword arguments as a dictionary. You can then pass these arguments when
359calling another function by using ``*`` and ``**``::
360
361 def f(x, *args, **kwargs):
362 ...
363 kwargs['width'] = '14.3c'
364 ...
365 g(x, *args, **kwargs)
366
Georg Brandld7413152009-10-11 21:25:26 +0000367
Chris Jerdonekb4309942012-12-25 14:54:44 -0800368.. index::
369 single: argument; difference from parameter
370 single: parameter; difference from argument
371
Chris Jerdonekc2a7fd62012-11-28 02:29:33 -0800372.. _faq-argument-vs-parameter:
373
374What is the difference between arguments and parameters?
375--------------------------------------------------------
376
377:term:`Parameters <parameter>` are defined by the names that appear in a
378function definition, whereas :term:`arguments <argument>` are the values
379actually passed to a function when calling it. Parameters define what types of
380arguments a function can accept. For example, given the function definition::
381
382 def func(foo, bar=None, **kwargs):
383 pass
384
385*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling
386``func``, for example::
387
388 func(42, bar=314, extra=somevar)
389
390the values ``42``, ``314``, and ``somevar`` are arguments.
391
392
Georg Brandld7413152009-10-11 21:25:26 +0000393How do I write a function with output parameters (call by reference)?
394---------------------------------------------------------------------
395
396Remember that arguments are passed by assignment in Python. Since assignment
397just creates references to objects, there's no alias between an argument name in
398the caller and callee, and so no call-by-reference per se. You can achieve the
399desired effect in a number of ways.
400
4011) By returning a tuple of the results::
402
403 def func2(a, b):
404 a = 'new-value' # a and b are local names
405 b = b + 1 # assigned to new objects
406 return a, b # return new values
407
408 x, y = 'old-value', 99
409 x, y = func2(x, y)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000410 print(x, y) # output: new-value 100
Georg Brandld7413152009-10-11 21:25:26 +0000411
412 This is almost always the clearest solution.
413
4142) By using global variables. This isn't thread-safe, and is not recommended.
415
4163) By passing a mutable (changeable in-place) object::
417
418 def func1(a):
419 a[0] = 'new-value' # 'a' references a mutable list
420 a[1] = a[1] + 1 # changes a shared object
421
422 args = ['old-value', 99]
423 func1(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000424 print(args[0], args[1]) # output: new-value 100
Georg Brandld7413152009-10-11 21:25:26 +0000425
4264) By passing in a dictionary that gets mutated::
427
428 def func3(args):
429 args['a'] = 'new-value' # args is a mutable dictionary
430 args['b'] = args['b'] + 1 # change it in-place
431
432 args = {'a':' old-value', 'b': 99}
433 func3(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000434 print(args['a'], args['b'])
Georg Brandld7413152009-10-11 21:25:26 +0000435
4365) Or bundle up values in a class instance::
437
438 class callByRef:
439 def __init__(self, **args):
440 for (key, value) in args.items():
441 setattr(self, key, value)
442
443 def func4(args):
444 args.a = 'new-value' # args is a mutable callByRef
445 args.b = args.b + 1 # change object in-place
446
447 args = callByRef(a='old-value', b=99)
448 func4(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000449 print(args.a, args.b)
Georg Brandld7413152009-10-11 21:25:26 +0000450
451
452 There's almost never a good reason to get this complicated.
453
454Your best choice is to return a tuple containing the multiple results.
455
456
457How do you make a higher order function in Python?
458--------------------------------------------------
459
460You have two choices: you can use nested scopes or you can use callable objects.
461For example, suppose you wanted to define ``linear(a,b)`` which returns a
462function ``f(x)`` that computes the value ``a*x+b``. Using nested scopes::
463
464 def linear(a, b):
465 def result(x):
466 return a * x + b
467 return result
468
469Or using a callable object::
470
471 class linear:
472
473 def __init__(self, a, b):
474 self.a, self.b = a, b
475
476 def __call__(self, x):
477 return self.a * x + self.b
478
479In both cases, ::
480
481 taxes = linear(0.3, 2)
482
483gives a callable object where ``taxes(10e6) == 0.3 * 10e6 + 2``.
484
485The callable object approach has the disadvantage that it is a bit slower and
486results in slightly longer code. However, note that a collection of callables
487can share their signature via inheritance::
488
489 class exponential(linear):
490 # __init__ inherited
491 def __call__(self, x):
492 return self.a * (x ** self.b)
493
494Object can encapsulate state for several methods::
495
496 class counter:
497
498 value = 0
499
500 def set(self, x):
501 self.value = x
502
503 def up(self):
504 self.value = self.value + 1
505
506 def down(self):
507 self.value = self.value - 1
508
509 count = counter()
510 inc, dec, reset = count.up, count.down, count.set
511
512Here ``inc()``, ``dec()`` and ``reset()`` act like functions which share the
513same counting variable.
514
515
516How do I copy an object in Python?
517----------------------------------
518
519In general, try :func:`copy.copy` or :func:`copy.deepcopy` for the general case.
520Not all objects can be copied, but most can.
521
522Some objects can be copied more easily. Dictionaries have a :meth:`~dict.copy`
523method::
524
525 newdict = olddict.copy()
526
527Sequences can be copied by slicing::
528
529 new_l = l[:]
530
531
532How can I find the methods or attributes of an object?
533------------------------------------------------------
534
535For an instance x of a user-defined class, ``dir(x)`` returns an alphabetized
536list of the names containing the instance attributes and methods and attributes
537defined by its class.
538
539
540How can my code discover the name of an object?
541-----------------------------------------------
542
543Generally speaking, it can't, because objects don't really have names.
544Essentially, assignment always binds a name to a value; The same is true of
545``def`` and ``class`` statements, but in that case the value is a
546callable. Consider the following code::
547
548 class A:
549 pass
550
551 B = A
552
553 a = B()
554 b = a
Georg Brandl62eaaf62009-12-19 17:51:41 +0000555 print(b)
556 <__main__.A object at 0x16D07CC>
557 print(a)
558 <__main__.A object at 0x16D07CC>
Georg Brandld7413152009-10-11 21:25:26 +0000559
560Arguably the class has a name: even though it is bound to two names and invoked
561through the name B the created instance is still reported as an instance of
562class A. However, it is impossible to say whether the instance's name is a or
563b, since both names are bound to the same value.
564
565Generally speaking it should not be necessary for your code to "know the names"
566of particular values. Unless you are deliberately writing introspective
567programs, this is usually an indication that a change of approach might be
568beneficial.
569
570In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to
571this question:
572
573 The same way as you get the name of that cat you found on your porch: the cat
574 (object) itself cannot tell you its name, and it doesn't really care -- so
575 the only way to find out what it's called is to ask all your neighbours
576 (namespaces) if it's their cat (object)...
577
578 ....and don't be surprised if you'll find that it's known by many names, or
579 no name at all!
580
581
582What's up with the comma operator's precedence?
583-----------------------------------------------
584
585Comma is not an operator in Python. Consider this session::
586
587 >>> "a" in "b", "a"
Georg Brandl62eaaf62009-12-19 17:51:41 +0000588 (False, 'a')
Georg Brandld7413152009-10-11 21:25:26 +0000589
590Since the comma is not an operator, but a separator between expressions the
591above is evaluated as if you had entered::
592
593 >>> ("a" in "b"), "a"
594
595not::
596
Georg Brandl62eaaf62009-12-19 17:51:41 +0000597 >>> "a" in ("b", "a")
Georg Brandld7413152009-10-11 21:25:26 +0000598
599The same is true of the various assignment operators (``=``, ``+=`` etc). They
600are not truly operators but syntactic delimiters in assignment statements.
601
602
603Is there an equivalent of C's "?:" ternary operator?
604----------------------------------------------------
605
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100606Yes, there is. The syntax is as follows::
Georg Brandld7413152009-10-11 21:25:26 +0000607
608 [on_true] if [expression] else [on_false]
609
610 x, y = 50, 25
Georg Brandld7413152009-10-11 21:25:26 +0000611 small = x if x < y else y
612
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100613Before this syntax was introduced in Python 2.5, a common idiom was to use
614logical operators::
Georg Brandld7413152009-10-11 21:25:26 +0000615
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100616 [expression] and [on_true] or [on_false]
Georg Brandld7413152009-10-11 21:25:26 +0000617
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100618However, this idiom is unsafe, as it can give wrong results when *on_true*
619has a false boolean value. Therefore, it is always better to use
620the ``... if ... else ...`` form.
Georg Brandld7413152009-10-11 21:25:26 +0000621
622
623Is it possible to write obfuscated one-liners in Python?
624--------------------------------------------------------
625
626Yes. Usually this is done by nesting :keyword:`lambda` within
627:keyword:`lambda`. See the following three examples, due to Ulf Bartelt::
628
Georg Brandl62eaaf62009-12-19 17:51:41 +0000629 from functools import reduce
630
Georg Brandld7413152009-10-11 21:25:26 +0000631 # Primes < 1000
Georg Brandl62eaaf62009-12-19 17:51:41 +0000632 print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
633 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 +0000634
635 # First 10 Fibonacci numbers
Georg Brandl62eaaf62009-12-19 17:51:41 +0000636 print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1:
637 f(x,f), range(10))))
Georg Brandld7413152009-10-11 21:25:26 +0000638
639 # Mandelbrot set
Georg Brandl62eaaf62009-12-19 17:51:41 +0000640 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 +0000641 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
642 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
643 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
644 >=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(
645 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 +0000646 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24))
Georg Brandld7413152009-10-11 21:25:26 +0000647 # \___ ___/ \___ ___/ | | |__ lines on screen
648 # V V | |______ columns on screen
649 # | | |__________ maximum of "iterations"
650 # | |_________________ range on y axis
651 # |____________________________ range on x axis
652
653Don't try this at home, kids!
654
655
656Numbers and strings
657===================
658
659How do I specify hexadecimal and octal integers?
660------------------------------------------------
661
Georg Brandl62eaaf62009-12-19 17:51:41 +0000662To specify an octal digit, precede the octal value with a zero, and then a lower
663or uppercase "o". For example, to set the variable "a" to the octal value "10"
664(8 in decimal), type::
Georg Brandld7413152009-10-11 21:25:26 +0000665
Georg Brandl62eaaf62009-12-19 17:51:41 +0000666 >>> a = 0o10
Georg Brandld7413152009-10-11 21:25:26 +0000667 >>> a
668 8
669
670Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,
671and then a lower or uppercase "x". Hexadecimal digits can be specified in lower
672or uppercase. For example, in the Python interpreter::
673
674 >>> a = 0xa5
675 >>> a
676 165
677 >>> b = 0XB2
678 >>> b
679 178
680
681
Georg Brandl62eaaf62009-12-19 17:51:41 +0000682Why does -22 // 10 return -3?
683-----------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000684
685It's primarily driven by the desire that ``i % j`` have the same sign as ``j``.
686If you want that, and also want::
687
Georg Brandl62eaaf62009-12-19 17:51:41 +0000688 i == (i // j) * j + (i % j)
Georg Brandld7413152009-10-11 21:25:26 +0000689
690then integer division has to return the floor. C also requires that identity to
Georg Brandl62eaaf62009-12-19 17:51:41 +0000691hold, and then compilers that truncate ``i // j`` need to make ``i % j`` have
692the same sign as ``i``.
Georg Brandld7413152009-10-11 21:25:26 +0000693
694There are few real use cases for ``i % j`` when ``j`` is negative. When ``j``
695is positive, there are many, and in virtually all of them it's more useful for
696``i % j`` to be ``>= 0``. If the clock says 10 now, what did it say 200 hours
697ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to
698bite.
699
700
701How do I convert a string to a number?
702--------------------------------------
703
704For integers, use the built-in :func:`int` type constructor, e.g. ``int('144')
705== 144``. Similarly, :func:`float` converts to floating-point,
706e.g. ``float('144') == 144.0``.
707
708By default, these interpret the number as decimal, so that ``int('0144') ==
709144`` and ``int('0x144')`` raises :exc:`ValueError`. ``int(string, base)`` takes
710the base to convert from as a second optional argument, so ``int('0x144', 16) ==
711324``. If the base is specified as 0, the number is interpreted using Python's
712rules: a leading '0' indicates octal, and '0x' indicates a hex number.
713
714Do not use the built-in function :func:`eval` if all you need is to convert
715strings to numbers. :func:`eval` will be significantly slower and it presents a
716security risk: someone could pass you a Python expression that might have
717unwanted side effects. For example, someone could pass
718``__import__('os').system("rm -rf $HOME")`` which would erase your home
719directory.
720
721:func:`eval` also has the effect of interpreting numbers as Python expressions,
Georg Brandl62eaaf62009-12-19 17:51:41 +0000722so that e.g. ``eval('09')`` gives a syntax error because Python does not allow
723leading '0' in a decimal number (except '0').
Georg Brandld7413152009-10-11 21:25:26 +0000724
725
726How do I convert a number to a string?
727--------------------------------------
728
729To convert, e.g., the number 144 to the string '144', use the built-in type
730constructor :func:`str`. If you want a hexadecimal or octal representation, use
Georg Brandl62eaaf62009-12-19 17:51:41 +0000731the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see
732the :ref:`string-formatting` section, e.g. ``"{:04d}".format(144)`` yields
Georg Brandl11b63622009-12-20 14:21:27 +0000733``'0144'`` and ``"{:.3f}".format(1/3)`` yields ``'0.333'``.
Georg Brandld7413152009-10-11 21:25:26 +0000734
735
736How do I modify a string in place?
737----------------------------------
738
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100739You can't, because strings are immutable. In most situations, you should
740simply construct a new string from the various parts you want to assemble
741it from. However, if you need an object with the ability to modify in-place
742unicode data, try using a :class:`io.StringIO` object or the :mod:`array`
743module::
Georg Brandld7413152009-10-11 21:25:26 +0000744
745 >>> s = "Hello, world"
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100746 >>> sio = io.StringIO(s)
747 >>> sio.getvalue()
748 'Hello, world'
749 >>> sio.seek(7)
750 7
751 >>> sio.write("there!")
752 6
753 >>> sio.getvalue()
Georg Brandld7413152009-10-11 21:25:26 +0000754 'Hello, there!'
755
756 >>> import array
Georg Brandl62eaaf62009-12-19 17:51:41 +0000757 >>> a = array.array('u', s)
758 >>> print(a)
759 array('u', 'Hello, world')
760 >>> a[0] = 'y'
761 >>> print(a)
762 array('u', 'yello world')
763 >>> a.tounicode()
Georg Brandld7413152009-10-11 21:25:26 +0000764 'yello, world'
765
766
767How do I use strings to call functions/methods?
768-----------------------------------------------
769
770There are various techniques.
771
772* The best is to use a dictionary that maps strings to functions. The primary
773 advantage of this technique is that the strings do not need to match the names
774 of the functions. This is also the primary technique used to emulate a case
775 construct::
776
777 def a():
778 pass
779
780 def b():
781 pass
782
783 dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
784
785 dispatch[get_input()]() # Note trailing parens to call function
786
787* Use the built-in function :func:`getattr`::
788
789 import foo
790 getattr(foo, 'bar')()
791
792 Note that :func:`getattr` works on any object, including classes, class
793 instances, modules, and so on.
794
795 This is used in several places in the standard library, like this::
796
797 class Foo:
798 def do_foo(self):
799 ...
800
801 def do_bar(self):
802 ...
803
804 f = getattr(foo_instance, 'do_' + opname)
805 f()
806
807
808* Use :func:`locals` or :func:`eval` to resolve the function name::
809
810 def myFunc():
Georg Brandl62eaaf62009-12-19 17:51:41 +0000811 print("hello")
Georg Brandld7413152009-10-11 21:25:26 +0000812
813 fname = "myFunc"
814
815 f = locals()[fname]
816 f()
817
818 f = eval(fname)
819 f()
820
821 Note: Using :func:`eval` is slow and dangerous. If you don't have absolute
822 control over the contents of the string, someone could pass a string that
823 resulted in an arbitrary function being executed.
824
825Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
826-------------------------------------------------------------------------------------
827
Antoine Pitrouf3520402011-12-03 22:19:55 +0100828You can use ``S.rstrip("\r\n")`` to remove all occurrences of any line
829terminator from the end of the string ``S`` without removing other trailing
830whitespace. If the string ``S`` represents more than one line, with several
831empty lines at the end, the line terminators for all the blank lines will
832be removed::
Georg Brandld7413152009-10-11 21:25:26 +0000833
834 >>> lines = ("line 1 \r\n"
835 ... "\r\n"
836 ... "\r\n")
837 >>> lines.rstrip("\n\r")
Georg Brandl62eaaf62009-12-19 17:51:41 +0000838 'line 1 '
Georg Brandld7413152009-10-11 21:25:26 +0000839
840Since this is typically only desired when reading text one line at a time, using
841``S.rstrip()`` this way works well.
842
Georg Brandld7413152009-10-11 21:25:26 +0000843
844Is there a scanf() or sscanf() equivalent?
845------------------------------------------
846
847Not as such.
848
849For simple input parsing, the easiest approach is usually to split the line into
850whitespace-delimited words using the :meth:`~str.split` method of string objects
851and then convert decimal strings to numeric values using :func:`int` or
852:func:`float`. ``split()`` supports an optional "sep" parameter which is useful
853if the line uses something other than whitespace as a separator.
854
Brian Curtin5a7a52f2010-09-23 13:45:21 +0000855For more complicated input parsing, regular expressions are more powerful
Georg Brandl60203b42010-10-06 10:11:56 +0000856than C's :c:func:`sscanf` and better suited for the task.
Georg Brandld7413152009-10-11 21:25:26 +0000857
858
Georg Brandl62eaaf62009-12-19 17:51:41 +0000859What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?
860-------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000861
Georg Brandl62eaaf62009-12-19 17:51:41 +0000862See the :ref:`unicode-howto`.
Georg Brandld7413152009-10-11 21:25:26 +0000863
864
Antoine Pitrou432259f2011-12-09 23:10:31 +0100865Performance
866===========
867
868My program is too slow. How do I speed it up?
869---------------------------------------------
870
871That's a tough one, in general. First, here are a list of things to
872remember before diving further:
873
Georg Brandl300a6912012-03-14 22:40:08 +0100874* Performance characteristics vary across Python implementations. This FAQ
Antoine Pitrou432259f2011-12-09 23:10:31 +0100875 focusses on :term:`CPython`.
Georg Brandl300a6912012-03-14 22:40:08 +0100876* Behaviour can vary across operating systems, especially when talking about
Antoine Pitrou432259f2011-12-09 23:10:31 +0100877 I/O or multi-threading.
878* You should always find the hot spots in your program *before* attempting to
879 optimize any code (see the :mod:`profile` module).
880* Writing benchmark scripts will allow you to iterate quickly when searching
881 for improvements (see the :mod:`timeit` module).
882* It is highly recommended to have good code coverage (through unit testing
883 or any other technique) before potentially introducing regressions hidden
884 in sophisticated optimizations.
885
886That being said, there are many tricks to speed up Python code. Here are
887some general principles which go a long way towards reaching acceptable
888performance levels:
889
890* Making your algorithms faster (or changing to faster ones) can yield
891 much larger benefits than trying to sprinkle micro-optimization tricks
892 all over your code.
893
894* Use the right data structures. Study documentation for the :ref:`bltin-types`
895 and the :mod:`collections` module.
896
897* When the standard library provides a primitive for doing something, it is
898 likely (although not guaranteed) to be faster than any alternative you
899 may come up with. This is doubly true for primitives written in C, such
900 as builtins and some extension types. For example, be sure to use
901 either the :meth:`list.sort` built-in method or the related :func:`sorted`
902 function to do sorting (and see the
903 `sorting mini-HOWTO <http://wiki.python.org/moin/HowTo/Sorting>`_ for examples
904 of moderately advanced usage).
905
906* Abstractions tend to create indirections and force the interpreter to work
907 more. If the levels of indirection outweigh the amount of useful work
908 done, your program will be slower. You should avoid excessive abstraction,
909 especially under the form of tiny functions or methods (which are also often
910 detrimental to readability).
911
912If you have reached the limit of what pure Python can allow, there are tools
913to take you further away. For example, `Cython <http://cython.org>`_ can
914compile a slightly modified version of Python code into a C extension, and
915can be used on many different platforms. Cython can take advantage of
916compilation (and optional type annotations) to make your code significantly
917faster than when interpreted. If you are confident in your C programming
918skills, you can also :ref:`write a C extension module <extending-index>`
919yourself.
920
921.. seealso::
922 The wiki page devoted to `performance tips
923 <http://wiki.python.org/moin/PythonSpeed/PerformanceTips>`_.
924
925.. _efficient_string_concatenation:
926
Antoine Pitroufd9ebd42011-11-25 16:33:53 +0100927What is the most efficient way to concatenate many strings together?
928--------------------------------------------------------------------
929
930:class:`str` and :class:`bytes` objects are immutable, therefore concatenating
931many strings together is inefficient as each concatenation creates a new
932object. In the general case, the total runtime cost is quadratic in the
933total string length.
934
935To accumulate many :class:`str` objects, the recommended idiom is to place
936them into a list and call :meth:`str.join` at the end::
937
938 chunks = []
939 for s in my_strings:
940 chunks.append(s)
941 result = ''.join(chunks)
942
943(another reasonably efficient idiom is to use :class:`io.StringIO`)
944
945To accumulate many :class:`bytes` objects, the recommended idiom is to extend
946a :class:`bytearray` object using in-place concatenation (the ``+=`` operator)::
947
948 result = bytearray()
949 for b in my_bytes_objects:
950 result += b
951
952
Georg Brandld7413152009-10-11 21:25:26 +0000953Sequences (Tuples/Lists)
954========================
955
956How do I convert between tuples and lists?
957------------------------------------------
958
959The type constructor ``tuple(seq)`` converts any sequence (actually, any
960iterable) into a tuple with the same items in the same order.
961
962For example, ``tuple([1, 2, 3])`` yields ``(1, 2, 3)`` and ``tuple('abc')``
963yields ``('a', 'b', 'c')``. If the argument is a tuple, it does not make a copy
964but returns the same object, so it is cheap to call :func:`tuple` when you
965aren't sure that an object is already a tuple.
966
967The type constructor ``list(seq)`` converts any sequence or iterable into a list
968with the same items in the same order. For example, ``list((1, 2, 3))`` yields
969``[1, 2, 3]`` and ``list('abc')`` yields ``['a', 'b', 'c']``. If the argument
970is a list, it makes a copy just like ``seq[:]`` would.
971
972
973What's a negative index?
974------------------------
975
976Python sequences are indexed with positive numbers and negative numbers. For
977positive numbers 0 is the first index 1 is the second index and so forth. For
978negative indices -1 is the last index and -2 is the penultimate (next to last)
979index and so forth. Think of ``seq[-n]`` as the same as ``seq[len(seq)-n]``.
980
981Using negative indices can be very convenient. For example ``S[:-1]`` is all of
982the string except for its last character, which is useful for removing the
983trailing newline from a string.
984
985
986How do I iterate over a sequence in reverse order?
987--------------------------------------------------
988
Georg Brandlc4a55fc2010-02-06 18:46:57 +0000989Use the :func:`reversed` built-in function, which is new in Python 2.4::
Georg Brandld7413152009-10-11 21:25:26 +0000990
991 for x in reversed(sequence):
992 ... # do something with x...
993
994This won't touch your original sequence, but build a new copy with reversed
995order to iterate over.
996
997With Python 2.3, you can use an extended slice syntax::
998
999 for x in sequence[::-1]:
1000 ... # do something with x...
1001
1002
1003How do you remove duplicates from a list?
1004-----------------------------------------
1005
1006See the Python Cookbook for a long discussion of many ways to do this:
1007
1008 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
1009
1010If you don't mind reordering the list, sort it and then scan from the end of the
1011list, deleting duplicates as you go::
1012
Georg Brandl62eaaf62009-12-19 17:51:41 +00001013 if mylist:
1014 mylist.sort()
1015 last = mylist[-1]
1016 for i in range(len(mylist)-2, -1, -1):
1017 if last == mylist[i]:
1018 del mylist[i]
Georg Brandld7413152009-10-11 21:25:26 +00001019 else:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001020 last = mylist[i]
Georg Brandld7413152009-10-11 21:25:26 +00001021
Antoine Pitrouf3520402011-12-03 22:19:55 +01001022If all elements of the list may be used as set keys (i.e. they are all
1023:term:`hashable`) this is often faster ::
Georg Brandld7413152009-10-11 21:25:26 +00001024
Georg Brandl62eaaf62009-12-19 17:51:41 +00001025 mylist = list(set(mylist))
Georg Brandld7413152009-10-11 21:25:26 +00001026
1027This converts the list into a set, thereby removing duplicates, and then back
1028into a list.
1029
1030
1031How do you make an array in Python?
1032-----------------------------------
1033
1034Use a list::
1035
1036 ["this", 1, "is", "an", "array"]
1037
1038Lists are equivalent to C or Pascal arrays in their time complexity; the primary
1039difference is that a Python list can contain objects of many different types.
1040
1041The ``array`` module also provides methods for creating arrays of fixed types
1042with compact representations, but they are slower to index than lists. Also
1043note that the Numeric extensions and others define array-like structures with
1044various characteristics as well.
1045
1046To get Lisp-style linked lists, you can emulate cons cells using tuples::
1047
1048 lisp_list = ("like", ("this", ("example", None) ) )
1049
1050If mutability is desired, you could use lists instead of tuples. Here the
1051analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is
1052``lisp_list[1]``. Only do this if you're sure you really need to, because it's
1053usually a lot slower than using Python lists.
1054
1055
1056How do I create a multidimensional list?
1057----------------------------------------
1058
1059You probably tried to make a multidimensional array like this::
1060
1061 A = [[None] * 2] * 3
1062
1063This looks correct if you print it::
1064
1065 >>> A
1066 [[None, None], [None, None], [None, None]]
1067
1068But when you assign a value, it shows up in multiple places:
1069
1070 >>> A[0][0] = 5
1071 >>> A
1072 [[5, None], [5, None], [5, None]]
1073
1074The reason is that replicating a list with ``*`` doesn't create copies, it only
1075creates references to the existing objects. The ``*3`` creates a list
1076containing 3 references to the same list of length two. Changes to one row will
1077show in all rows, which is almost certainly not what you want.
1078
1079The suggested approach is to create a list of the desired length first and then
1080fill in each element with a newly created list::
1081
1082 A = [None] * 3
1083 for i in range(3):
1084 A[i] = [None] * 2
1085
1086This generates a list containing 3 different lists of length two. You can also
1087use a list comprehension::
1088
1089 w, h = 2, 3
1090 A = [[None] * w for i in range(h)]
1091
1092Or, you can use an extension that provides a matrix datatype; `Numeric Python
Georg Brandl495f7b52009-10-27 15:28:25 +00001093<http://numpy.scipy.org/>`_ is the best known.
Georg Brandld7413152009-10-11 21:25:26 +00001094
1095
1096How do I apply a method to a sequence of objects?
1097-------------------------------------------------
1098
1099Use a list comprehension::
1100
Georg Brandl62eaaf62009-12-19 17:51:41 +00001101 result = [obj.method() for obj in mylist]
Georg Brandld7413152009-10-11 21:25:26 +00001102
1103
1104Dictionaries
1105============
1106
1107How can I get a dictionary to display its keys in a consistent order?
1108---------------------------------------------------------------------
1109
1110You can't. Dictionaries store their keys in an unpredictable order, so the
1111display order of a dictionary's elements will be similarly unpredictable.
1112
1113This can be frustrating if you want to save a printable version to a file, make
1114some changes and then compare it with some other printed dictionary. In this
1115case, use the ``pprint`` module to pretty-print the dictionary; the items will
1116be presented in order sorted by the key.
1117
Georg Brandl62eaaf62009-12-19 17:51:41 +00001118A more complicated solution is to subclass ``dict`` to create a
Georg Brandld7413152009-10-11 21:25:26 +00001119``SortedDict`` class that prints itself in a predictable order. Here's one
1120simpleminded implementation of such a class::
1121
Georg Brandl62eaaf62009-12-19 17:51:41 +00001122 class SortedDict(dict):
Georg Brandld7413152009-10-11 21:25:26 +00001123 def __repr__(self):
Georg Brandl62eaaf62009-12-19 17:51:41 +00001124 keys = sorted(self.keys())
1125 result = ("{!r}: {!r}".format(k, self[k]) for k in keys)
1126 return "{{{}}}".format(", ".join(result))
Georg Brandld7413152009-10-11 21:25:26 +00001127
Georg Brandl62eaaf62009-12-19 17:51:41 +00001128 __str__ = __repr__
Georg Brandld7413152009-10-11 21:25:26 +00001129
1130This will work for many common situations you might encounter, though it's far
1131from a perfect solution. The largest flaw is that if some values in the
1132dictionary are also dictionaries, their values won't be presented in any
1133particular order.
1134
1135
1136I want to do a complicated sort: can you do a Schwartzian Transform in Python?
1137------------------------------------------------------------------------------
1138
1139The technique, attributed to Randal Schwartz of the Perl community, sorts the
1140elements of a list by a metric which maps each element to its "sort value". In
1141Python, just use the ``key`` argument for the ``sort()`` method::
1142
1143 Isorted = L[:]
1144 Isorted.sort(key=lambda s: int(s[10:15]))
1145
1146The ``key`` argument is new in Python 2.4, for older versions this kind of
1147sorting is quite simple to do with list comprehensions. To sort a list of
1148strings by their uppercase values::
1149
Georg Brandl62eaaf62009-12-19 17:51:41 +00001150 tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform
Georg Brandld7413152009-10-11 21:25:26 +00001151 tmp1.sort()
1152 Usorted = [x[1] for x in tmp1]
1153
1154To sort by the integer value of a subfield extending from positions 10-15 in
1155each string::
1156
Georg Brandl62eaaf62009-12-19 17:51:41 +00001157 tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform
Georg Brandld7413152009-10-11 21:25:26 +00001158 tmp2.sort()
1159 Isorted = [x[1] for x in tmp2]
1160
Georg Brandl62eaaf62009-12-19 17:51:41 +00001161For versions prior to 3.0, Isorted may also be computed by ::
Georg Brandld7413152009-10-11 21:25:26 +00001162
1163 def intfield(s):
1164 return int(s[10:15])
1165
1166 def Icmp(s1, s2):
1167 return cmp(intfield(s1), intfield(s2))
1168
1169 Isorted = L[:]
1170 Isorted.sort(Icmp)
1171
1172but since this method calls ``intfield()`` many times for each element of L, it
1173is slower than the Schwartzian Transform.
1174
1175
1176How can I sort one list by values from another list?
1177----------------------------------------------------
1178
Georg Brandl62eaaf62009-12-19 17:51:41 +00001179Merge them into an iterator of tuples, sort the resulting list, and then pick
Georg Brandld7413152009-10-11 21:25:26 +00001180out the element you want. ::
1181
1182 >>> list1 = ["what", "I'm", "sorting", "by"]
1183 >>> list2 = ["something", "else", "to", "sort"]
1184 >>> pairs = zip(list1, list2)
Georg Brandl62eaaf62009-12-19 17:51:41 +00001185 >>> pairs = sorted(pairs)
Georg Brandld7413152009-10-11 21:25:26 +00001186 >>> pairs
Georg Brandl62eaaf62009-12-19 17:51:41 +00001187 [("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]
1188 >>> result = [x[1] for x in pairs]
Georg Brandld7413152009-10-11 21:25:26 +00001189 >>> result
1190 ['else', 'sort', 'to', 'something']
1191
Georg Brandl62eaaf62009-12-19 17:51:41 +00001192
Georg Brandld7413152009-10-11 21:25:26 +00001193An alternative for the last step is::
1194
Georg Brandl62eaaf62009-12-19 17:51:41 +00001195 >>> result = []
1196 >>> for p in pairs: result.append(p[1])
Georg Brandld7413152009-10-11 21:25:26 +00001197
1198If you find this more legible, you might prefer to use this instead of the final
1199list comprehension. However, it is almost twice as slow for long lists. Why?
1200First, the ``append()`` operation has to reallocate memory, and while it uses
1201some tricks to avoid doing that each time, it still has to do it occasionally,
1202and that costs quite a bit. Second, the expression "result.append" requires an
1203extra attribute lookup, and third, there's a speed reduction from having to make
1204all those function calls.
1205
1206
1207Objects
1208=======
1209
1210What is a class?
1211----------------
1212
1213A class is the particular object type created by executing a class statement.
1214Class objects are used as templates to create instance objects, which embody
1215both the data (attributes) and code (methods) specific to a datatype.
1216
1217A class can be based on one or more other classes, called its base class(es). It
1218then inherits the attributes and methods of its base classes. This allows an
1219object model to be successively refined by inheritance. You might have a
1220generic ``Mailbox`` class that provides basic accessor methods for a mailbox,
1221and subclasses such as ``MboxMailbox``, ``MaildirMailbox``, ``OutlookMailbox``
1222that handle various specific mailbox formats.
1223
1224
1225What is a method?
1226-----------------
1227
1228A method is a function on some object ``x`` that you normally call as
1229``x.name(arguments...)``. Methods are defined as functions inside the class
1230definition::
1231
1232 class C:
1233 def meth (self, arg):
1234 return arg * 2 + self.attribute
1235
1236
1237What is self?
1238-------------
1239
1240Self is merely a conventional name for the first argument of a method. A method
1241defined as ``meth(self, a, b, c)`` should be called as ``x.meth(a, b, c)`` for
1242some instance ``x`` of the class in which the definition occurs; the called
1243method will think it is called as ``meth(x, a, b, c)``.
1244
1245See also :ref:`why-self`.
1246
1247
1248How do I check if an object is an instance of a given class or of a subclass of it?
1249-----------------------------------------------------------------------------------
1250
1251Use the built-in function ``isinstance(obj, cls)``. You can check if an object
1252is an instance of any of a number of classes by providing a tuple instead of a
1253single class, e.g. ``isinstance(obj, (class1, class2, ...))``, and can also
1254check whether an object is one of Python's built-in types, e.g.
Georg Brandl62eaaf62009-12-19 17:51:41 +00001255``isinstance(obj, str)`` or ``isinstance(obj, (int, float, complex))``.
Georg Brandld7413152009-10-11 21:25:26 +00001256
1257Note that most programs do not use :func:`isinstance` on user-defined classes
1258very often. If you are developing the classes yourself, a more proper
1259object-oriented style is to define methods on the classes that encapsulate a
1260particular behaviour, instead of checking the object's class and doing a
1261different thing based on what class it is. For example, if you have a function
1262that does something::
1263
Georg Brandl62eaaf62009-12-19 17:51:41 +00001264 def search(obj):
Georg Brandld7413152009-10-11 21:25:26 +00001265 if isinstance(obj, Mailbox):
1266 # ... code to search a mailbox
1267 elif isinstance(obj, Document):
1268 # ... code to search a document
1269 elif ...
1270
1271A better approach is to define a ``search()`` method on all the classes and just
1272call it::
1273
1274 class Mailbox:
1275 def search(self):
1276 # ... code to search a mailbox
1277
1278 class Document:
1279 def search(self):
1280 # ... code to search a document
1281
1282 obj.search()
1283
1284
1285What is delegation?
1286-------------------
1287
1288Delegation is an object oriented technique (also called a design pattern).
1289Let's say you have an object ``x`` and want to change the behaviour of just one
1290of its methods. You can create a new class that provides a new implementation
1291of the method you're interested in changing and delegates all other methods to
1292the corresponding method of ``x``.
1293
1294Python programmers can easily implement delegation. For example, the following
1295class implements a class that behaves like a file but converts all written data
1296to uppercase::
1297
1298 class UpperOut:
1299
1300 def __init__(self, outfile):
1301 self._outfile = outfile
1302
1303 def write(self, s):
1304 self._outfile.write(s.upper())
1305
1306 def __getattr__(self, name):
1307 return getattr(self._outfile, name)
1308
1309Here the ``UpperOut`` class redefines the ``write()`` method to convert the
1310argument string to uppercase before calling the underlying
1311``self.__outfile.write()`` method. All other methods are delegated to the
1312underlying ``self.__outfile`` object. The delegation is accomplished via the
1313``__getattr__`` method; consult :ref:`the language reference <attribute-access>`
1314for more information about controlling attribute access.
1315
1316Note that for more general cases delegation can get trickier. When attributes
1317must be set as well as retrieved, the class must define a :meth:`__setattr__`
1318method too, and it must do so carefully. The basic implementation of
1319:meth:`__setattr__` is roughly equivalent to the following::
1320
1321 class X:
1322 ...
1323 def __setattr__(self, name, value):
1324 self.__dict__[name] = value
1325 ...
1326
1327Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store
1328local state for self without causing an infinite recursion.
1329
1330
1331How do I call a method defined in a base class from a derived class that overrides it?
1332--------------------------------------------------------------------------------------
1333
Georg Brandl62eaaf62009-12-19 17:51:41 +00001334Use the built-in :func:`super` function::
Georg Brandld7413152009-10-11 21:25:26 +00001335
1336 class Derived(Base):
1337 def meth (self):
1338 super(Derived, self).meth()
1339
Georg Brandl62eaaf62009-12-19 17:51:41 +00001340For version prior to 3.0, you may be using classic classes: For a class
1341definition such as ``class Derived(Base): ...`` you can call method ``meth()``
1342defined in ``Base`` (or one of ``Base``'s base classes) as ``Base.meth(self,
1343arguments...)``. Here, ``Base.meth`` is an unbound method, so you need to
1344provide the ``self`` argument.
Georg Brandld7413152009-10-11 21:25:26 +00001345
1346
1347How can I organize my code to make it easier to change the base class?
1348----------------------------------------------------------------------
1349
1350You could define an alias for the base class, assign the real base class to it
1351before your class definition, and use the alias throughout your class. Then all
1352you have to change is the value assigned to the alias. Incidentally, this trick
1353is also handy if you want to decide dynamically (e.g. depending on availability
1354of resources) which base class to use. Example::
1355
1356 BaseAlias = <real base class>
1357
1358 class Derived(BaseAlias):
1359 def meth(self):
1360 BaseAlias.meth(self)
1361 ...
1362
1363
1364How do I create static class data and static class methods?
1365-----------------------------------------------------------
1366
Georg Brandl62eaaf62009-12-19 17:51:41 +00001367Both static data and static methods (in the sense of C++ or Java) are supported
1368in Python.
Georg Brandld7413152009-10-11 21:25:26 +00001369
1370For static data, simply define a class attribute. To assign a new value to the
1371attribute, you have to explicitly use the class name in the assignment::
1372
1373 class C:
1374 count = 0 # number of times C.__init__ called
1375
1376 def __init__(self):
1377 C.count = C.count + 1
1378
1379 def getcount(self):
1380 return C.count # or return self.count
1381
1382``c.count`` also refers to ``C.count`` for any ``c`` such that ``isinstance(c,
1383C)`` holds, unless overridden by ``c`` itself or by some class on the base-class
1384search path from ``c.__class__`` back to ``C``.
1385
1386Caution: within a method of C, an assignment like ``self.count = 42`` creates a
Georg Brandl62eaaf62009-12-19 17:51:41 +00001387new and unrelated instance named "count" in ``self``'s own dict. Rebinding of a
1388class-static data name must always specify the class whether inside a method or
1389not::
Georg Brandld7413152009-10-11 21:25:26 +00001390
1391 C.count = 314
1392
Antoine Pitrouf3520402011-12-03 22:19:55 +01001393Static methods are possible::
Georg Brandld7413152009-10-11 21:25:26 +00001394
1395 class C:
1396 @staticmethod
1397 def static(arg1, arg2, arg3):
1398 # No 'self' parameter!
1399 ...
1400
1401However, a far more straightforward way to get the effect of a static method is
1402via a simple module-level function::
1403
1404 def getcount():
1405 return C.count
1406
1407If your code is structured so as to define one class (or tightly related class
1408hierarchy) per module, this supplies the desired encapsulation.
1409
1410
1411How can I overload constructors (or methods) in Python?
1412-------------------------------------------------------
1413
1414This answer actually applies to all methods, but the question usually comes up
1415first in the context of constructors.
1416
1417In C++ you'd write
1418
1419.. code-block:: c
1420
1421 class C {
1422 C() { cout << "No arguments\n"; }
1423 C(int i) { cout << "Argument is " << i << "\n"; }
1424 }
1425
1426In Python you have to write a single constructor that catches all cases using
1427default arguments. For example::
1428
1429 class C:
1430 def __init__(self, i=None):
1431 if i is None:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001432 print("No arguments")
Georg Brandld7413152009-10-11 21:25:26 +00001433 else:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001434 print("Argument is", i)
Georg Brandld7413152009-10-11 21:25:26 +00001435
1436This is not entirely equivalent, but close enough in practice.
1437
1438You could also try a variable-length argument list, e.g. ::
1439
1440 def __init__(self, *args):
1441 ...
1442
1443The same approach works for all method definitions.
1444
1445
1446I try to use __spam and I get an error about _SomeClassName__spam.
1447------------------------------------------------------------------
1448
1449Variable names with double leading underscores are "mangled" to provide a simple
1450but effective way to define class private variables. Any identifier of the form
1451``__spam`` (at least two leading underscores, at most one trailing underscore)
1452is textually replaced with ``_classname__spam``, where ``classname`` is the
1453current class name with any leading underscores stripped.
1454
1455This doesn't guarantee privacy: an outside user can still deliberately access
1456the "_classname__spam" attribute, and private values are visible in the object's
1457``__dict__``. Many Python programmers never bother to use private variable
1458names at all.
1459
1460
1461My class defines __del__ but it is not called when I delete the object.
1462-----------------------------------------------------------------------
1463
1464There are several possible reasons for this.
1465
1466The del statement does not necessarily call :meth:`__del__` -- it simply
1467decrements the object's reference count, and if this reaches zero
1468:meth:`__del__` is called.
1469
1470If your data structures contain circular links (e.g. a tree where each child has
1471a parent reference and each parent has a list of children) the reference counts
1472will never go back to zero. Once in a while Python runs an algorithm to detect
1473such cycles, but the garbage collector might run some time after the last
1474reference to your data structure vanishes, so your :meth:`__del__` method may be
1475called at an inconvenient and random time. This is inconvenient if you're trying
1476to reproduce a problem. Worse, the order in which object's :meth:`__del__`
1477methods are executed is arbitrary. You can run :func:`gc.collect` to force a
1478collection, but there *are* pathological cases where objects will never be
1479collected.
1480
1481Despite the cycle collector, it's still a good idea to define an explicit
1482``close()`` method on objects to be called whenever you're done with them. The
1483``close()`` method can then remove attributes that refer to subobjecs. Don't
1484call :meth:`__del__` directly -- :meth:`__del__` should call ``close()`` and
1485``close()`` should make sure that it can be called more than once for the same
1486object.
1487
1488Another way to avoid cyclical references is to use the :mod:`weakref` module,
1489which allows you to point to objects without incrementing their reference count.
1490Tree data structures, for instance, should use weak references for their parent
1491and sibling references (if they need them!).
1492
Georg Brandl62eaaf62009-12-19 17:51:41 +00001493.. XXX relevant for Python 3?
1494
1495 If the object has ever been a local variable in a function that caught an
1496 expression in an except clause, chances are that a reference to the object
1497 still exists in that function's stack frame as contained in the stack trace.
1498 Normally, calling :func:`sys.exc_clear` will take care of this by clearing
1499 the last recorded exception.
Georg Brandld7413152009-10-11 21:25:26 +00001500
1501Finally, if your :meth:`__del__` method raises an exception, a warning message
1502is printed to :data:`sys.stderr`.
1503
1504
1505How do I get a list of all instances of a given class?
1506------------------------------------------------------
1507
1508Python does not keep track of all instances of a class (or of a built-in type).
1509You can program the class's constructor to keep track of all instances by
1510keeping a list of weak references to each instance.
1511
1512
1513Modules
1514=======
1515
1516How do I create a .pyc file?
1517----------------------------
1518
1519When a module is imported for the first time (or when the source is more recent
1520than the current compiled file) a ``.pyc`` file containing the compiled code
1521should be created in the same directory as the ``.py`` file.
1522
1523One reason that a ``.pyc`` file may not be created is permissions problems with
1524the directory. This can happen, for example, if you develop as one user but run
1525as another, such as if you are testing with a web server. Creation of a .pyc
1526file is automatic if you're importing a module and Python has the ability
1527(permissions, free space, etc...) to write the compiled module back to the
1528directory.
1529
1530Running Python on a top level script is not considered an import and no ``.pyc``
1531will be created. For example, if you have a top-level module ``abc.py`` that
1532imports another module ``xyz.py``, when you run abc, ``xyz.pyc`` will be created
1533since xyz is imported, but no ``abc.pyc`` file will be created since ``abc.py``
1534isn't being imported.
1535
1536If you need to create abc.pyc -- that is, to create a .pyc file for a module
1537that is not imported -- you can, using the :mod:`py_compile` and
1538:mod:`compileall` modules.
1539
1540The :mod:`py_compile` module can manually compile any module. One way is to use
1541the ``compile()`` function in that module interactively::
1542
1543 >>> import py_compile
1544 >>> py_compile.compile('abc.py')
1545
1546This will write the ``.pyc`` to the same location as ``abc.py`` (or you can
1547override that with the optional parameter ``cfile``).
1548
1549You can also automatically compile all files in a directory or directories using
1550the :mod:`compileall` module. You can do it from the shell prompt by running
1551``compileall.py`` and providing the path of a directory containing Python files
1552to compile::
1553
1554 python -m compileall .
1555
1556
1557How do I find the current module name?
1558--------------------------------------
1559
1560A module can find out its own module name by looking at the predefined global
1561variable ``__name__``. If this has the value ``'__main__'``, the program is
1562running as a script. Many modules that are usually used by importing them also
1563provide a command-line interface or a self-test, and only execute this code
1564after checking ``__name__``::
1565
1566 def main():
Georg Brandl62eaaf62009-12-19 17:51:41 +00001567 print('Running test...')
Georg Brandld7413152009-10-11 21:25:26 +00001568 ...
1569
1570 if __name__ == '__main__':
1571 main()
1572
1573
1574How can I have modules that mutually import each other?
1575-------------------------------------------------------
1576
1577Suppose you have the following modules:
1578
1579foo.py::
1580
1581 from bar import bar_var
1582 foo_var = 1
1583
1584bar.py::
1585
1586 from foo import foo_var
1587 bar_var = 2
1588
1589The problem is that the interpreter will perform the following steps:
1590
1591* main imports foo
1592* Empty globals for foo are created
1593* foo is compiled and starts executing
1594* foo imports bar
1595* Empty globals for bar are created
1596* bar is compiled and starts executing
1597* bar imports foo (which is a no-op since there already is a module named foo)
1598* bar.foo_var = foo.foo_var
1599
1600The last step fails, because Python isn't done with interpreting ``foo`` yet and
1601the global symbol dictionary for ``foo`` is still empty.
1602
1603The same thing happens when you use ``import foo``, and then try to access
1604``foo.foo_var`` in global code.
1605
1606There are (at least) three possible workarounds for this problem.
1607
1608Guido van Rossum recommends avoiding all uses of ``from <module> import ...``,
1609and placing all code inside functions. Initializations of global variables and
1610class variables should use constants or built-in functions only. This means
1611everything from an imported module is referenced as ``<module>.<name>``.
1612
1613Jim Roskind suggests performing steps in the following order in each module:
1614
1615* exports (globals, functions, and classes that don't need imported base
1616 classes)
1617* ``import`` statements
1618* active code (including globals that are initialized from imported values).
1619
1620van Rossum doesn't like this approach much because the imports appear in a
1621strange place, but it does work.
1622
1623Matthias Urlichs recommends restructuring your code so that the recursive import
1624is not necessary in the first place.
1625
1626These solutions are not mutually exclusive.
1627
1628
1629__import__('x.y.z') returns <module 'x'>; how do I get z?
1630---------------------------------------------------------
1631
1632Try::
1633
1634 __import__('x.y.z').y.z
1635
1636For more realistic situations, you may have to do something like ::
1637
1638 m = __import__(s)
1639 for i in s.split(".")[1:]:
1640 m = getattr(m, i)
1641
1642See :mod:`importlib` for a convenience function called
1643:func:`~importlib.import_module`.
1644
1645
1646
1647When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
1648-------------------------------------------------------------------------------------------------
1649
1650For reasons of efficiency as well as consistency, Python only reads the module
1651file on the first time a module is imported. If it didn't, in a program
1652consisting of many modules where each one imports the same basic module, the
1653basic module would be parsed and re-parsed many times. To force rereading of a
1654changed module, do this::
1655
Georg Brandl62eaaf62009-12-19 17:51:41 +00001656 import imp
Georg Brandld7413152009-10-11 21:25:26 +00001657 import modname
Georg Brandl62eaaf62009-12-19 17:51:41 +00001658 imp.reload(modname)
Georg Brandld7413152009-10-11 21:25:26 +00001659
1660Warning: this technique is not 100% fool-proof. In particular, modules
1661containing statements like ::
1662
1663 from modname import some_objects
1664
1665will continue to work with the old version of the imported objects. If the
1666module contains class definitions, existing class instances will *not* be
1667updated to use the new class definition. This can result in the following
1668paradoxical behaviour:
1669
Georg Brandl62eaaf62009-12-19 17:51:41 +00001670 >>> import imp
Georg Brandld7413152009-10-11 21:25:26 +00001671 >>> import cls
1672 >>> c = cls.C() # Create an instance of C
Georg Brandl62eaaf62009-12-19 17:51:41 +00001673 >>> imp.reload(cls)
1674 <module 'cls' from 'cls.py'>
Georg Brandld7413152009-10-11 21:25:26 +00001675 >>> isinstance(c, cls.C) # isinstance is false?!?
1676 False
1677
Georg Brandl62eaaf62009-12-19 17:51:41 +00001678The nature of the problem is made clear if you print out the "identity" of the
1679class objects:
Georg Brandld7413152009-10-11 21:25:26 +00001680
Georg Brandl62eaaf62009-12-19 17:51:41 +00001681 >>> hex(id(c.__class__))
1682 '0x7352a0'
1683 >>> hex(id(cls.C))
1684 '0x4198d0'