blob: e72ee2a3f59f87f6296aa16269e02115e8255b67 [file] [log] [blame]
Georg Brandl5d2eb342009-10-27 15:08:27 +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.
70http://www.logilab.org/card/pylint_manual provides a full list of Pylint's
71features.
72
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
118My program is too slow. How do I speed it up?
119---------------------------------------------
120
121That's a tough one, in general. There are many tricks to speed up Python code;
122consider rewriting parts in C as a last resort.
123
124In some cases it's possible to automatically translate Python to C or x86
125assembly language, meaning that you don't have to modify your code to gain
126increased speed.
127
128.. XXX seems to have overlap with other questions!
129
130`Pyrex <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_ can compile a
131slightly modified version of Python code into a C extension, and can be used on
132many different platforms.
133
134`Psyco <http://psyco.sourceforge.net>`_ is a just-in-time compiler that
135translates Python code into x86 assembly language. If you can use it, Psyco can
136provide dramatic speedups for critical functions.
137
138The rest of this answer will discuss various tricks for squeezing a bit more
139speed out of Python code. *Never* apply any optimization tricks unless you know
140you need them, after profiling has indicated that a particular function is the
141heavily executed hot spot in the code. Optimizations almost always make the
142code less clear, and you shouldn't pay the costs of reduced clarity (increased
143development time, greater likelihood of bugs) unless the resulting performance
144benefit is worth it.
145
146There is a page on the wiki devoted to `performance tips
147<http://wiki.python.org/moin/PythonSpeed/PerformanceTips>`_.
148
149Guido van Rossum has written up an anecdote related to optimization at
150http://www.python.org/doc/essays/list2str.html.
151
152One thing to notice is that function and (especially) method calls are rather
153expensive; if you have designed a purely OO interface with lots of tiny
154functions that don't do much more than get or set an instance variable or call
155another method, you might consider using a more direct way such as directly
156accessing instance variables. Also see the standard module :mod:`profile` which
157makes it possible to find out where your program is spending most of its time
158(if you have some patience -- the profiling itself can slow your program down by
159an order of magnitude).
160
161Remember that many standard optimization heuristics you may know from other
162programming experience may well apply to Python. For example it may be faster
163to send output to output devices using larger writes rather than smaller ones in
164order to reduce the overhead of kernel system calls. Thus CGI scripts that
165write all output in "one shot" may be faster than those that write lots of small
166pieces of output.
167
168Also, be sure to use Python's core features where appropriate. For example,
169slicing allows programs to chop up lists and other sequence objects in a single
170tick of the interpreter's mainloop using highly optimized C implementations.
171Thus to get the same effect as::
172
173 L2 = []
174 for i in range[3]:
175 L2.append(L1[i])
176
177it is much shorter and far faster to use ::
178
179 L2 = list(L1[:3]) # "list" is redundant if L1 is a list.
180
Georg Brandl9c2c1f82010-02-07 12:01:57 +0000181Note that the functionally-oriented built-in functions such as :func:`map`,
182:func:`zip`, and friends can be a convenient accelerator for loops that
183perform a single task. For example to pair the elements of two lists
184together::
Georg Brandl5d2eb342009-10-27 15:08:27 +0000185
186 >>> zip([1,2,3], [4,5,6])
187 [(1, 4), (2, 5), (3, 6)]
188
189or to compute a number of sines::
190
191 >>> map( math.sin, (1,2,3,4))
192 [0.841470984808, 0.909297426826, 0.14112000806, -0.756802495308]
193
194The operation completes very quickly in such cases.
195
196Other examples include the ``join()`` and ``split()`` methods of string objects.
197For example if s1..s7 are large (10K+) strings then
198``"".join([s1,s2,s3,s4,s5,s6,s7])`` may be far faster than the more obvious
199``s1+s2+s3+s4+s5+s6+s7``, since the "summation" will compute many
200subexpressions, whereas ``join()`` does all the copying in one pass. For
201manipulating strings, use the ``replace()`` method on string objects. Use
202regular expressions only when you're not dealing with constant string patterns.
203Consider using the string formatting operations ``string % tuple`` and ``string
204% dictionary``.
205
Georg Brandl9c2c1f82010-02-07 12:01:57 +0000206Be sure to use the :meth:`list.sort` built-in method to do sorting, and see the
Georg Brandl5d2eb342009-10-27 15:08:27 +0000207`sorting mini-HOWTO <http://wiki.python.org/moin/HowTo/Sorting>`_ for examples
208of moderately advanced usage. :meth:`list.sort` beats other techniques for
209sorting in all but the most extreme circumstances.
210
211Another common trick is to "push loops into functions or methods." For example
212suppose you have a program that runs slowly and you use the profiler to
213determine that a Python function ``ff()`` is being called lots of times. If you
214notice that ``ff ()``::
215
216 def ff(x):
217 ... # do something with x computing result...
218 return result
219
220tends to be called in loops like::
221
222 list = map(ff, oldlist)
223
224or::
225
226 for x in sequence:
227 value = ff(x)
228 ... # do something with value...
229
230then you can often eliminate function call overhead by rewriting ``ff()`` to::
231
232 def ffseq(seq):
233 resultseq = []
234 for x in seq:
235 ... # do something with x computing result...
236 resultseq.append(result)
237 return resultseq
238
239and rewrite the two examples to ``list = ffseq(oldlist)`` and to::
240
241 for value in ffseq(sequence):
242 ... # do something with value...
243
244Single calls to ``ff(x)`` translate to ``ffseq([x])[0]`` with little penalty.
245Of course this technique is not always appropriate and there are other variants
246which you can figure out.
247
248You can gain some performance by explicitly storing the results of a function or
249method lookup into a local variable. A loop like::
250
251 for key in token:
252 dict[key] = dict.get(key, 0) + 1
253
254resolves ``dict.get`` every iteration. If the method isn't going to change, a
255slightly faster implementation is::
256
257 dict_get = dict.get # look up the method once
258 for key in token:
259 dict[key] = dict_get(key, 0) + 1
260
261Default arguments can be used to determine values once, at compile time instead
262of at run time. This can only be done for functions or objects which will not
263be changed during program execution, such as replacing ::
264
265 def degree_sin(deg):
266 return math.sin(deg * math.pi / 180.0)
267
268with ::
269
270 def degree_sin(deg, factor=math.pi/180.0, sin=math.sin):
271 return sin(deg * factor)
272
273Because this trick uses default arguments for terms which should not be changed,
274it should only be used when you are not concerned with presenting a possibly
275confusing API to your users.
276
277
278Core Language
279=============
280
R. David Murray384a4fc2009-11-14 22:08:02 +0000281Why am I getting an UnboundLocalError when the variable has a value?
282--------------------------------------------------------------------
Georg Brandl5d2eb342009-10-27 15:08:27 +0000283
R. David Murray384a4fc2009-11-14 22:08:02 +0000284It can be a surprise to get the UnboundLocalError in previously working
285code when it is modified by adding an assignment statement somewhere in
286the body of a function.
Georg Brandl5d2eb342009-10-27 15:08:27 +0000287
R. David Murray384a4fc2009-11-14 22:08:02 +0000288This code:
Georg Brandl5d2eb342009-10-27 15:08:27 +0000289
R. David Murray384a4fc2009-11-14 22:08:02 +0000290 >>> x = 10
291 >>> def bar():
292 ... print x
293 >>> bar()
294 10
Georg Brandl5d2eb342009-10-27 15:08:27 +0000295
R. David Murray384a4fc2009-11-14 22:08:02 +0000296works, but this code:
Georg Brandl5d2eb342009-10-27 15:08:27 +0000297
R. David Murray384a4fc2009-11-14 22:08:02 +0000298 >>> x = 10
299 >>> def foo():
300 ... print x
301 ... x += 1
Georg Brandl5d2eb342009-10-27 15:08:27 +0000302
R. David Murray384a4fc2009-11-14 22:08:02 +0000303results in an UnboundLocalError:
Georg Brandl5d2eb342009-10-27 15:08:27 +0000304
R. David Murray384a4fc2009-11-14 22:08:02 +0000305 >>> foo()
306 Traceback (most recent call last):
307 ...
308 UnboundLocalError: local variable 'x' referenced before assignment
309
310This is because when you make an assignment to a variable in a scope, that
311variable becomes local to that scope and shadows any similarly named variable
312in the outer scope. Since the last statement in foo assigns a new value to
313``x``, the compiler recognizes it as a local variable. Consequently when the
314earlier ``print x`` attempts to print the uninitialized local variable and
315an error results.
316
317In the example above you can access the outer scope variable by declaring it
318global:
319
320 >>> x = 10
321 >>> def foobar():
322 ... global x
323 ... print x
324 ... x += 1
325 >>> foobar()
326 10
327
328This explicit declaration is required in order to remind you that (unlike the
329superficially analogous situation with class and instance variables) you are
330actually modifying the value of the variable in the outer scope:
331
332 >>> print x
333 11
334
335In Python3, you can do a similar thing in a nested scope using the
336:keyword:`nonlocal` keyword:
337
338.. doctest::
339 :options: +SKIP
340
341 >>> def foo():
342 ... x = 10
343 ... def bar():
344 ... nonlocal x
345 ... print x
346 ... x += 1
347 ... bar()
348 ... print x
349 >>> foo()
350 10
351 11
Georg Brandl5d2eb342009-10-27 15:08:27 +0000352
353
354What are the rules for local and global variables in Python?
355------------------------------------------------------------
356
357In Python, variables that are only referenced inside a function are implicitly
358global. If a variable is assigned a new value anywhere within the function's
359body, it's assumed to be a local. If a variable is ever assigned a new value
360inside the function, the variable is implicitly local, and you need to
361explicitly declare it as 'global'.
362
363Though a bit surprising at first, a moment's consideration explains this. On
364one hand, requiring :keyword:`global` for assigned variables provides a bar
365against unintended side-effects. On the other hand, if ``global`` was required
366for all global references, you'd be using ``global`` all the time. You'd have
Georg Brandl9c2c1f82010-02-07 12:01:57 +0000367to declare as global every reference to a built-in function or to a component of
Georg Brandl5d2eb342009-10-27 15:08:27 +0000368an imported module. This clutter would defeat the usefulness of the ``global``
369declaration for identifying side-effects.
370
371
372How do I share global variables across modules?
373------------------------------------------------
374
375The canonical way to share information across modules within a single program is
376to create a special module (often called config or cfg). Just import the config
377module in all modules of your application; the module then becomes available as
378a global name. Because there is only one instance of each module, any changes
379made to the module object get reflected everywhere. For example:
380
381config.py::
382
383 x = 0 # Default value of the 'x' configuration setting
384
385mod.py::
386
387 import config
388 config.x = 1
389
390main.py::
391
392 import config
393 import mod
394 print config.x
395
396Note that using a module is also the basis for implementing the Singleton design
397pattern, for the same reason.
398
399
400What are the "best practices" for using import in a module?
401-----------------------------------------------------------
402
403In general, don't use ``from modulename import *``. Doing so clutters the
404importer's namespace. Some people avoid this idiom even with the few modules
405that were designed to be imported in this manner. Modules designed in this
406manner include :mod:`Tkinter`, and :mod:`threading`.
407
408Import modules at the top of a file. Doing so makes it clear what other modules
409your code requires and avoids questions of whether the module name is in scope.
410Using one import per line makes it easy to add and delete module imports, but
411using multiple imports per line uses less screen space.
412
413It's good practice if you import modules in the following order:
414
4151. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re``)
4162. third-party library modules (anything installed in Python's site-packages
417 directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc.
4183. locally-developed modules
419
420Never use relative package imports. If you're writing code that's in the
421``package.sub.m1`` module and want to import ``package.sub.m2``, do not just
422write ``import m2``, even though it's legal. Write ``from package.sub import
423m2`` instead. Relative imports can lead to a module being initialized twice,
424leading to confusing bugs.
425
426It is sometimes necessary to move imports to a function or class to avoid
427problems with circular imports. Gordon McMillan says:
428
429 Circular imports are fine where both modules use the "import <module>" form
430 of import. They fail when the 2nd module wants to grab a name out of the
431 first ("from module import name") and the import is at the top level. That's
432 because names in the 1st are not yet available, because the first module is
433 busy importing the 2nd.
434
435In this case, if the second module is only used in one function, then the import
436can easily be moved into that function. By the time the import is called, the
437first module will have finished initializing, and the second module can do its
438import.
439
440It may also be necessary to move imports out of the top level of code if some of
441the modules are platform-specific. In that case, it may not even be possible to
442import all of the modules at the top of the file. In this case, importing the
443correct modules in the corresponding platform-specific code is a good option.
444
445Only move imports into a local scope, such as inside a function definition, if
446it's necessary to solve a problem such as avoiding a circular import or are
447trying to reduce the initialization time of a module. This technique is
448especially helpful if many of the imports are unnecessary depending on how the
449program executes. You may also want to move imports into a function if the
450modules are only ever used in that function. Note that loading a module the
451first time may be expensive because of the one time initialization of the
452module, but loading a module multiple times is virtually free, costing only a
453couple of dictionary lookups. Even if the module name has gone out of scope,
454the module is probably available in :data:`sys.modules`.
455
456If only instances of a specific class use a module, then it is reasonable to
457import the module in the class's ``__init__`` method and then assign the module
458to an instance variable so that the module is always available (via that
459instance variable) during the life of the object. Note that to delay an import
460until the class is instantiated, the import must be inside a method. Putting
461the import inside the class but outside of any method still causes the import to
462occur when the module is initialized.
463
464
465How can I pass optional or keyword parameters from one function to another?
466---------------------------------------------------------------------------
467
468Collect the arguments using the ``*`` and ``**`` specifiers in the function's
469parameter list; this gives you the positional arguments as a tuple and the
470keyword arguments as a dictionary. You can then pass these arguments when
471calling another function by using ``*`` and ``**``::
472
473 def f(x, *args, **kwargs):
474 ...
475 kwargs['width'] = '14.3c'
476 ...
477 g(x, *args, **kwargs)
478
479In the unlikely case that you care about Python versions older than 2.0, use
480:func:`apply`::
481
482 def f(x, *args, **kwargs):
483 ...
484 kwargs['width'] = '14.3c'
485 ...
486 apply(g, (x,)+args, kwargs)
487
488
489How do I write a function with output parameters (call by reference)?
490---------------------------------------------------------------------
491
492Remember that arguments are passed by assignment in Python. Since assignment
493just creates references to objects, there's no alias between an argument name in
494the caller and callee, and so no call-by-reference per se. You can achieve the
495desired effect in a number of ways.
496
4971) By returning a tuple of the results::
498
499 def func2(a, b):
500 a = 'new-value' # a and b are local names
501 b = b + 1 # assigned to new objects
502 return a, b # return new values
503
504 x, y = 'old-value', 99
505 x, y = func2(x, y)
506 print x, y # output: new-value 100
507
508 This is almost always the clearest solution.
509
5102) By using global variables. This isn't thread-safe, and is not recommended.
511
5123) By passing a mutable (changeable in-place) object::
513
514 def func1(a):
515 a[0] = 'new-value' # 'a' references a mutable list
516 a[1] = a[1] + 1 # changes a shared object
517
518 args = ['old-value', 99]
519 func1(args)
520 print args[0], args[1] # output: new-value 100
521
5224) By passing in a dictionary that gets mutated::
523
524 def func3(args):
525 args['a'] = 'new-value' # args is a mutable dictionary
526 args['b'] = args['b'] + 1 # change it in-place
527
528 args = {'a':' old-value', 'b': 99}
529 func3(args)
530 print args['a'], args['b']
531
5325) Or bundle up values in a class instance::
533
534 class callByRef:
535 def __init__(self, **args):
536 for (key, value) in args.items():
537 setattr(self, key, value)
538
539 def func4(args):
540 args.a = 'new-value' # args is a mutable callByRef
541 args.b = args.b + 1 # change object in-place
542
543 args = callByRef(a='old-value', b=99)
544 func4(args)
545 print args.a, args.b
546
547
548 There's almost never a good reason to get this complicated.
549
550Your best choice is to return a tuple containing the multiple results.
551
552
553How do you make a higher order function in Python?
554--------------------------------------------------
555
556You have two choices: you can use nested scopes or you can use callable objects.
557For example, suppose you wanted to define ``linear(a,b)`` which returns a
558function ``f(x)`` that computes the value ``a*x+b``. Using nested scopes::
559
560 def linear(a, b):
561 def result(x):
562 return a * x + b
563 return result
564
565Or using a callable object::
566
567 class linear:
568
569 def __init__(self, a, b):
570 self.a, self.b = a, b
571
572 def __call__(self, x):
573 return self.a * x + self.b
574
575In both cases, ::
576
577 taxes = linear(0.3, 2)
578
579gives a callable object where ``taxes(10e6) == 0.3 * 10e6 + 2``.
580
581The callable object approach has the disadvantage that it is a bit slower and
582results in slightly longer code. However, note that a collection of callables
583can share their signature via inheritance::
584
585 class exponential(linear):
586 # __init__ inherited
587 def __call__(self, x):
588 return self.a * (x ** self.b)
589
590Object can encapsulate state for several methods::
591
592 class counter:
593
594 value = 0
595
596 def set(self, x):
597 self.value = x
598
599 def up(self):
600 self.value = self.value + 1
601
602 def down(self):
603 self.value = self.value - 1
604
605 count = counter()
606 inc, dec, reset = count.up, count.down, count.set
607
608Here ``inc()``, ``dec()`` and ``reset()`` act like functions which share the
609same counting variable.
610
611
612How do I copy an object in Python?
613----------------------------------
614
615In general, try :func:`copy.copy` or :func:`copy.deepcopy` for the general case.
616Not all objects can be copied, but most can.
617
618Some objects can be copied more easily. Dictionaries have a :meth:`~dict.copy`
619method::
620
621 newdict = olddict.copy()
622
623Sequences can be copied by slicing::
624
625 new_l = l[:]
626
627
628How can I find the methods or attributes of an object?
629------------------------------------------------------
630
631For an instance x of a user-defined class, ``dir(x)`` returns an alphabetized
632list of the names containing the instance attributes and methods and attributes
633defined by its class.
634
635
636How can my code discover the name of an object?
637-----------------------------------------------
638
639Generally speaking, it can't, because objects don't really have names.
640Essentially, assignment always binds a name to a value; The same is true of
641``def`` and ``class`` statements, but in that case the value is a
642callable. Consider the following code::
643
644 class A:
645 pass
646
647 B = A
648
649 a = B()
650 b = a
651 print b
652 <__main__.A instance at 016D07CC>
653 print a
654 <__main__.A instance at 016D07CC>
655
656Arguably the class has a name: even though it is bound to two names and invoked
657through the name B the created instance is still reported as an instance of
658class A. However, it is impossible to say whether the instance's name is a or
659b, since both names are bound to the same value.
660
661Generally speaking it should not be necessary for your code to "know the names"
662of particular values. Unless you are deliberately writing introspective
663programs, this is usually an indication that a change of approach might be
664beneficial.
665
666In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to
667this question:
668
669 The same way as you get the name of that cat you found on your porch: the cat
670 (object) itself cannot tell you its name, and it doesn't really care -- so
671 the only way to find out what it's called is to ask all your neighbours
672 (namespaces) if it's their cat (object)...
673
674 ....and don't be surprised if you'll find that it's known by many names, or
675 no name at all!
676
677
678What's up with the comma operator's precedence?
679-----------------------------------------------
680
681Comma is not an operator in Python. Consider this session::
682
683 >>> "a" in "b", "a"
684 (False, '1')
685
686Since the comma is not an operator, but a separator between expressions the
687above is evaluated as if you had entered::
688
689 >>> ("a" in "b"), "a"
690
691not::
692
693 >>> "a" in ("5", "a")
694
695The same is true of the various assignment operators (``=``, ``+=`` etc). They
696are not truly operators but syntactic delimiters in assignment statements.
697
698
699Is there an equivalent of C's "?:" ternary operator?
700----------------------------------------------------
701
702Yes, this feature was added in Python 2.5. The syntax would be as follows::
703
704 [on_true] if [expression] else [on_false]
705
706 x, y = 50, 25
707
708 small = x if x < y else y
709
710For versions previous to 2.5 the answer would be 'No'.
711
712.. XXX remove rest?
713
714In many cases you can mimic ``a ? b : c`` with ``a and b or c``, but there's a
715flaw: if *b* is zero (or empty, or ``None`` -- anything that tests false) then
716*c* will be selected instead. In many cases you can prove by looking at the
717code that this can't happen (e.g. because *b* is a constant or has a type that
718can never be false), but in general this can be a problem.
719
720Tim Peters (who wishes it was Steve Majewski) suggested the following solution:
721``(a and [b] or [c])[0]``. Because ``[b]`` is a singleton list it is never
722false, so the wrong path is never taken; then applying ``[0]`` to the whole
723thing gets the *b* or *c* that you really wanted. Ugly, but it gets you there
724in the rare cases where it is really inconvenient to rewrite your code using
725'if'.
726
727The best course is usually to write a simple ``if...else`` statement. Another
728solution is to implement the ``?:`` operator as a function::
729
730 def q(cond, on_true, on_false):
731 if cond:
732 if not isfunction(on_true):
733 return on_true
734 else:
735 return apply(on_true)
736 else:
737 if not isfunction(on_false):
738 return on_false
739 else:
740 return apply(on_false)
741
742In most cases you'll pass b and c directly: ``q(a, b, c)``. To avoid evaluating
743b or c when they shouldn't be, encapsulate them within a lambda function, e.g.:
744``q(a, lambda: b, lambda: c)``.
745
746It has been asked *why* Python has no if-then-else expression. There are
747several answers: many languages do just fine without one; it can easily lead to
748less readable code; no sufficiently "Pythonic" syntax has been discovered; a
749search of the standard library found remarkably few places where using an
750if-then-else expression would make the code more understandable.
751
752In 2002, :pep:`308` was written proposing several possible syntaxes and the
753community was asked to vote on the issue. The vote was inconclusive. Most
754people liked one of the syntaxes, but also hated other syntaxes; many votes
755implied that people preferred no ternary operator rather than having a syntax
756they hated.
757
758
759Is it possible to write obfuscated one-liners in Python?
760--------------------------------------------------------
761
762Yes. Usually this is done by nesting :keyword:`lambda` within
763:keyword:`lambda`. See the following three examples, due to Ulf Bartelt::
764
765 # Primes < 1000
766 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
767 map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
768
769 # First 10 Fibonacci numbers
770 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
771 range(10))
772
773 # Mandelbrot set
774 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
775 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
776 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
777 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
778 >=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(
779 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
780 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
781 # \___ ___/ \___ ___/ | | |__ lines on screen
782 # V V | |______ columns on screen
783 # | | |__________ maximum of "iterations"
784 # | |_________________ range on y axis
785 # |____________________________ range on x axis
786
787Don't try this at home, kids!
788
789
790Numbers and strings
791===================
792
793How do I specify hexadecimal and octal integers?
794------------------------------------------------
795
796To specify an octal digit, precede the octal value with a zero. For example, to
797set the variable "a" to the octal value "10" (8 in decimal), type::
798
799 >>> a = 010
800 >>> a
801 8
802
803Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,
804and then a lower or uppercase "x". Hexadecimal digits can be specified in lower
805or uppercase. For example, in the Python interpreter::
806
807 >>> a = 0xa5
808 >>> a
809 165
810 >>> b = 0XB2
811 >>> b
812 178
813
814
815Why does -22 / 10 return -3?
816----------------------------
817
818It's primarily driven by the desire that ``i % j`` have the same sign as ``j``.
819If you want that, and also want::
820
821 i == (i / j) * j + (i % j)
822
823then integer division has to return the floor. C also requires that identity to
824hold, and then compilers that truncate ``i / j`` need to make ``i % j`` have the
825same sign as ``i``.
826
827There are few real use cases for ``i % j`` when ``j`` is negative. When ``j``
828is positive, there are many, and in virtually all of them it's more useful for
829``i % j`` to be ``>= 0``. If the clock says 10 now, what did it say 200 hours
830ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to
831bite.
832
833
834How do I convert a string to a number?
835--------------------------------------
836
837For integers, use the built-in :func:`int` type constructor, e.g. ``int('144')
838== 144``. Similarly, :func:`float` converts to floating-point,
839e.g. ``float('144') == 144.0``.
840
841By default, these interpret the number as decimal, so that ``int('0144') ==
842144`` and ``int('0x144')`` raises :exc:`ValueError`. ``int(string, base)`` takes
843the base to convert from as a second optional argument, so ``int('0x144', 16) ==
844324``. If the base is specified as 0, the number is interpreted using Python's
845rules: a leading '0' indicates octal, and '0x' indicates a hex number.
846
847Do not use the built-in function :func:`eval` if all you need is to convert
848strings to numbers. :func:`eval` will be significantly slower and it presents a
849security risk: someone could pass you a Python expression that might have
850unwanted side effects. For example, someone could pass
851``__import__('os').system("rm -rf $HOME")`` which would erase your home
852directory.
853
854:func:`eval` also has the effect of interpreting numbers as Python expressions,
855so that e.g. ``eval('09')`` gives a syntax error because Python regards numbers
856starting with '0' as octal (base 8).
857
858
859How do I convert a number to a string?
860--------------------------------------
861
862To convert, e.g., the number 144 to the string '144', use the built-in type
863constructor :func:`str`. If you want a hexadecimal or octal representation, use
864the built-in functions ``hex()`` or ``oct()``. For fancy formatting, use
865:ref:`the % operator <string-formatting>` on strings, e.g. ``"%04d" % 144``
866yields ``'0144'`` and ``"%.3f" % (1/3.0)`` yields ``'0.333'``. See the library
867reference manual for details.
868
869
870How do I modify a string in place?
871----------------------------------
872
873You can't, because strings are immutable. If you need an object with this
874ability, try converting the string to a list or use the array module::
875
876 >>> s = "Hello, world"
877 >>> a = list(s)
878 >>> print a
879 ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
880 >>> a[7:] = list("there!")
881 >>> ''.join(a)
882 'Hello, there!'
883
884 >>> import array
885 >>> a = array.array('c', s)
886 >>> print a
887 array('c', 'Hello, world')
888 >>> a[0] = 'y' ; print a
889 array('c', 'yello world')
890 >>> a.tostring()
891 'yello, world'
892
893
894How do I use strings to call functions/methods?
895-----------------------------------------------
896
897There are various techniques.
898
899* The best is to use a dictionary that maps strings to functions. The primary
900 advantage of this technique is that the strings do not need to match the names
901 of the functions. This is also the primary technique used to emulate a case
902 construct::
903
904 def a():
905 pass
906
907 def b():
908 pass
909
910 dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
911
912 dispatch[get_input()]() # Note trailing parens to call function
913
914* Use the built-in function :func:`getattr`::
915
916 import foo
917 getattr(foo, 'bar')()
918
919 Note that :func:`getattr` works on any object, including classes, class
920 instances, modules, and so on.
921
922 This is used in several places in the standard library, like this::
923
924 class Foo:
925 def do_foo(self):
926 ...
927
928 def do_bar(self):
929 ...
930
931 f = getattr(foo_instance, 'do_' + opname)
932 f()
933
934
935* Use :func:`locals` or :func:`eval` to resolve the function name::
936
937 def myFunc():
938 print "hello"
939
940 fname = "myFunc"
941
942 f = locals()[fname]
943 f()
944
945 f = eval(fname)
946 f()
947
948 Note: Using :func:`eval` is slow and dangerous. If you don't have absolute
949 control over the contents of the string, someone could pass a string that
950 resulted in an arbitrary function being executed.
951
952Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
953-------------------------------------------------------------------------------------
954
955Starting with Python 2.2, you can use ``S.rstrip("\r\n")`` to remove all
956occurences of any line terminator from the end of the string ``S`` without
957removing other trailing whitespace. If the string ``S`` represents more than
958one line, with several empty lines at the end, the line terminators for all the
959blank lines will be removed::
960
961 >>> lines = ("line 1 \r\n"
962 ... "\r\n"
963 ... "\r\n")
964 >>> lines.rstrip("\n\r")
965 "line 1 "
966
967Since this is typically only desired when reading text one line at a time, using
968``S.rstrip()`` this way works well.
969
970For older versions of Python, There are two partial substitutes:
971
972- If you want to remove all trailing whitespace, use the ``rstrip()`` method of
973 string objects. This removes all trailing whitespace, not just a single
974 newline.
975
976- Otherwise, if there is only one line in the string ``S``, use
977 ``S.splitlines()[0]``.
978
979
980Is there a scanf() or sscanf() equivalent?
981------------------------------------------
982
983Not as such.
984
985For simple input parsing, the easiest approach is usually to split the line into
986whitespace-delimited words using the :meth:`~str.split` method of string objects
987and then convert decimal strings to numeric values using :func:`int` or
988:func:`float`. ``split()`` supports an optional "sep" parameter which is useful
989if the line uses something other than whitespace as a separator.
990
991For more complicated input parsing, regular expressions more powerful than C's
992:cfunc:`sscanf` and better suited for the task.
993
994
995What does 'UnicodeError: ASCII [decoding,encoding] error: ordinal not in range(128)' mean?
996------------------------------------------------------------------------------------------
997
998This error indicates that your Python installation can handle only 7-bit ASCII
999strings. There are a couple ways to fix or work around the problem.
1000
1001If your programs must handle data in arbitrary character set encodings, the
1002environment the application runs in will generally identify the encoding of the
1003data it is handing you. You need to convert the input to Unicode data using
1004that encoding. For example, a program that handles email or web input will
1005typically find character set encoding information in Content-Type headers. This
1006can then be used to properly convert input data to Unicode. Assuming the string
1007referred to by ``value`` is encoded as UTF-8::
1008
1009 value = unicode(value, "utf-8")
1010
1011will return a Unicode object. If the data is not correctly encoded as UTF-8,
1012the above call will raise a :exc:`UnicodeError` exception.
1013
1014If you only want strings converted to Unicode which have non-ASCII data, you can
1015try converting them first assuming an ASCII encoding, and then generate Unicode
1016objects if that fails::
1017
1018 try:
1019 x = unicode(value, "ascii")
1020 except UnicodeError:
1021 value = unicode(value, "utf-8")
1022 else:
1023 # value was valid ASCII data
1024 pass
1025
1026It's possible to set a default encoding in a file called ``sitecustomize.py``
1027that's part of the Python library. However, this isn't recommended because
1028changing the Python-wide default encoding may cause third-party extension
1029modules to fail.
1030
1031Note that on Windows, there is an encoding known as "mbcs", which uses an
1032encoding specific to your current locale. In many cases, and particularly when
1033working with COM, this may be an appropriate default encoding to use.
1034
1035
1036Sequences (Tuples/Lists)
1037========================
1038
1039How do I convert between tuples and lists?
1040------------------------------------------
1041
1042The type constructor ``tuple(seq)`` converts any sequence (actually, any
1043iterable) into a tuple with the same items in the same order.
1044
1045For example, ``tuple([1, 2, 3])`` yields ``(1, 2, 3)`` and ``tuple('abc')``
1046yields ``('a', 'b', 'c')``. If the argument is a tuple, it does not make a copy
1047but returns the same object, so it is cheap to call :func:`tuple` when you
1048aren't sure that an object is already a tuple.
1049
1050The type constructor ``list(seq)`` converts any sequence or iterable into a list
1051with the same items in the same order. For example, ``list((1, 2, 3))`` yields
1052``[1, 2, 3]`` and ``list('abc')`` yields ``['a', 'b', 'c']``. If the argument
1053is a list, it makes a copy just like ``seq[:]`` would.
1054
1055
1056What's a negative index?
1057------------------------
1058
1059Python sequences are indexed with positive numbers and negative numbers. For
1060positive numbers 0 is the first index 1 is the second index and so forth. For
1061negative indices -1 is the last index and -2 is the penultimate (next to last)
1062index and so forth. Think of ``seq[-n]`` as the same as ``seq[len(seq)-n]``.
1063
1064Using negative indices can be very convenient. For example ``S[:-1]`` is all of
1065the string except for its last character, which is useful for removing the
1066trailing newline from a string.
1067
1068
1069How do I iterate over a sequence in reverse order?
1070--------------------------------------------------
1071
Georg Brandl9c2c1f82010-02-07 12:01:57 +00001072Use the :func:`reversed` built-in function, which is new in Python 2.4::
Georg Brandl5d2eb342009-10-27 15:08:27 +00001073
1074 for x in reversed(sequence):
1075 ... # do something with x...
1076
1077This won't touch your original sequence, but build a new copy with reversed
1078order to iterate over.
1079
1080With Python 2.3, you can use an extended slice syntax::
1081
1082 for x in sequence[::-1]:
1083 ... # do something with x...
1084
1085
1086How do you remove duplicates from a list?
1087-----------------------------------------
1088
1089See the Python Cookbook for a long discussion of many ways to do this:
1090
1091 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
1092
1093If you don't mind reordering the list, sort it and then scan from the end of the
1094list, deleting duplicates as you go::
1095
1096 if List:
1097 List.sort()
1098 last = List[-1]
1099 for i in range(len(List)-2, -1, -1):
1100 if last == List[i]:
1101 del List[i]
1102 else:
1103 last = List[i]
1104
1105If all elements of the list may be used as dictionary keys (i.e. they are all
1106hashable) this is often faster ::
1107
1108 d = {}
1109 for x in List:
1110 d[x] = x
1111 List = d.values()
1112
1113In Python 2.5 and later, the following is possible instead::
1114
1115 List = list(set(List))
1116
1117This converts the list into a set, thereby removing duplicates, and then back
1118into a list.
1119
1120
1121How do you make an array in Python?
1122-----------------------------------
1123
1124Use a list::
1125
1126 ["this", 1, "is", "an", "array"]
1127
1128Lists are equivalent to C or Pascal arrays in their time complexity; the primary
1129difference is that a Python list can contain objects of many different types.
1130
1131The ``array`` module also provides methods for creating arrays of fixed types
1132with compact representations, but they are slower to index than lists. Also
1133note that the Numeric extensions and others define array-like structures with
1134various characteristics as well.
1135
1136To get Lisp-style linked lists, you can emulate cons cells using tuples::
1137
1138 lisp_list = ("like", ("this", ("example", None) ) )
1139
1140If mutability is desired, you could use lists instead of tuples. Here the
1141analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is
1142``lisp_list[1]``. Only do this if you're sure you really need to, because it's
1143usually a lot slower than using Python lists.
1144
1145
1146How do I create a multidimensional list?
1147----------------------------------------
1148
1149You probably tried to make a multidimensional array like this::
1150
1151 A = [[None] * 2] * 3
1152
1153This looks correct if you print it::
1154
1155 >>> A
1156 [[None, None], [None, None], [None, None]]
1157
1158But when you assign a value, it shows up in multiple places:
1159
1160 >>> A[0][0] = 5
1161 >>> A
1162 [[5, None], [5, None], [5, None]]
1163
1164The reason is that replicating a list with ``*`` doesn't create copies, it only
1165creates references to the existing objects. The ``*3`` creates a list
1166containing 3 references to the same list of length two. Changes to one row will
1167show in all rows, which is almost certainly not what you want.
1168
1169The suggested approach is to create a list of the desired length first and then
1170fill in each element with a newly created list::
1171
1172 A = [None] * 3
1173 for i in range(3):
1174 A[i] = [None] * 2
1175
1176This generates a list containing 3 different lists of length two. You can also
1177use a list comprehension::
1178
1179 w, h = 2, 3
1180 A = [[None] * w for i in range(h)]
1181
1182Or, you can use an extension that provides a matrix datatype; `Numeric Python
1183<http://numpy.scipy.org/>`_ is the best known.
1184
1185
1186How do I apply a method to a sequence of objects?
1187-------------------------------------------------
1188
1189Use a list comprehension::
1190
1191 result = [obj.method() for obj in List]
1192
1193More generically, you can try the following function::
1194
1195 def method_map(objects, method, arguments):
1196 """method_map([a,b], "meth", (1,2)) gives [a.meth(1,2), b.meth(1,2)]"""
1197 nobjects = len(objects)
1198 methods = map(getattr, objects, [method]*nobjects)
1199 return map(apply, methods, [arguments]*nobjects)
1200
1201
1202Dictionaries
1203============
1204
1205How can I get a dictionary to display its keys in a consistent order?
1206---------------------------------------------------------------------
1207
1208You can't. Dictionaries store their keys in an unpredictable order, so the
1209display order of a dictionary's elements will be similarly unpredictable.
1210
1211This can be frustrating if you want to save a printable version to a file, make
1212some changes and then compare it with some other printed dictionary. In this
1213case, use the ``pprint`` module to pretty-print the dictionary; the items will
1214be presented in order sorted by the key.
1215
1216A more complicated solution is to subclass ``UserDict.UserDict`` to create a
1217``SortedDict`` class that prints itself in a predictable order. Here's one
1218simpleminded implementation of such a class::
1219
1220 import UserDict, string
1221
1222 class SortedDict(UserDict.UserDict):
1223 def __repr__(self):
1224 result = []
1225 append = result.append
1226 keys = self.data.keys()
1227 keys.sort()
1228 for k in keys:
1229 append("%s: %s" % (`k`, `self.data[k]`))
1230 return "{%s}" % string.join(result, ", ")
1231
1232 __str__ = __repr__
1233
1234This will work for many common situations you might encounter, though it's far
1235from a perfect solution. The largest flaw is that if some values in the
1236dictionary are also dictionaries, their values won't be presented in any
1237particular order.
1238
1239
1240I want to do a complicated sort: can you do a Schwartzian Transform in Python?
1241------------------------------------------------------------------------------
1242
1243The technique, attributed to Randal Schwartz of the Perl community, sorts the
1244elements of a list by a metric which maps each element to its "sort value". In
1245Python, just use the ``key`` argument for the ``sort()`` method::
1246
1247 Isorted = L[:]
1248 Isorted.sort(key=lambda s: int(s[10:15]))
1249
1250The ``key`` argument is new in Python 2.4, for older versions this kind of
1251sorting is quite simple to do with list comprehensions. To sort a list of
1252strings by their uppercase values::
1253
1254 tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform
1255 tmp1.sort()
1256 Usorted = [x[1] for x in tmp1]
1257
1258To sort by the integer value of a subfield extending from positions 10-15 in
1259each string::
1260
1261 tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform
1262 tmp2.sort()
1263 Isorted = [x[1] for x in tmp2]
1264
1265Note that Isorted may also be computed by ::
1266
1267 def intfield(s):
1268 return int(s[10:15])
1269
1270 def Icmp(s1, s2):
1271 return cmp(intfield(s1), intfield(s2))
1272
1273 Isorted = L[:]
1274 Isorted.sort(Icmp)
1275
1276but since this method calls ``intfield()`` many times for each element of L, it
1277is slower than the Schwartzian Transform.
1278
1279
1280How can I sort one list by values from another list?
1281----------------------------------------------------
1282
1283Merge them into a single list of tuples, sort the resulting list, and then pick
1284out the element you want. ::
1285
1286 >>> list1 = ["what", "I'm", "sorting", "by"]
1287 >>> list2 = ["something", "else", "to", "sort"]
1288 >>> pairs = zip(list1, list2)
1289 >>> pairs
1290 [('what', 'something'), ("I'm", 'else'), ('sorting', 'to'), ('by', 'sort')]
1291 >>> pairs.sort()
1292 >>> result = [ x[1] for x in pairs ]
1293 >>> result
1294 ['else', 'sort', 'to', 'something']
1295
1296An alternative for the last step is::
1297
1298 result = []
1299 for p in pairs: result.append(p[1])
1300
1301If you find this more legible, you might prefer to use this instead of the final
1302list comprehension. However, it is almost twice as slow for long lists. Why?
1303First, the ``append()`` operation has to reallocate memory, and while it uses
1304some tricks to avoid doing that each time, it still has to do it occasionally,
1305and that costs quite a bit. Second, the expression "result.append" requires an
1306extra attribute lookup, and third, there's a speed reduction from having to make
1307all those function calls.
1308
1309
1310Objects
1311=======
1312
1313What is a class?
1314----------------
1315
1316A class is the particular object type created by executing a class statement.
1317Class objects are used as templates to create instance objects, which embody
1318both the data (attributes) and code (methods) specific to a datatype.
1319
1320A class can be based on one or more other classes, called its base class(es). It
1321then inherits the attributes and methods of its base classes. This allows an
1322object model to be successively refined by inheritance. You might have a
1323generic ``Mailbox`` class that provides basic accessor methods for a mailbox,
1324and subclasses such as ``MboxMailbox``, ``MaildirMailbox``, ``OutlookMailbox``
1325that handle various specific mailbox formats.
1326
1327
1328What is a method?
1329-----------------
1330
1331A method is a function on some object ``x`` that you normally call as
1332``x.name(arguments...)``. Methods are defined as functions inside the class
1333definition::
1334
1335 class C:
1336 def meth (self, arg):
1337 return arg * 2 + self.attribute
1338
1339
1340What is self?
1341-------------
1342
1343Self is merely a conventional name for the first argument of a method. A method
1344defined as ``meth(self, a, b, c)`` should be called as ``x.meth(a, b, c)`` for
1345some instance ``x`` of the class in which the definition occurs; the called
1346method will think it is called as ``meth(x, a, b, c)``.
1347
1348See also :ref:`why-self`.
1349
1350
1351How do I check if an object is an instance of a given class or of a subclass of it?
1352-----------------------------------------------------------------------------------
1353
1354Use the built-in function ``isinstance(obj, cls)``. You can check if an object
1355is an instance of any of a number of classes by providing a tuple instead of a
1356single class, e.g. ``isinstance(obj, (class1, class2, ...))``, and can also
1357check whether an object is one of Python's built-in types, e.g.
1358``isinstance(obj, str)`` or ``isinstance(obj, (int, long, float, complex))``.
1359
1360Note that most programs do not use :func:`isinstance` on user-defined classes
1361very often. If you are developing the classes yourself, a more proper
1362object-oriented style is to define methods on the classes that encapsulate a
1363particular behaviour, instead of checking the object's class and doing a
1364different thing based on what class it is. For example, if you have a function
1365that does something::
1366
1367 def search (obj):
1368 if isinstance(obj, Mailbox):
1369 # ... code to search a mailbox
1370 elif isinstance(obj, Document):
1371 # ... code to search a document
1372 elif ...
1373
1374A better approach is to define a ``search()`` method on all the classes and just
1375call it::
1376
1377 class Mailbox:
1378 def search(self):
1379 # ... code to search a mailbox
1380
1381 class Document:
1382 def search(self):
1383 # ... code to search a document
1384
1385 obj.search()
1386
1387
1388What is delegation?
1389-------------------
1390
1391Delegation is an object oriented technique (also called a design pattern).
1392Let's say you have an object ``x`` and want to change the behaviour of just one
1393of its methods. You can create a new class that provides a new implementation
1394of the method you're interested in changing and delegates all other methods to
1395the corresponding method of ``x``.
1396
1397Python programmers can easily implement delegation. For example, the following
1398class implements a class that behaves like a file but converts all written data
1399to uppercase::
1400
1401 class UpperOut:
1402
1403 def __init__(self, outfile):
1404 self._outfile = outfile
1405
1406 def write(self, s):
1407 self._outfile.write(s.upper())
1408
1409 def __getattr__(self, name):
1410 return getattr(self._outfile, name)
1411
1412Here the ``UpperOut`` class redefines the ``write()`` method to convert the
1413argument string to uppercase before calling the underlying
1414``self.__outfile.write()`` method. All other methods are delegated to the
1415underlying ``self.__outfile`` object. The delegation is accomplished via the
1416``__getattr__`` method; consult :ref:`the language reference <attribute-access>`
1417for more information about controlling attribute access.
1418
1419Note that for more general cases delegation can get trickier. When attributes
1420must be set as well as retrieved, the class must define a :meth:`__setattr__`
1421method too, and it must do so carefully. The basic implementation of
1422:meth:`__setattr__` is roughly equivalent to the following::
1423
1424 class X:
1425 ...
1426 def __setattr__(self, name, value):
1427 self.__dict__[name] = value
1428 ...
1429
1430Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store
1431local state for self without causing an infinite recursion.
1432
1433
1434How do I call a method defined in a base class from a derived class that overrides it?
1435--------------------------------------------------------------------------------------
1436
1437If you're using new-style classes, use the built-in :func:`super` function::
1438
1439 class Derived(Base):
1440 def meth (self):
1441 super(Derived, self).meth()
1442
1443If you're using classic classes: For a class definition such as ``class
1444Derived(Base): ...`` you can call method ``meth()`` defined in ``Base`` (or one
1445of ``Base``'s base classes) as ``Base.meth(self, arguments...)``. Here,
1446``Base.meth`` is an unbound method, so you need to provide the ``self``
1447argument.
1448
1449
1450How can I organize my code to make it easier to change the base class?
1451----------------------------------------------------------------------
1452
1453You could define an alias for the base class, assign the real base class to it
1454before your class definition, and use the alias throughout your class. Then all
1455you have to change is the value assigned to the alias. Incidentally, this trick
1456is also handy if you want to decide dynamically (e.g. depending on availability
1457of resources) which base class to use. Example::
1458
1459 BaseAlias = <real base class>
1460
1461 class Derived(BaseAlias):
1462 def meth(self):
1463 BaseAlias.meth(self)
1464 ...
1465
1466
1467How do I create static class data and static class methods?
1468-----------------------------------------------------------
1469
1470Static data (in the sense of C++ or Java) is easy; static methods (again in the
1471sense of C++ or Java) are not supported directly.
1472
1473For static data, simply define a class attribute. To assign a new value to the
1474attribute, you have to explicitly use the class name in the assignment::
1475
1476 class C:
1477 count = 0 # number of times C.__init__ called
1478
1479 def __init__(self):
1480 C.count = C.count + 1
1481
1482 def getcount(self):
1483 return C.count # or return self.count
1484
1485``c.count`` also refers to ``C.count`` for any ``c`` such that ``isinstance(c,
1486C)`` holds, unless overridden by ``c`` itself or by some class on the base-class
1487search path from ``c.__class__`` back to ``C``.
1488
1489Caution: within a method of C, an assignment like ``self.count = 42`` creates a
1490new and unrelated instance vrbl named "count" in ``self``'s own dict. Rebinding
1491of a class-static data name must always specify the class whether inside a
1492method or not::
1493
1494 C.count = 314
1495
1496Static methods are possible since Python 2.2::
1497
1498 class C:
1499 def static(arg1, arg2, arg3):
1500 # No 'self' parameter!
1501 ...
1502 static = staticmethod(static)
1503
1504With Python 2.4's decorators, this can also be written as ::
1505
1506 class C:
1507 @staticmethod
1508 def static(arg1, arg2, arg3):
1509 # No 'self' parameter!
1510 ...
1511
1512However, a far more straightforward way to get the effect of a static method is
1513via a simple module-level function::
1514
1515 def getcount():
1516 return C.count
1517
1518If your code is structured so as to define one class (or tightly related class
1519hierarchy) per module, this supplies the desired encapsulation.
1520
1521
1522How can I overload constructors (or methods) in Python?
1523-------------------------------------------------------
1524
1525This answer actually applies to all methods, but the question usually comes up
1526first in the context of constructors.
1527
1528In C++ you'd write
1529
1530.. code-block:: c
1531
1532 class C {
1533 C() { cout << "No arguments\n"; }
1534 C(int i) { cout << "Argument is " << i << "\n"; }
1535 }
1536
1537In Python you have to write a single constructor that catches all cases using
1538default arguments. For example::
1539
1540 class C:
1541 def __init__(self, i=None):
1542 if i is None:
1543 print "No arguments"
1544 else:
1545 print "Argument is", i
1546
1547This is not entirely equivalent, but close enough in practice.
1548
1549You could also try a variable-length argument list, e.g. ::
1550
1551 def __init__(self, *args):
1552 ...
1553
1554The same approach works for all method definitions.
1555
1556
1557I try to use __spam and I get an error about _SomeClassName__spam.
1558------------------------------------------------------------------
1559
1560Variable names with double leading underscores are "mangled" to provide a simple
1561but effective way to define class private variables. Any identifier of the form
1562``__spam`` (at least two leading underscores, at most one trailing underscore)
1563is textually replaced with ``_classname__spam``, where ``classname`` is the
1564current class name with any leading underscores stripped.
1565
1566This doesn't guarantee privacy: an outside user can still deliberately access
1567the "_classname__spam" attribute, and private values are visible in the object's
1568``__dict__``. Many Python programmers never bother to use private variable
1569names at all.
1570
1571
1572My class defines __del__ but it is not called when I delete the object.
1573-----------------------------------------------------------------------
1574
1575There are several possible reasons for this.
1576
1577The del statement does not necessarily call :meth:`__del__` -- it simply
1578decrements the object's reference count, and if this reaches zero
1579:meth:`__del__` is called.
1580
1581If your data structures contain circular links (e.g. a tree where each child has
1582a parent reference and each parent has a list of children) the reference counts
1583will never go back to zero. Once in a while Python runs an algorithm to detect
1584such cycles, but the garbage collector might run some time after the last
1585reference to your data structure vanishes, so your :meth:`__del__` method may be
1586called at an inconvenient and random time. This is inconvenient if you're trying
1587to reproduce a problem. Worse, the order in which object's :meth:`__del__`
1588methods are executed is arbitrary. You can run :func:`gc.collect` to force a
1589collection, but there *are* pathological cases where objects will never be
1590collected.
1591
1592Despite the cycle collector, it's still a good idea to define an explicit
1593``close()`` method on objects to be called whenever you're done with them. The
1594``close()`` method can then remove attributes that refer to subobjecs. Don't
1595call :meth:`__del__` directly -- :meth:`__del__` should call ``close()`` and
1596``close()`` should make sure that it can be called more than once for the same
1597object.
1598
1599Another way to avoid cyclical references is to use the :mod:`weakref` module,
1600which allows you to point to objects without incrementing their reference count.
1601Tree data structures, for instance, should use weak references for their parent
1602and sibling references (if they need them!).
1603
1604If the object has ever been a local variable in a function that caught an
1605expression in an except clause, chances are that a reference to the object still
1606exists in that function's stack frame as contained in the stack trace.
1607Normally, calling :func:`sys.exc_clear` will take care of this by clearing the
1608last recorded exception.
1609
1610Finally, if your :meth:`__del__` method raises an exception, a warning message
1611is printed to :data:`sys.stderr`.
1612
1613
1614How do I get a list of all instances of a given class?
1615------------------------------------------------------
1616
1617Python does not keep track of all instances of a class (or of a built-in type).
1618You can program the class's constructor to keep track of all instances by
1619keeping a list of weak references to each instance.
1620
1621
1622Modules
1623=======
1624
1625How do I create a .pyc file?
1626----------------------------
1627
1628When a module is imported for the first time (or when the source is more recent
1629than the current compiled file) a ``.pyc`` file containing the compiled code
1630should be created in the same directory as the ``.py`` file.
1631
1632One reason that a ``.pyc`` file may not be created is permissions problems with
1633the directory. This can happen, for example, if you develop as one user but run
1634as another, such as if you are testing with a web server. Creation of a .pyc
1635file is automatic if you're importing a module and Python has the ability
1636(permissions, free space, etc...) to write the compiled module back to the
1637directory.
1638
1639Running Python on a top level script is not considered an import and no ``.pyc``
1640will be created. For example, if you have a top-level module ``abc.py`` that
1641imports another module ``xyz.py``, when you run abc, ``xyz.pyc`` will be created
1642since xyz is imported, but no ``abc.pyc`` file will be created since ``abc.py``
1643isn't being imported.
1644
1645If you need to create abc.pyc -- that is, to create a .pyc file for a module
1646that is not imported -- you can, using the :mod:`py_compile` and
1647:mod:`compileall` modules.
1648
1649The :mod:`py_compile` module can manually compile any module. One way is to use
1650the ``compile()`` function in that module interactively::
1651
1652 >>> import py_compile
1653 >>> py_compile.compile('abc.py')
1654
1655This will write the ``.pyc`` to the same location as ``abc.py`` (or you can
1656override that with the optional parameter ``cfile``).
1657
1658You can also automatically compile all files in a directory or directories using
1659the :mod:`compileall` module. You can do it from the shell prompt by running
1660``compileall.py`` and providing the path of a directory containing Python files
1661to compile::
1662
1663 python -m compileall .
1664
1665
1666How do I find the current module name?
1667--------------------------------------
1668
1669A module can find out its own module name by looking at the predefined global
1670variable ``__name__``. If this has the value ``'__main__'``, the program is
1671running as a script. Many modules that are usually used by importing them also
1672provide a command-line interface or a self-test, and only execute this code
1673after checking ``__name__``::
1674
1675 def main():
1676 print 'Running test...'
1677 ...
1678
1679 if __name__ == '__main__':
1680 main()
1681
1682
1683How can I have modules that mutually import each other?
1684-------------------------------------------------------
1685
1686Suppose you have the following modules:
1687
1688foo.py::
1689
1690 from bar import bar_var
1691 foo_var = 1
1692
1693bar.py::
1694
1695 from foo import foo_var
1696 bar_var = 2
1697
1698The problem is that the interpreter will perform the following steps:
1699
1700* main imports foo
1701* Empty globals for foo are created
1702* foo is compiled and starts executing
1703* foo imports bar
1704* Empty globals for bar are created
1705* bar is compiled and starts executing
1706* bar imports foo (which is a no-op since there already is a module named foo)
1707* bar.foo_var = foo.foo_var
1708
1709The last step fails, because Python isn't done with interpreting ``foo`` yet and
1710the global symbol dictionary for ``foo`` is still empty.
1711
1712The same thing happens when you use ``import foo``, and then try to access
1713``foo.foo_var`` in global code.
1714
1715There are (at least) three possible workarounds for this problem.
1716
1717Guido van Rossum recommends avoiding all uses of ``from <module> import ...``,
1718and placing all code inside functions. Initializations of global variables and
1719class variables should use constants or built-in functions only. This means
1720everything from an imported module is referenced as ``<module>.<name>``.
1721
1722Jim Roskind suggests performing steps in the following order in each module:
1723
1724* exports (globals, functions, and classes that don't need imported base
1725 classes)
1726* ``import`` statements
1727* active code (including globals that are initialized from imported values).
1728
1729van Rossum doesn't like this approach much because the imports appear in a
1730strange place, but it does work.
1731
1732Matthias Urlichs recommends restructuring your code so that the recursive import
1733is not necessary in the first place.
1734
1735These solutions are not mutually exclusive.
1736
1737
1738__import__('x.y.z') returns <module 'x'>; how do I get z?
1739---------------------------------------------------------
1740
1741Try::
1742
1743 __import__('x.y.z').y.z
1744
1745For more realistic situations, you may have to do something like ::
1746
1747 m = __import__(s)
1748 for i in s.split(".")[1:]:
1749 m = getattr(m, i)
1750
1751See :mod:`importlib` for a convenience function called
1752:func:`~importlib.import_module`.
1753
1754
1755
1756When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
1757-------------------------------------------------------------------------------------------------
1758
1759For reasons of efficiency as well as consistency, Python only reads the module
1760file on the first time a module is imported. If it didn't, in a program
1761consisting of many modules where each one imports the same basic module, the
1762basic module would be parsed and re-parsed many times. To force rereading of a
1763changed module, do this::
1764
1765 import modname
1766 reload(modname)
1767
1768Warning: this technique is not 100% fool-proof. In particular, modules
1769containing statements like ::
1770
1771 from modname import some_objects
1772
1773will continue to work with the old version of the imported objects. If the
1774module contains class definitions, existing class instances will *not* be
1775updated to use the new class definition. This can result in the following
1776paradoxical behaviour:
1777
1778 >>> import cls
1779 >>> c = cls.C() # Create an instance of C
1780 >>> reload(cls)
1781 <module 'cls' from 'cls.pyc'>
1782 >>> isinstance(c, cls.C) # isinstance is false?!?
1783 False
1784
1785The nature of the problem is made clear if you print out the class objects:
1786
1787 >>> c.__class__
1788 <class cls.C at 0x7352a0>
1789 >>> cls.C
1790 <class cls.C at 0x4198d0>
1791