blob: 2d5d2b123a1130de0884fb9d3b35aeb7580afc6b [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
209How do I share global variables across modules?
210------------------------------------------------
211
212The canonical way to share information across modules within a single program is
213to create a special module (often called config or cfg). Just import the config
214module in all modules of your application; the module then becomes available as
215a global name. Because there is only one instance of each module, any changes
216made to the module object get reflected everywhere. For example:
217
218config.py::
219
220 x = 0 # Default value of the 'x' configuration setting
221
222mod.py::
223
224 import config
225 config.x = 1
226
227main.py::
228
229 import config
230 import mod
Georg Brandl62eaaf62009-12-19 17:51:41 +0000231 print(config.x)
Georg Brandld7413152009-10-11 21:25:26 +0000232
233Note that using a module is also the basis for implementing the Singleton design
234pattern, for the same reason.
235
236
237What are the "best practices" for using import in a module?
238-----------------------------------------------------------
239
240In general, don't use ``from modulename import *``. Doing so clutters the
241importer's namespace. Some people avoid this idiom even with the few modules
242that were designed to be imported in this manner. Modules designed in this
Georg Brandld404fa62009-10-13 16:55:12 +0000243manner include :mod:`tkinter`, and :mod:`threading`.
Georg Brandld7413152009-10-11 21:25:26 +0000244
245Import modules at the top of a file. Doing so makes it clear what other modules
246your code requires and avoids questions of whether the module name is in scope.
247Using one import per line makes it easy to add and delete module imports, but
248using multiple imports per line uses less screen space.
249
250It's good practice if you import modules in the following order:
251
Georg Brandl62eaaf62009-12-19 17:51:41 +00002521. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re``
Georg Brandld7413152009-10-11 21:25:26 +00002532. third-party library modules (anything installed in Python's site-packages
254 directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc.
2553. locally-developed modules
256
257Never use relative package imports. If you're writing code that's in the
258``package.sub.m1`` module and want to import ``package.sub.m2``, do not just
Georg Brandl11b63622009-12-20 14:21:27 +0000259write ``from . import m2``, even though it's legal. Write ``from package.sub
260import m2`` instead. See :pep:`328` for details.
Georg Brandld7413152009-10-11 21:25:26 +0000261
262It is sometimes necessary to move imports to a function or class to avoid
263problems with circular imports. Gordon McMillan says:
264
265 Circular imports are fine where both modules use the "import <module>" form
266 of import. They fail when the 2nd module wants to grab a name out of the
267 first ("from module import name") and the import is at the top level. That's
268 because names in the 1st are not yet available, because the first module is
269 busy importing the 2nd.
270
271In this case, if the second module is only used in one function, then the import
272can easily be moved into that function. By the time the import is called, the
273first module will have finished initializing, and the second module can do its
274import.
275
276It may also be necessary to move imports out of the top level of code if some of
277the modules are platform-specific. In that case, it may not even be possible to
278import all of the modules at the top of the file. In this case, importing the
279correct modules in the corresponding platform-specific code is a good option.
280
281Only move imports into a local scope, such as inside a function definition, if
282it's necessary to solve a problem such as avoiding a circular import or are
283trying to reduce the initialization time of a module. This technique is
284especially helpful if many of the imports are unnecessary depending on how the
285program executes. You may also want to move imports into a function if the
286modules are only ever used in that function. Note that loading a module the
287first time may be expensive because of the one time initialization of the
288module, but loading a module multiple times is virtually free, costing only a
289couple of dictionary lookups. Even if the module name has gone out of scope,
290the module is probably available in :data:`sys.modules`.
291
292If only instances of a specific class use a module, then it is reasonable to
293import the module in the class's ``__init__`` method and then assign the module
294to an instance variable so that the module is always available (via that
295instance variable) during the life of the object. Note that to delay an import
296until the class is instantiated, the import must be inside a method. Putting
297the import inside the class but outside of any method still causes the import to
298occur when the module is initialized.
299
300
301How can I pass optional or keyword parameters from one function to another?
302---------------------------------------------------------------------------
303
304Collect the arguments using the ``*`` and ``**`` specifiers in the function's
305parameter list; this gives you the positional arguments as a tuple and the
306keyword arguments as a dictionary. You can then pass these arguments when
307calling another function by using ``*`` and ``**``::
308
309 def f(x, *args, **kwargs):
310 ...
311 kwargs['width'] = '14.3c'
312 ...
313 g(x, *args, **kwargs)
314
Georg Brandld7413152009-10-11 21:25:26 +0000315
Chris Jerdonekc2a7fd62012-11-28 02:29:33 -0800316.. _faq-argument-vs-parameter:
317
318What is the difference between arguments and parameters?
319--------------------------------------------------------
320
321:term:`Parameters <parameter>` are defined by the names that appear in a
322function definition, whereas :term:`arguments <argument>` are the values
323actually passed to a function when calling it. Parameters define what types of
324arguments a function can accept. For example, given the function definition::
325
326 def func(foo, bar=None, **kwargs):
327 pass
328
329*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling
330``func``, for example::
331
332 func(42, bar=314, extra=somevar)
333
334the values ``42``, ``314``, and ``somevar`` are arguments.
335
336
Georg Brandld7413152009-10-11 21:25:26 +0000337How do I write a function with output parameters (call by reference)?
338---------------------------------------------------------------------
339
340Remember that arguments are passed by assignment in Python. Since assignment
341just creates references to objects, there's no alias between an argument name in
342the caller and callee, and so no call-by-reference per se. You can achieve the
343desired effect in a number of ways.
344
3451) By returning a tuple of the results::
346
347 def func2(a, b):
348 a = 'new-value' # a and b are local names
349 b = b + 1 # assigned to new objects
350 return a, b # return new values
351
352 x, y = 'old-value', 99
353 x, y = func2(x, y)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000354 print(x, y) # output: new-value 100
Georg Brandld7413152009-10-11 21:25:26 +0000355
356 This is almost always the clearest solution.
357
3582) By using global variables. This isn't thread-safe, and is not recommended.
359
3603) By passing a mutable (changeable in-place) object::
361
362 def func1(a):
363 a[0] = 'new-value' # 'a' references a mutable list
364 a[1] = a[1] + 1 # changes a shared object
365
366 args = ['old-value', 99]
367 func1(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000368 print(args[0], args[1]) # output: new-value 100
Georg Brandld7413152009-10-11 21:25:26 +0000369
3704) By passing in a dictionary that gets mutated::
371
372 def func3(args):
373 args['a'] = 'new-value' # args is a mutable dictionary
374 args['b'] = args['b'] + 1 # change it in-place
375
376 args = {'a':' old-value', 'b': 99}
377 func3(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000378 print(args['a'], args['b'])
Georg Brandld7413152009-10-11 21:25:26 +0000379
3805) Or bundle up values in a class instance::
381
382 class callByRef:
383 def __init__(self, **args):
384 for (key, value) in args.items():
385 setattr(self, key, value)
386
387 def func4(args):
388 args.a = 'new-value' # args is a mutable callByRef
389 args.b = args.b + 1 # change object in-place
390
391 args = callByRef(a='old-value', b=99)
392 func4(args)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000393 print(args.a, args.b)
Georg Brandld7413152009-10-11 21:25:26 +0000394
395
396 There's almost never a good reason to get this complicated.
397
398Your best choice is to return a tuple containing the multiple results.
399
400
401How do you make a higher order function in Python?
402--------------------------------------------------
403
404You have two choices: you can use nested scopes or you can use callable objects.
405For example, suppose you wanted to define ``linear(a,b)`` which returns a
406function ``f(x)`` that computes the value ``a*x+b``. Using nested scopes::
407
408 def linear(a, b):
409 def result(x):
410 return a * x + b
411 return result
412
413Or using a callable object::
414
415 class linear:
416
417 def __init__(self, a, b):
418 self.a, self.b = a, b
419
420 def __call__(self, x):
421 return self.a * x + self.b
422
423In both cases, ::
424
425 taxes = linear(0.3, 2)
426
427gives a callable object where ``taxes(10e6) == 0.3 * 10e6 + 2``.
428
429The callable object approach has the disadvantage that it is a bit slower and
430results in slightly longer code. However, note that a collection of callables
431can share their signature via inheritance::
432
433 class exponential(linear):
434 # __init__ inherited
435 def __call__(self, x):
436 return self.a * (x ** self.b)
437
438Object can encapsulate state for several methods::
439
440 class counter:
441
442 value = 0
443
444 def set(self, x):
445 self.value = x
446
447 def up(self):
448 self.value = self.value + 1
449
450 def down(self):
451 self.value = self.value - 1
452
453 count = counter()
454 inc, dec, reset = count.up, count.down, count.set
455
456Here ``inc()``, ``dec()`` and ``reset()`` act like functions which share the
457same counting variable.
458
459
460How do I copy an object in Python?
461----------------------------------
462
463In general, try :func:`copy.copy` or :func:`copy.deepcopy` for the general case.
464Not all objects can be copied, but most can.
465
466Some objects can be copied more easily. Dictionaries have a :meth:`~dict.copy`
467method::
468
469 newdict = olddict.copy()
470
471Sequences can be copied by slicing::
472
473 new_l = l[:]
474
475
476How can I find the methods or attributes of an object?
477------------------------------------------------------
478
479For an instance x of a user-defined class, ``dir(x)`` returns an alphabetized
480list of the names containing the instance attributes and methods and attributes
481defined by its class.
482
483
484How can my code discover the name of an object?
485-----------------------------------------------
486
487Generally speaking, it can't, because objects don't really have names.
488Essentially, assignment always binds a name to a value; The same is true of
489``def`` and ``class`` statements, but in that case the value is a
490callable. Consider the following code::
491
492 class A:
493 pass
494
495 B = A
496
497 a = B()
498 b = a
Georg Brandl62eaaf62009-12-19 17:51:41 +0000499 print(b)
500 <__main__.A object at 0x16D07CC>
501 print(a)
502 <__main__.A object at 0x16D07CC>
Georg Brandld7413152009-10-11 21:25:26 +0000503
504Arguably the class has a name: even though it is bound to two names and invoked
505through the name B the created instance is still reported as an instance of
506class A. However, it is impossible to say whether the instance's name is a or
507b, since both names are bound to the same value.
508
509Generally speaking it should not be necessary for your code to "know the names"
510of particular values. Unless you are deliberately writing introspective
511programs, this is usually an indication that a change of approach might be
512beneficial.
513
514In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to
515this question:
516
517 The same way as you get the name of that cat you found on your porch: the cat
518 (object) itself cannot tell you its name, and it doesn't really care -- so
519 the only way to find out what it's called is to ask all your neighbours
520 (namespaces) if it's their cat (object)...
521
522 ....and don't be surprised if you'll find that it's known by many names, or
523 no name at all!
524
525
526What's up with the comma operator's precedence?
527-----------------------------------------------
528
529Comma is not an operator in Python. Consider this session::
530
531 >>> "a" in "b", "a"
Georg Brandl62eaaf62009-12-19 17:51:41 +0000532 (False, 'a')
Georg Brandld7413152009-10-11 21:25:26 +0000533
534Since the comma is not an operator, but a separator between expressions the
535above is evaluated as if you had entered::
536
537 >>> ("a" in "b"), "a"
538
539not::
540
Georg Brandl62eaaf62009-12-19 17:51:41 +0000541 >>> "a" in ("b", "a")
Georg Brandld7413152009-10-11 21:25:26 +0000542
543The same is true of the various assignment operators (``=``, ``+=`` etc). They
544are not truly operators but syntactic delimiters in assignment statements.
545
546
547Is there an equivalent of C's "?:" ternary operator?
548----------------------------------------------------
549
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100550Yes, there is. The syntax is as follows::
Georg Brandld7413152009-10-11 21:25:26 +0000551
552 [on_true] if [expression] else [on_false]
553
554 x, y = 50, 25
Georg Brandld7413152009-10-11 21:25:26 +0000555 small = x if x < y else y
556
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100557Before this syntax was introduced in Python 2.5, a common idiom was to use
558logical operators::
Georg Brandld7413152009-10-11 21:25:26 +0000559
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100560 [expression] and [on_true] or [on_false]
Georg Brandld7413152009-10-11 21:25:26 +0000561
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100562However, this idiom is unsafe, as it can give wrong results when *on_true*
563has a false boolean value. Therefore, it is always better to use
564the ``... if ... else ...`` form.
Georg Brandld7413152009-10-11 21:25:26 +0000565
566
567Is it possible to write obfuscated one-liners in Python?
568--------------------------------------------------------
569
570Yes. Usually this is done by nesting :keyword:`lambda` within
571:keyword:`lambda`. See the following three examples, due to Ulf Bartelt::
572
Georg Brandl62eaaf62009-12-19 17:51:41 +0000573 from functools import reduce
574
Georg Brandld7413152009-10-11 21:25:26 +0000575 # Primes < 1000
Georg Brandl62eaaf62009-12-19 17:51:41 +0000576 print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
577 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 +0000578
579 # First 10 Fibonacci numbers
Georg Brandl62eaaf62009-12-19 17:51:41 +0000580 print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1:
581 f(x,f), range(10))))
Georg Brandld7413152009-10-11 21:25:26 +0000582
583 # Mandelbrot set
Georg Brandl62eaaf62009-12-19 17:51:41 +0000584 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 +0000585 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
586 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
587 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
588 >=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(
589 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 +0000590 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24))
Georg Brandld7413152009-10-11 21:25:26 +0000591 # \___ ___/ \___ ___/ | | |__ lines on screen
592 # V V | |______ columns on screen
593 # | | |__________ maximum of "iterations"
594 # | |_________________ range on y axis
595 # |____________________________ range on x axis
596
597Don't try this at home, kids!
598
599
600Numbers and strings
601===================
602
603How do I specify hexadecimal and octal integers?
604------------------------------------------------
605
Georg Brandl62eaaf62009-12-19 17:51:41 +0000606To specify an octal digit, precede the octal value with a zero, and then a lower
607or uppercase "o". For example, to set the variable "a" to the octal value "10"
608(8 in decimal), type::
Georg Brandld7413152009-10-11 21:25:26 +0000609
Georg Brandl62eaaf62009-12-19 17:51:41 +0000610 >>> a = 0o10
Georg Brandld7413152009-10-11 21:25:26 +0000611 >>> a
612 8
613
614Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,
615and then a lower or uppercase "x". Hexadecimal digits can be specified in lower
616or uppercase. For example, in the Python interpreter::
617
618 >>> a = 0xa5
619 >>> a
620 165
621 >>> b = 0XB2
622 >>> b
623 178
624
625
Georg Brandl62eaaf62009-12-19 17:51:41 +0000626Why does -22 // 10 return -3?
627-----------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000628
629It's primarily driven by the desire that ``i % j`` have the same sign as ``j``.
630If you want that, and also want::
631
Georg Brandl62eaaf62009-12-19 17:51:41 +0000632 i == (i // j) * j + (i % j)
Georg Brandld7413152009-10-11 21:25:26 +0000633
634then integer division has to return the floor. C also requires that identity to
Georg Brandl62eaaf62009-12-19 17:51:41 +0000635hold, and then compilers that truncate ``i // j`` need to make ``i % j`` have
636the same sign as ``i``.
Georg Brandld7413152009-10-11 21:25:26 +0000637
638There are few real use cases for ``i % j`` when ``j`` is negative. When ``j``
639is positive, there are many, and in virtually all of them it's more useful for
640``i % j`` to be ``>= 0``. If the clock says 10 now, what did it say 200 hours
641ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to
642bite.
643
644
645How do I convert a string to a number?
646--------------------------------------
647
648For integers, use the built-in :func:`int` type constructor, e.g. ``int('144')
649== 144``. Similarly, :func:`float` converts to floating-point,
650e.g. ``float('144') == 144.0``.
651
652By default, these interpret the number as decimal, so that ``int('0144') ==
653144`` and ``int('0x144')`` raises :exc:`ValueError`. ``int(string, base)`` takes
654the base to convert from as a second optional argument, so ``int('0x144', 16) ==
655324``. If the base is specified as 0, the number is interpreted using Python's
656rules: a leading '0' indicates octal, and '0x' indicates a hex number.
657
658Do not use the built-in function :func:`eval` if all you need is to convert
659strings to numbers. :func:`eval` will be significantly slower and it presents a
660security risk: someone could pass you a Python expression that might have
661unwanted side effects. For example, someone could pass
662``__import__('os').system("rm -rf $HOME")`` which would erase your home
663directory.
664
665:func:`eval` also has the effect of interpreting numbers as Python expressions,
Georg Brandl62eaaf62009-12-19 17:51:41 +0000666so that e.g. ``eval('09')`` gives a syntax error because Python does not allow
667leading '0' in a decimal number (except '0').
Georg Brandld7413152009-10-11 21:25:26 +0000668
669
670How do I convert a number to a string?
671--------------------------------------
672
673To convert, e.g., the number 144 to the string '144', use the built-in type
674constructor :func:`str`. If you want a hexadecimal or octal representation, use
Georg Brandl62eaaf62009-12-19 17:51:41 +0000675the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see
676the :ref:`string-formatting` section, e.g. ``"{:04d}".format(144)`` yields
Georg Brandl11b63622009-12-20 14:21:27 +0000677``'0144'`` and ``"{:.3f}".format(1/3)`` yields ``'0.333'``.
Georg Brandld7413152009-10-11 21:25:26 +0000678
679
680How do I modify a string in place?
681----------------------------------
682
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100683You can't, because strings are immutable. In most situations, you should
684simply construct a new string from the various parts you want to assemble
685it from. However, if you need an object with the ability to modify in-place
686unicode data, try using a :class:`io.StringIO` object or the :mod:`array`
687module::
Georg Brandld7413152009-10-11 21:25:26 +0000688
689 >>> s = "Hello, world"
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100690 >>> sio = io.StringIO(s)
691 >>> sio.getvalue()
692 'Hello, world'
693 >>> sio.seek(7)
694 7
695 >>> sio.write("there!")
696 6
697 >>> sio.getvalue()
Georg Brandld7413152009-10-11 21:25:26 +0000698 'Hello, there!'
699
700 >>> import array
Georg Brandl62eaaf62009-12-19 17:51:41 +0000701 >>> a = array.array('u', s)
702 >>> print(a)
703 array('u', 'Hello, world')
704 >>> a[0] = 'y'
705 >>> print(a)
706 array('u', 'yello world')
707 >>> a.tounicode()
Georg Brandld7413152009-10-11 21:25:26 +0000708 'yello, world'
709
710
711How do I use strings to call functions/methods?
712-----------------------------------------------
713
714There are various techniques.
715
716* The best is to use a dictionary that maps strings to functions. The primary
717 advantage of this technique is that the strings do not need to match the names
718 of the functions. This is also the primary technique used to emulate a case
719 construct::
720
721 def a():
722 pass
723
724 def b():
725 pass
726
727 dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
728
729 dispatch[get_input()]() # Note trailing parens to call function
730
731* Use the built-in function :func:`getattr`::
732
733 import foo
734 getattr(foo, 'bar')()
735
736 Note that :func:`getattr` works on any object, including classes, class
737 instances, modules, and so on.
738
739 This is used in several places in the standard library, like this::
740
741 class Foo:
742 def do_foo(self):
743 ...
744
745 def do_bar(self):
746 ...
747
748 f = getattr(foo_instance, 'do_' + opname)
749 f()
750
751
752* Use :func:`locals` or :func:`eval` to resolve the function name::
753
754 def myFunc():
Georg Brandl62eaaf62009-12-19 17:51:41 +0000755 print("hello")
Georg Brandld7413152009-10-11 21:25:26 +0000756
757 fname = "myFunc"
758
759 f = locals()[fname]
760 f()
761
762 f = eval(fname)
763 f()
764
765 Note: Using :func:`eval` is slow and dangerous. If you don't have absolute
766 control over the contents of the string, someone could pass a string that
767 resulted in an arbitrary function being executed.
768
769Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
770-------------------------------------------------------------------------------------
771
Antoine Pitrouf3520402011-12-03 22:19:55 +0100772You can use ``S.rstrip("\r\n")`` to remove all occurrences of any line
773terminator from the end of the string ``S`` without removing other trailing
774whitespace. If the string ``S`` represents more than one line, with several
775empty lines at the end, the line terminators for all the blank lines will
776be removed::
Georg Brandld7413152009-10-11 21:25:26 +0000777
778 >>> lines = ("line 1 \r\n"
779 ... "\r\n"
780 ... "\r\n")
781 >>> lines.rstrip("\n\r")
Georg Brandl62eaaf62009-12-19 17:51:41 +0000782 'line 1 '
Georg Brandld7413152009-10-11 21:25:26 +0000783
784Since this is typically only desired when reading text one line at a time, using
785``S.rstrip()`` this way works well.
786
Georg Brandld7413152009-10-11 21:25:26 +0000787
788Is there a scanf() or sscanf() equivalent?
789------------------------------------------
790
791Not as such.
792
793For simple input parsing, the easiest approach is usually to split the line into
794whitespace-delimited words using the :meth:`~str.split` method of string objects
795and then convert decimal strings to numeric values using :func:`int` or
796:func:`float`. ``split()`` supports an optional "sep" parameter which is useful
797if the line uses something other than whitespace as a separator.
798
Brian Curtin5a7a52f2010-09-23 13:45:21 +0000799For more complicated input parsing, regular expressions are more powerful
Georg Brandl60203b42010-10-06 10:11:56 +0000800than C's :c:func:`sscanf` and better suited for the task.
Georg Brandld7413152009-10-11 21:25:26 +0000801
802
Georg Brandl62eaaf62009-12-19 17:51:41 +0000803What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?
804-------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000805
Georg Brandl62eaaf62009-12-19 17:51:41 +0000806See the :ref:`unicode-howto`.
Georg Brandld7413152009-10-11 21:25:26 +0000807
808
Antoine Pitrou432259f2011-12-09 23:10:31 +0100809Performance
810===========
811
812My program is too slow. How do I speed it up?
813---------------------------------------------
814
815That's a tough one, in general. First, here are a list of things to
816remember before diving further:
817
Georg Brandl300a6912012-03-14 22:40:08 +0100818* Performance characteristics vary across Python implementations. This FAQ
Antoine Pitrou432259f2011-12-09 23:10:31 +0100819 focusses on :term:`CPython`.
Georg Brandl300a6912012-03-14 22:40:08 +0100820* Behaviour can vary across operating systems, especially when talking about
Antoine Pitrou432259f2011-12-09 23:10:31 +0100821 I/O or multi-threading.
822* You should always find the hot spots in your program *before* attempting to
823 optimize any code (see the :mod:`profile` module).
824* Writing benchmark scripts will allow you to iterate quickly when searching
825 for improvements (see the :mod:`timeit` module).
826* It is highly recommended to have good code coverage (through unit testing
827 or any other technique) before potentially introducing regressions hidden
828 in sophisticated optimizations.
829
830That being said, there are many tricks to speed up Python code. Here are
831some general principles which go a long way towards reaching acceptable
832performance levels:
833
834* Making your algorithms faster (or changing to faster ones) can yield
835 much larger benefits than trying to sprinkle micro-optimization tricks
836 all over your code.
837
838* Use the right data structures. Study documentation for the :ref:`bltin-types`
839 and the :mod:`collections` module.
840
841* When the standard library provides a primitive for doing something, it is
842 likely (although not guaranteed) to be faster than any alternative you
843 may come up with. This is doubly true for primitives written in C, such
844 as builtins and some extension types. For example, be sure to use
845 either the :meth:`list.sort` built-in method or the related :func:`sorted`
846 function to do sorting (and see the
847 `sorting mini-HOWTO <http://wiki.python.org/moin/HowTo/Sorting>`_ for examples
848 of moderately advanced usage).
849
850* Abstractions tend to create indirections and force the interpreter to work
851 more. If the levels of indirection outweigh the amount of useful work
852 done, your program will be slower. You should avoid excessive abstraction,
853 especially under the form of tiny functions or methods (which are also often
854 detrimental to readability).
855
856If you have reached the limit of what pure Python can allow, there are tools
857to take you further away. For example, `Cython <http://cython.org>`_ can
858compile a slightly modified version of Python code into a C extension, and
859can be used on many different platforms. Cython can take advantage of
860compilation (and optional type annotations) to make your code significantly
861faster than when interpreted. If you are confident in your C programming
862skills, you can also :ref:`write a C extension module <extending-index>`
863yourself.
864
865.. seealso::
866 The wiki page devoted to `performance tips
867 <http://wiki.python.org/moin/PythonSpeed/PerformanceTips>`_.
868
869.. _efficient_string_concatenation:
870
Antoine Pitroufd9ebd42011-11-25 16:33:53 +0100871What is the most efficient way to concatenate many strings together?
872--------------------------------------------------------------------
873
874:class:`str` and :class:`bytes` objects are immutable, therefore concatenating
875many strings together is inefficient as each concatenation creates a new
876object. In the general case, the total runtime cost is quadratic in the
877total string length.
878
879To accumulate many :class:`str` objects, the recommended idiom is to place
880them into a list and call :meth:`str.join` at the end::
881
882 chunks = []
883 for s in my_strings:
884 chunks.append(s)
885 result = ''.join(chunks)
886
887(another reasonably efficient idiom is to use :class:`io.StringIO`)
888
889To accumulate many :class:`bytes` objects, the recommended idiom is to extend
890a :class:`bytearray` object using in-place concatenation (the ``+=`` operator)::
891
892 result = bytearray()
893 for b in my_bytes_objects:
894 result += b
895
896
Georg Brandld7413152009-10-11 21:25:26 +0000897Sequences (Tuples/Lists)
898========================
899
900How do I convert between tuples and lists?
901------------------------------------------
902
903The type constructor ``tuple(seq)`` converts any sequence (actually, any
904iterable) into a tuple with the same items in the same order.
905
906For example, ``tuple([1, 2, 3])`` yields ``(1, 2, 3)`` and ``tuple('abc')``
907yields ``('a', 'b', 'c')``. If the argument is a tuple, it does not make a copy
908but returns the same object, so it is cheap to call :func:`tuple` when you
909aren't sure that an object is already a tuple.
910
911The type constructor ``list(seq)`` converts any sequence or iterable into a list
912with the same items in the same order. For example, ``list((1, 2, 3))`` yields
913``[1, 2, 3]`` and ``list('abc')`` yields ``['a', 'b', 'c']``. If the argument
914is a list, it makes a copy just like ``seq[:]`` would.
915
916
917What's a negative index?
918------------------------
919
920Python sequences are indexed with positive numbers and negative numbers. For
921positive numbers 0 is the first index 1 is the second index and so forth. For
922negative indices -1 is the last index and -2 is the penultimate (next to last)
923index and so forth. Think of ``seq[-n]`` as the same as ``seq[len(seq)-n]``.
924
925Using negative indices can be very convenient. For example ``S[:-1]`` is all of
926the string except for its last character, which is useful for removing the
927trailing newline from a string.
928
929
930How do I iterate over a sequence in reverse order?
931--------------------------------------------------
932
Georg Brandlc4a55fc2010-02-06 18:46:57 +0000933Use the :func:`reversed` built-in function, which is new in Python 2.4::
Georg Brandld7413152009-10-11 21:25:26 +0000934
935 for x in reversed(sequence):
936 ... # do something with x...
937
938This won't touch your original sequence, but build a new copy with reversed
939order to iterate over.
940
941With Python 2.3, you can use an extended slice syntax::
942
943 for x in sequence[::-1]:
944 ... # do something with x...
945
946
947How do you remove duplicates from a list?
948-----------------------------------------
949
950See the Python Cookbook for a long discussion of many ways to do this:
951
952 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
953
954If you don't mind reordering the list, sort it and then scan from the end of the
955list, deleting duplicates as you go::
956
Georg Brandl62eaaf62009-12-19 17:51:41 +0000957 if mylist:
958 mylist.sort()
959 last = mylist[-1]
960 for i in range(len(mylist)-2, -1, -1):
961 if last == mylist[i]:
962 del mylist[i]
Georg Brandld7413152009-10-11 21:25:26 +0000963 else:
Georg Brandl62eaaf62009-12-19 17:51:41 +0000964 last = mylist[i]
Georg Brandld7413152009-10-11 21:25:26 +0000965
Antoine Pitrouf3520402011-12-03 22:19:55 +0100966If all elements of the list may be used as set keys (i.e. they are all
967:term:`hashable`) this is often faster ::
Georg Brandld7413152009-10-11 21:25:26 +0000968
Georg Brandl62eaaf62009-12-19 17:51:41 +0000969 mylist = list(set(mylist))
Georg Brandld7413152009-10-11 21:25:26 +0000970
971This converts the list into a set, thereby removing duplicates, and then back
972into a list.
973
974
975How do you make an array in Python?
976-----------------------------------
977
978Use a list::
979
980 ["this", 1, "is", "an", "array"]
981
982Lists are equivalent to C or Pascal arrays in their time complexity; the primary
983difference is that a Python list can contain objects of many different types.
984
985The ``array`` module also provides methods for creating arrays of fixed types
986with compact representations, but they are slower to index than lists. Also
987note that the Numeric extensions and others define array-like structures with
988various characteristics as well.
989
990To get Lisp-style linked lists, you can emulate cons cells using tuples::
991
992 lisp_list = ("like", ("this", ("example", None) ) )
993
994If mutability is desired, you could use lists instead of tuples. Here the
995analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is
996``lisp_list[1]``. Only do this if you're sure you really need to, because it's
997usually a lot slower than using Python lists.
998
999
1000How do I create a multidimensional list?
1001----------------------------------------
1002
1003You probably tried to make a multidimensional array like this::
1004
1005 A = [[None] * 2] * 3
1006
1007This looks correct if you print it::
1008
1009 >>> A
1010 [[None, None], [None, None], [None, None]]
1011
1012But when you assign a value, it shows up in multiple places:
1013
1014 >>> A[0][0] = 5
1015 >>> A
1016 [[5, None], [5, None], [5, None]]
1017
1018The reason is that replicating a list with ``*`` doesn't create copies, it only
1019creates references to the existing objects. The ``*3`` creates a list
1020containing 3 references to the same list of length two. Changes to one row will
1021show in all rows, which is almost certainly not what you want.
1022
1023The suggested approach is to create a list of the desired length first and then
1024fill in each element with a newly created list::
1025
1026 A = [None] * 3
1027 for i in range(3):
1028 A[i] = [None] * 2
1029
1030This generates a list containing 3 different lists of length two. You can also
1031use a list comprehension::
1032
1033 w, h = 2, 3
1034 A = [[None] * w for i in range(h)]
1035
1036Or, you can use an extension that provides a matrix datatype; `Numeric Python
Georg Brandl495f7b52009-10-27 15:28:25 +00001037<http://numpy.scipy.org/>`_ is the best known.
Georg Brandld7413152009-10-11 21:25:26 +00001038
1039
1040How do I apply a method to a sequence of objects?
1041-------------------------------------------------
1042
1043Use a list comprehension::
1044
Georg Brandl62eaaf62009-12-19 17:51:41 +00001045 result = [obj.method() for obj in mylist]
Georg Brandld7413152009-10-11 21:25:26 +00001046
1047
1048Dictionaries
1049============
1050
1051How can I get a dictionary to display its keys in a consistent order?
1052---------------------------------------------------------------------
1053
1054You can't. Dictionaries store their keys in an unpredictable order, so the
1055display order of a dictionary's elements will be similarly unpredictable.
1056
1057This can be frustrating if you want to save a printable version to a file, make
1058some changes and then compare it with some other printed dictionary. In this
1059case, use the ``pprint`` module to pretty-print the dictionary; the items will
1060be presented in order sorted by the key.
1061
Georg Brandl62eaaf62009-12-19 17:51:41 +00001062A more complicated solution is to subclass ``dict`` to create a
Georg Brandld7413152009-10-11 21:25:26 +00001063``SortedDict`` class that prints itself in a predictable order. Here's one
1064simpleminded implementation of such a class::
1065
Georg Brandl62eaaf62009-12-19 17:51:41 +00001066 class SortedDict(dict):
Georg Brandld7413152009-10-11 21:25:26 +00001067 def __repr__(self):
Georg Brandl62eaaf62009-12-19 17:51:41 +00001068 keys = sorted(self.keys())
1069 result = ("{!r}: {!r}".format(k, self[k]) for k in keys)
1070 return "{{{}}}".format(", ".join(result))
Georg Brandld7413152009-10-11 21:25:26 +00001071
Georg Brandl62eaaf62009-12-19 17:51:41 +00001072 __str__ = __repr__
Georg Brandld7413152009-10-11 21:25:26 +00001073
1074This will work for many common situations you might encounter, though it's far
1075from a perfect solution. The largest flaw is that if some values in the
1076dictionary are also dictionaries, their values won't be presented in any
1077particular order.
1078
1079
1080I want to do a complicated sort: can you do a Schwartzian Transform in Python?
1081------------------------------------------------------------------------------
1082
1083The technique, attributed to Randal Schwartz of the Perl community, sorts the
1084elements of a list by a metric which maps each element to its "sort value". In
1085Python, just use the ``key`` argument for the ``sort()`` method::
1086
1087 Isorted = L[:]
1088 Isorted.sort(key=lambda s: int(s[10:15]))
1089
1090The ``key`` argument is new in Python 2.4, for older versions this kind of
1091sorting is quite simple to do with list comprehensions. To sort a list of
1092strings by their uppercase values::
1093
Georg Brandl62eaaf62009-12-19 17:51:41 +00001094 tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform
Georg Brandld7413152009-10-11 21:25:26 +00001095 tmp1.sort()
1096 Usorted = [x[1] for x in tmp1]
1097
1098To sort by the integer value of a subfield extending from positions 10-15 in
1099each string::
1100
Georg Brandl62eaaf62009-12-19 17:51:41 +00001101 tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform
Georg Brandld7413152009-10-11 21:25:26 +00001102 tmp2.sort()
1103 Isorted = [x[1] for x in tmp2]
1104
Georg Brandl62eaaf62009-12-19 17:51:41 +00001105For versions prior to 3.0, Isorted may also be computed by ::
Georg Brandld7413152009-10-11 21:25:26 +00001106
1107 def intfield(s):
1108 return int(s[10:15])
1109
1110 def Icmp(s1, s2):
1111 return cmp(intfield(s1), intfield(s2))
1112
1113 Isorted = L[:]
1114 Isorted.sort(Icmp)
1115
1116but since this method calls ``intfield()`` many times for each element of L, it
1117is slower than the Schwartzian Transform.
1118
1119
1120How can I sort one list by values from another list?
1121----------------------------------------------------
1122
Georg Brandl62eaaf62009-12-19 17:51:41 +00001123Merge them into an iterator of tuples, sort the resulting list, and then pick
Georg Brandld7413152009-10-11 21:25:26 +00001124out the element you want. ::
1125
1126 >>> list1 = ["what", "I'm", "sorting", "by"]
1127 >>> list2 = ["something", "else", "to", "sort"]
1128 >>> pairs = zip(list1, list2)
Georg Brandl62eaaf62009-12-19 17:51:41 +00001129 >>> pairs = sorted(pairs)
Georg Brandld7413152009-10-11 21:25:26 +00001130 >>> pairs
Georg Brandl62eaaf62009-12-19 17:51:41 +00001131 [("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]
1132 >>> result = [x[1] for x in pairs]
Georg Brandld7413152009-10-11 21:25:26 +00001133 >>> result
1134 ['else', 'sort', 'to', 'something']
1135
Georg Brandl62eaaf62009-12-19 17:51:41 +00001136
Georg Brandld7413152009-10-11 21:25:26 +00001137An alternative for the last step is::
1138
Georg Brandl62eaaf62009-12-19 17:51:41 +00001139 >>> result = []
1140 >>> for p in pairs: result.append(p[1])
Georg Brandld7413152009-10-11 21:25:26 +00001141
1142If you find this more legible, you might prefer to use this instead of the final
1143list comprehension. However, it is almost twice as slow for long lists. Why?
1144First, the ``append()`` operation has to reallocate memory, and while it uses
1145some tricks to avoid doing that each time, it still has to do it occasionally,
1146and that costs quite a bit. Second, the expression "result.append" requires an
1147extra attribute lookup, and third, there's a speed reduction from having to make
1148all those function calls.
1149
1150
1151Objects
1152=======
1153
1154What is a class?
1155----------------
1156
1157A class is the particular object type created by executing a class statement.
1158Class objects are used as templates to create instance objects, which embody
1159both the data (attributes) and code (methods) specific to a datatype.
1160
1161A class can be based on one or more other classes, called its base class(es). It
1162then inherits the attributes and methods of its base classes. This allows an
1163object model to be successively refined by inheritance. You might have a
1164generic ``Mailbox`` class that provides basic accessor methods for a mailbox,
1165and subclasses such as ``MboxMailbox``, ``MaildirMailbox``, ``OutlookMailbox``
1166that handle various specific mailbox formats.
1167
1168
1169What is a method?
1170-----------------
1171
1172A method is a function on some object ``x`` that you normally call as
1173``x.name(arguments...)``. Methods are defined as functions inside the class
1174definition::
1175
1176 class C:
1177 def meth (self, arg):
1178 return arg * 2 + self.attribute
1179
1180
1181What is self?
1182-------------
1183
1184Self is merely a conventional name for the first argument of a method. A method
1185defined as ``meth(self, a, b, c)`` should be called as ``x.meth(a, b, c)`` for
1186some instance ``x`` of the class in which the definition occurs; the called
1187method will think it is called as ``meth(x, a, b, c)``.
1188
1189See also :ref:`why-self`.
1190
1191
1192How do I check if an object is an instance of a given class or of a subclass of it?
1193-----------------------------------------------------------------------------------
1194
1195Use the built-in function ``isinstance(obj, cls)``. You can check if an object
1196is an instance of any of a number of classes by providing a tuple instead of a
1197single class, e.g. ``isinstance(obj, (class1, class2, ...))``, and can also
1198check whether an object is one of Python's built-in types, e.g.
Georg Brandl62eaaf62009-12-19 17:51:41 +00001199``isinstance(obj, str)`` or ``isinstance(obj, (int, float, complex))``.
Georg Brandld7413152009-10-11 21:25:26 +00001200
1201Note that most programs do not use :func:`isinstance` on user-defined classes
1202very often. If you are developing the classes yourself, a more proper
1203object-oriented style is to define methods on the classes that encapsulate a
1204particular behaviour, instead of checking the object's class and doing a
1205different thing based on what class it is. For example, if you have a function
1206that does something::
1207
Georg Brandl62eaaf62009-12-19 17:51:41 +00001208 def search(obj):
Georg Brandld7413152009-10-11 21:25:26 +00001209 if isinstance(obj, Mailbox):
1210 # ... code to search a mailbox
1211 elif isinstance(obj, Document):
1212 # ... code to search a document
1213 elif ...
1214
1215A better approach is to define a ``search()`` method on all the classes and just
1216call it::
1217
1218 class Mailbox:
1219 def search(self):
1220 # ... code to search a mailbox
1221
1222 class Document:
1223 def search(self):
1224 # ... code to search a document
1225
1226 obj.search()
1227
1228
1229What is delegation?
1230-------------------
1231
1232Delegation is an object oriented technique (also called a design pattern).
1233Let's say you have an object ``x`` and want to change the behaviour of just one
1234of its methods. You can create a new class that provides a new implementation
1235of the method you're interested in changing and delegates all other methods to
1236the corresponding method of ``x``.
1237
1238Python programmers can easily implement delegation. For example, the following
1239class implements a class that behaves like a file but converts all written data
1240to uppercase::
1241
1242 class UpperOut:
1243
1244 def __init__(self, outfile):
1245 self._outfile = outfile
1246
1247 def write(self, s):
1248 self._outfile.write(s.upper())
1249
1250 def __getattr__(self, name):
1251 return getattr(self._outfile, name)
1252
1253Here the ``UpperOut`` class redefines the ``write()`` method to convert the
1254argument string to uppercase before calling the underlying
1255``self.__outfile.write()`` method. All other methods are delegated to the
1256underlying ``self.__outfile`` object. The delegation is accomplished via the
1257``__getattr__`` method; consult :ref:`the language reference <attribute-access>`
1258for more information about controlling attribute access.
1259
1260Note that for more general cases delegation can get trickier. When attributes
1261must be set as well as retrieved, the class must define a :meth:`__setattr__`
1262method too, and it must do so carefully. The basic implementation of
1263:meth:`__setattr__` is roughly equivalent to the following::
1264
1265 class X:
1266 ...
1267 def __setattr__(self, name, value):
1268 self.__dict__[name] = value
1269 ...
1270
1271Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store
1272local state for self without causing an infinite recursion.
1273
1274
1275How do I call a method defined in a base class from a derived class that overrides it?
1276--------------------------------------------------------------------------------------
1277
Georg Brandl62eaaf62009-12-19 17:51:41 +00001278Use the built-in :func:`super` function::
Georg Brandld7413152009-10-11 21:25:26 +00001279
1280 class Derived(Base):
1281 def meth (self):
1282 super(Derived, self).meth()
1283
Georg Brandl62eaaf62009-12-19 17:51:41 +00001284For version prior to 3.0, you may be using classic classes: For a class
1285definition such as ``class Derived(Base): ...`` you can call method ``meth()``
1286defined in ``Base`` (or one of ``Base``'s base classes) as ``Base.meth(self,
1287arguments...)``. Here, ``Base.meth`` is an unbound method, so you need to
1288provide the ``self`` argument.
Georg Brandld7413152009-10-11 21:25:26 +00001289
1290
1291How can I organize my code to make it easier to change the base class?
1292----------------------------------------------------------------------
1293
1294You could define an alias for the base class, assign the real base class to it
1295before your class definition, and use the alias throughout your class. Then all
1296you have to change is the value assigned to the alias. Incidentally, this trick
1297is also handy if you want to decide dynamically (e.g. depending on availability
1298of resources) which base class to use. Example::
1299
1300 BaseAlias = <real base class>
1301
1302 class Derived(BaseAlias):
1303 def meth(self):
1304 BaseAlias.meth(self)
1305 ...
1306
1307
1308How do I create static class data and static class methods?
1309-----------------------------------------------------------
1310
Georg Brandl62eaaf62009-12-19 17:51:41 +00001311Both static data and static methods (in the sense of C++ or Java) are supported
1312in Python.
Georg Brandld7413152009-10-11 21:25:26 +00001313
1314For static data, simply define a class attribute. To assign a new value to the
1315attribute, you have to explicitly use the class name in the assignment::
1316
1317 class C:
1318 count = 0 # number of times C.__init__ called
1319
1320 def __init__(self):
1321 C.count = C.count + 1
1322
1323 def getcount(self):
1324 return C.count # or return self.count
1325
1326``c.count`` also refers to ``C.count`` for any ``c`` such that ``isinstance(c,
1327C)`` holds, unless overridden by ``c`` itself or by some class on the base-class
1328search path from ``c.__class__`` back to ``C``.
1329
1330Caution: within a method of C, an assignment like ``self.count = 42`` creates a
Georg Brandl62eaaf62009-12-19 17:51:41 +00001331new and unrelated instance named "count" in ``self``'s own dict. Rebinding of a
1332class-static data name must always specify the class whether inside a method or
1333not::
Georg Brandld7413152009-10-11 21:25:26 +00001334
1335 C.count = 314
1336
Antoine Pitrouf3520402011-12-03 22:19:55 +01001337Static methods are possible::
Georg Brandld7413152009-10-11 21:25:26 +00001338
1339 class C:
1340 @staticmethod
1341 def static(arg1, arg2, arg3):
1342 # No 'self' parameter!
1343 ...
1344
1345However, a far more straightforward way to get the effect of a static method is
1346via a simple module-level function::
1347
1348 def getcount():
1349 return C.count
1350
1351If your code is structured so as to define one class (or tightly related class
1352hierarchy) per module, this supplies the desired encapsulation.
1353
1354
1355How can I overload constructors (or methods) in Python?
1356-------------------------------------------------------
1357
1358This answer actually applies to all methods, but the question usually comes up
1359first in the context of constructors.
1360
1361In C++ you'd write
1362
1363.. code-block:: c
1364
1365 class C {
1366 C() { cout << "No arguments\n"; }
1367 C(int i) { cout << "Argument is " << i << "\n"; }
1368 }
1369
1370In Python you have to write a single constructor that catches all cases using
1371default arguments. For example::
1372
1373 class C:
1374 def __init__(self, i=None):
1375 if i is None:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001376 print("No arguments")
Georg Brandld7413152009-10-11 21:25:26 +00001377 else:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001378 print("Argument is", i)
Georg Brandld7413152009-10-11 21:25:26 +00001379
1380This is not entirely equivalent, but close enough in practice.
1381
1382You could also try a variable-length argument list, e.g. ::
1383
1384 def __init__(self, *args):
1385 ...
1386
1387The same approach works for all method definitions.
1388
1389
1390I try to use __spam and I get an error about _SomeClassName__spam.
1391------------------------------------------------------------------
1392
1393Variable names with double leading underscores are "mangled" to provide a simple
1394but effective way to define class private variables. Any identifier of the form
1395``__spam`` (at least two leading underscores, at most one trailing underscore)
1396is textually replaced with ``_classname__spam``, where ``classname`` is the
1397current class name with any leading underscores stripped.
1398
1399This doesn't guarantee privacy: an outside user can still deliberately access
1400the "_classname__spam" attribute, and private values are visible in the object's
1401``__dict__``. Many Python programmers never bother to use private variable
1402names at all.
1403
1404
1405My class defines __del__ but it is not called when I delete the object.
1406-----------------------------------------------------------------------
1407
1408There are several possible reasons for this.
1409
1410The del statement does not necessarily call :meth:`__del__` -- it simply
1411decrements the object's reference count, and if this reaches zero
1412:meth:`__del__` is called.
1413
1414If your data structures contain circular links (e.g. a tree where each child has
1415a parent reference and each parent has a list of children) the reference counts
1416will never go back to zero. Once in a while Python runs an algorithm to detect
1417such cycles, but the garbage collector might run some time after the last
1418reference to your data structure vanishes, so your :meth:`__del__` method may be
1419called at an inconvenient and random time. This is inconvenient if you're trying
1420to reproduce a problem. Worse, the order in which object's :meth:`__del__`
1421methods are executed is arbitrary. You can run :func:`gc.collect` to force a
1422collection, but there *are* pathological cases where objects will never be
1423collected.
1424
1425Despite the cycle collector, it's still a good idea to define an explicit
1426``close()`` method on objects to be called whenever you're done with them. The
1427``close()`` method can then remove attributes that refer to subobjecs. Don't
1428call :meth:`__del__` directly -- :meth:`__del__` should call ``close()`` and
1429``close()`` should make sure that it can be called more than once for the same
1430object.
1431
1432Another way to avoid cyclical references is to use the :mod:`weakref` module,
1433which allows you to point to objects without incrementing their reference count.
1434Tree data structures, for instance, should use weak references for their parent
1435and sibling references (if they need them!).
1436
Georg Brandl62eaaf62009-12-19 17:51:41 +00001437.. XXX relevant for Python 3?
1438
1439 If the object has ever been a local variable in a function that caught an
1440 expression in an except clause, chances are that a reference to the object
1441 still exists in that function's stack frame as contained in the stack trace.
1442 Normally, calling :func:`sys.exc_clear` will take care of this by clearing
1443 the last recorded exception.
Georg Brandld7413152009-10-11 21:25:26 +00001444
1445Finally, if your :meth:`__del__` method raises an exception, a warning message
1446is printed to :data:`sys.stderr`.
1447
1448
1449How do I get a list of all instances of a given class?
1450------------------------------------------------------
1451
1452Python does not keep track of all instances of a class (or of a built-in type).
1453You can program the class's constructor to keep track of all instances by
1454keeping a list of weak references to each instance.
1455
1456
1457Modules
1458=======
1459
1460How do I create a .pyc file?
1461----------------------------
1462
1463When a module is imported for the first time (or when the source is more recent
1464than the current compiled file) a ``.pyc`` file containing the compiled code
1465should be created in the same directory as the ``.py`` file.
1466
1467One reason that a ``.pyc`` file may not be created is permissions problems with
1468the directory. This can happen, for example, if you develop as one user but run
1469as another, such as if you are testing with a web server. Creation of a .pyc
1470file is automatic if you're importing a module and Python has the ability
1471(permissions, free space, etc...) to write the compiled module back to the
1472directory.
1473
1474Running Python on a top level script is not considered an import and no ``.pyc``
1475will be created. For example, if you have a top-level module ``abc.py`` that
1476imports another module ``xyz.py``, when you run abc, ``xyz.pyc`` will be created
1477since xyz is imported, but no ``abc.pyc`` file will be created since ``abc.py``
1478isn't being imported.
1479
1480If you need to create abc.pyc -- that is, to create a .pyc file for a module
1481that is not imported -- you can, using the :mod:`py_compile` and
1482:mod:`compileall` modules.
1483
1484The :mod:`py_compile` module can manually compile any module. One way is to use
1485the ``compile()`` function in that module interactively::
1486
1487 >>> import py_compile
1488 >>> py_compile.compile('abc.py')
1489
1490This will write the ``.pyc`` to the same location as ``abc.py`` (or you can
1491override that with the optional parameter ``cfile``).
1492
1493You can also automatically compile all files in a directory or directories using
1494the :mod:`compileall` module. You can do it from the shell prompt by running
1495``compileall.py`` and providing the path of a directory containing Python files
1496to compile::
1497
1498 python -m compileall .
1499
1500
1501How do I find the current module name?
1502--------------------------------------
1503
1504A module can find out its own module name by looking at the predefined global
1505variable ``__name__``. If this has the value ``'__main__'``, the program is
1506running as a script. Many modules that are usually used by importing them also
1507provide a command-line interface or a self-test, and only execute this code
1508after checking ``__name__``::
1509
1510 def main():
Georg Brandl62eaaf62009-12-19 17:51:41 +00001511 print('Running test...')
Georg Brandld7413152009-10-11 21:25:26 +00001512 ...
1513
1514 if __name__ == '__main__':
1515 main()
1516
1517
1518How can I have modules that mutually import each other?
1519-------------------------------------------------------
1520
1521Suppose you have the following modules:
1522
1523foo.py::
1524
1525 from bar import bar_var
1526 foo_var = 1
1527
1528bar.py::
1529
1530 from foo import foo_var
1531 bar_var = 2
1532
1533The problem is that the interpreter will perform the following steps:
1534
1535* main imports foo
1536* Empty globals for foo are created
1537* foo is compiled and starts executing
1538* foo imports bar
1539* Empty globals for bar are created
1540* bar is compiled and starts executing
1541* bar imports foo (which is a no-op since there already is a module named foo)
1542* bar.foo_var = foo.foo_var
1543
1544The last step fails, because Python isn't done with interpreting ``foo`` yet and
1545the global symbol dictionary for ``foo`` is still empty.
1546
1547The same thing happens when you use ``import foo``, and then try to access
1548``foo.foo_var`` in global code.
1549
1550There are (at least) three possible workarounds for this problem.
1551
1552Guido van Rossum recommends avoiding all uses of ``from <module> import ...``,
1553and placing all code inside functions. Initializations of global variables and
1554class variables should use constants or built-in functions only. This means
1555everything from an imported module is referenced as ``<module>.<name>``.
1556
1557Jim Roskind suggests performing steps in the following order in each module:
1558
1559* exports (globals, functions, and classes that don't need imported base
1560 classes)
1561* ``import`` statements
1562* active code (including globals that are initialized from imported values).
1563
1564van Rossum doesn't like this approach much because the imports appear in a
1565strange place, but it does work.
1566
1567Matthias Urlichs recommends restructuring your code so that the recursive import
1568is not necessary in the first place.
1569
1570These solutions are not mutually exclusive.
1571
1572
1573__import__('x.y.z') returns <module 'x'>; how do I get z?
1574---------------------------------------------------------
1575
1576Try::
1577
1578 __import__('x.y.z').y.z
1579
1580For more realistic situations, you may have to do something like ::
1581
1582 m = __import__(s)
1583 for i in s.split(".")[1:]:
1584 m = getattr(m, i)
1585
1586See :mod:`importlib` for a convenience function called
1587:func:`~importlib.import_module`.
1588
1589
1590
1591When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
1592-------------------------------------------------------------------------------------------------
1593
1594For reasons of efficiency as well as consistency, Python only reads the module
1595file on the first time a module is imported. If it didn't, in a program
1596consisting of many modules where each one imports the same basic module, the
1597basic module would be parsed and re-parsed many times. To force rereading of a
1598changed module, do this::
1599
Georg Brandl62eaaf62009-12-19 17:51:41 +00001600 import imp
Georg Brandld7413152009-10-11 21:25:26 +00001601 import modname
Georg Brandl62eaaf62009-12-19 17:51:41 +00001602 imp.reload(modname)
Georg Brandld7413152009-10-11 21:25:26 +00001603
1604Warning: this technique is not 100% fool-proof. In particular, modules
1605containing statements like ::
1606
1607 from modname import some_objects
1608
1609will continue to work with the old version of the imported objects. If the
1610module contains class definitions, existing class instances will *not* be
1611updated to use the new class definition. This can result in the following
1612paradoxical behaviour:
1613
Georg Brandl62eaaf62009-12-19 17:51:41 +00001614 >>> import imp
Georg Brandld7413152009-10-11 21:25:26 +00001615 >>> import cls
1616 >>> c = cls.C() # Create an instance of C
Georg Brandl62eaaf62009-12-19 17:51:41 +00001617 >>> imp.reload(cls)
1618 <module 'cls' from 'cls.py'>
Georg Brandld7413152009-10-11 21:25:26 +00001619 >>> isinstance(c, cls.C) # isinstance is false?!?
1620 False
1621
Georg Brandl62eaaf62009-12-19 17:51:41 +00001622The nature of the problem is made clear if you print out the "identity" of the
1623class objects:
Georg Brandld7413152009-10-11 21:25:26 +00001624
Georg Brandl62eaaf62009-12-19 17:51:41 +00001625 >>> hex(id(c.__class__))
1626 '0x7352a0'
1627 >>> hex(id(cls.C))
1628 '0x4198d0'