12 Is there a source code level debugger with breakpoints, single-stepping, etc.?
13 ------------------------------------------------------------------------------
17 The pdb module is a simple but adequate console-mode debugger for Python. It is
18 part of the standard Python library, and is :mod:`documented in the Library
19 Reference Manual <pdb>`. You can also write your own debugger by using the code
20 for pdb as an example.
22 The IDLE interactive development environment, which is part of the standard
23 Python distribution (normally available as Tools/scripts/idle), includes a
24 graphical debugger. There is documentation for the IDLE debugger at
25 http://www.python.org/idle/doc/idle2.html#Debugger.
27 PythonWin is a Python IDE that includes a GUI debugger based on pdb. The
28 Pythonwin debugger colors breakpoints and has quite a few cool features such as
29 debugging non-Pythonwin programs. Pythonwin is available as part of the `Python
30 for Windows Extensions <http://sourceforge.net/projects/pywin32/>`__ project and
31 as a part of the ActivePython distribution (see
32 http://www.activestate.com/Products/ActivePython/index.html).
34 `Boa Constructor <http://boa-constructor.sourceforge.net/>`_ is an IDE and GUI
35 builder that uses wxWidgets. It offers visual frame creation and manipulation,
36 an object inspector, many views on the source like object browsers, inheritance
37 hierarchies, doc string generated html documentation, an advanced debugger,
38 integrated help, and Zope support.
40 `Eric <http://www.die-offenbachs.de/eric/index.html>`_ is an IDE built on PyQt
41 and the Scintilla editing component.
43 Pydb 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
45 found at http://bashdb.sourceforge.net/pydb/ and DDD can be found at
46 http://www.gnu.org/software/ddd.
48 There are a number of commercial Python IDEs that include graphical debuggers.
51 * Wing IDE (http://wingware.com/)
52 * Komodo IDE (http://www.activestate.com/Products/Komodo)
55 Is there a tool to help find bugs or perform static analysis?
56 -------------------------------------------------------------
60 PyChecker is a static analysis tool that finds bugs in Python source code and
61 warns about code complexity and style. You can get PyChecker from
62 http://pychecker.sf.net.
64 `Pylint <http://www.logilab.org/projects/pylint>`_ is another tool that checks
65 if a module satisfies a coding standard, and also makes it possible to write
66 plug-ins to add a custom feature. In addition to the bug checking that
67 PyChecker performs, Pylint offers some additional features such as checking line
68 length, whether variable names are well-formed according to your coding
69 standard, whether declared interfaces are fully implemented, and more.
70 http://www.logilab.org/card/pylint_manual provides a full list of Pylint's
74 How can I create a stand-alone binary from a Python script?
75 -----------------------------------------------------------
77 You don't need the ability to compile Python to C code if all you want is a
78 stand-alone program that users can download and run without having to install
79 the Python distribution first. There are a number of tools that determine the
80 set of modules required by a program and bind these modules together with a
81 Python binary to produce a single executable.
83 One 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
85 embed all your modules into a new program, which is then linked with the
86 standard Python modules.
88 It works by scanning your source recursively for import statements (in both
89 forms) and looking for the modules in the standard Python path as well as in the
90 source directory (for built-in modules). It then turns the bytecode for modules
91 written in Python into C code (array initializers that can be turned into code
92 objects using the marshal module) and creates a custom-made config file that
93 only contains those built-in modules which are actually used in the program. It
94 then compiles the generated C code and links it with the rest of the Python
95 interpreter to form a self-contained binary which acts exactly like your script.
97 Obviously, freeze requires a C compiler. There are several other utilities
98 which don't. One is Thomas Heller's py2exe (Windows only) at
100 http://www.py2exe.org/
102 Another is Christian Tismer's `SQFREEZE <http://starship.python.net/crew/pirx>`_
103 which appends the byte code to a specially-prepared Python interpreter that can
104 find the byte code in the executable.
106 Other 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>`_.
111 Are there coding standards or a style guide for Python programs?
112 ----------------------------------------------------------------
114 Yes. The coding style required for standard library modules is documented as
118 My program is too slow. How do I speed it up?
119 ---------------------------------------------
121 That's a tough one, in general. There are many tricks to speed up Python code;
122 consider rewriting parts in C as a last resort.
124 In some cases it's possible to automatically translate Python to C or x86
125 assembly language, meaning that you don't have to modify your code to gain
128 .. XXX seems to have overlap with other questions!
130 `Pyrex <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_ can compile a
131 slightly modified version of Python code into a C extension, and can be used on
132 many different platforms.
134 `Psyco <http://psyco.sourceforge.net>`_ is a just-in-time compiler that
135 translates Python code into x86 assembly language. If you can use it, Psyco can
136 provide dramatic speedups for critical functions.
138 The rest of this answer will discuss various tricks for squeezing a bit more
139 speed out of Python code. *Never* apply any optimization tricks unless you know
140 you need them, after profiling has indicated that a particular function is the
141 heavily executed hot spot in the code. Optimizations almost always make the
142 code less clear, and you shouldn't pay the costs of reduced clarity (increased
143 development time, greater likelihood of bugs) unless the resulting performance
146 There is a page on the wiki devoted to `performance tips
147 <http://wiki.python.org/moin/PythonSpeed/PerformanceTips>`_.
149 Guido van Rossum has written up an anecdote related to optimization at
150 http://www.python.org/doc/essays/list2str.html.
152 One thing to notice is that function and (especially) method calls are rather
153 expensive; if you have designed a purely OO interface with lots of tiny
154 functions that don't do much more than get or set an instance variable or call
155 another method, you might consider using a more direct way such as directly
156 accessing instance variables. Also see the standard module :mod:`profile` which
157 makes 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
159 an order of magnitude).
161 Remember that many standard optimization heuristics you may know from other
162 programming experience may well apply to Python. For example it may be faster
163 to send output to output devices using larger writes rather than smaller ones in
164 order to reduce the overhead of kernel system calls. Thus CGI scripts that
165 write all output in "one shot" may be faster than those that write lots of small
168 Also, be sure to use Python's core features where appropriate. For example,
169 slicing allows programs to chop up lists and other sequence objects in a single
170 tick of the interpreter's mainloop using highly optimized C implementations.
171 Thus to get the same effect as::
177 it is much shorter and far faster to use ::
179 L2 = list(L1[:3]) # "list" is redundant if L1 is a list.
181 Note that the functionally-oriented builtins such as :func:`map`, :func:`zip`,
182 and friends can be a convenient accelerator for loops that perform a single
183 task. For example to pair the elements of two lists together::
185 >>> zip([1,2,3], [4,5,6])
186 [(1, 4), (2, 5), (3, 6)]
188 or to compute a number of sines::
190 >>> map( math.sin, (1,2,3,4))
191 [0.841470984808, 0.909297426826, 0.14112000806, -0.756802495308]
193 The operation completes very quickly in such cases.
195 Other examples include the ``join()`` and ``split()`` methods of string objects.
196 For example if s1..s7 are large (10K+) strings then
197 ``"".join([s1,s2,s3,s4,s5,s6,s7])`` may be far faster than the more obvious
198 ``s1+s2+s3+s4+s5+s6+s7``, since the "summation" will compute many
199 subexpressions, whereas ``join()`` does all the copying in one pass. For
200 manipulating strings, use the ``replace()`` method on string objects. Use
201 regular expressions only when you're not dealing with constant string patterns.
202 Consider using the string formatting operations ``string % tuple`` and ``string
205 Be sure to use the :meth:`list.sort` builtin method to do sorting, and see the
206 `sorting mini-HOWTO <http://wiki.python.org/moin/HowTo/Sorting>`_ for examples
207 of moderately advanced usage. :meth:`list.sort` beats other techniques for
208 sorting in all but the most extreme circumstances.
210 Another common trick is to "push loops into functions or methods." For example
211 suppose you have a program that runs slowly and you use the profiler to
212 determine that a Python function ``ff()`` is being called lots of times. If you
213 notice that ``ff ()``::
216 ... # do something with x computing result...
219 tends to be called in loops like::
221 list = map(ff, oldlist)
227 ... # do something with value...
229 then you can often eliminate function call overhead by rewriting ``ff()`` to::
234 ... # do something with x computing result...
235 resultseq.append(result)
238 and rewrite the two examples to ``list = ffseq(oldlist)`` and to::
240 for value in ffseq(sequence):
241 ... # do something with value...
243 Single calls to ``ff(x)`` translate to ``ffseq([x])[0]`` with little penalty.
244 Of course this technique is not always appropriate and there are other variants
245 which you can figure out.
247 You can gain some performance by explicitly storing the results of a function or
248 method lookup into a local variable. A loop like::
251 dict[key] = dict.get(key, 0) + 1
253 resolves ``dict.get`` every iteration. If the method isn't going to change, a
254 slightly faster implementation is::
256 dict_get = dict.get # look up the method once
258 dict[key] = dict_get(key, 0) + 1
260 Default arguments can be used to determine values once, at compile time instead
261 of at run time. This can only be done for functions or objects which will not
262 be changed during program execution, such as replacing ::
265 return math.sin(deg * math.pi / 180.0)
269 def degree_sin(deg, factor=math.pi/180.0, sin=math.sin):
270 return sin(deg * factor)
272 Because this trick uses default arguments for terms which should not be changed,
273 it should only be used when you are not concerned with presenting a possibly
274 confusing API to your users.
280 How do you set a global variable in a function?
281 -----------------------------------------------
283 Did you do something like this? ::
285 x = 1 # make a global
288 print x # try to print the global
294 Any variable assigned in a function is local to that function. unless it is
295 specifically declared global. Since a value is bound to ``x`` as the last
296 statement of the function body, the compiler assumes that ``x`` is
297 local. Consequently the ``print x`` attempts to print an uninitialized local
298 variable and will trigger a ``NameError``.
300 The solution is to insert an explicit global declaration at the start of the
305 print x # try to print the global
311 In this case, all references to ``x`` are interpreted as references to the ``x``
312 from the module namespace.
315 What are the rules for local and global variables in Python?
316 ------------------------------------------------------------
318 In Python, variables that are only referenced inside a function are implicitly
319 global. If a variable is assigned a new value anywhere within the function's
320 body, it's assumed to be a local. If a variable is ever assigned a new value
321 inside the function, the variable is implicitly local, and you need to
322 explicitly declare it as 'global'.
324 Though a bit surprising at first, a moment's consideration explains this. On
325 one hand, requiring :keyword:`global` for assigned variables provides a bar
326 against unintended side-effects. On the other hand, if ``global`` was required
327 for all global references, you'd be using ``global`` all the time. You'd have
328 to declare as global every reference to a builtin function or to a component of
329 an imported module. This clutter would defeat the usefulness of the ``global``
330 declaration for identifying side-effects.
333 How do I share global variables across modules?
334 ------------------------------------------------
336 The canonical way to share information across modules within a single program is
337 to create a special module (often called config or cfg). Just import the config
338 module in all modules of your application; the module then becomes available as
339 a global name. Because there is only one instance of each module, any changes
340 made to the module object get reflected everywhere. For example:
344 x = 0 # Default value of the 'x' configuration setting
357 Note that using a module is also the basis for implementing the Singleton design
358 pattern, for the same reason.
361 What are the "best practices" for using import in a module?
362 -----------------------------------------------------------
364 In general, don't use ``from modulename import *``. Doing so clutters the
365 importer's namespace. Some people avoid this idiom even with the few modules
366 that were designed to be imported in this manner. Modules designed in this
367 manner include :mod:`Tkinter`, and :mod:`threading`.
369 Import modules at the top of a file. Doing so makes it clear what other modules
370 your code requires and avoids questions of whether the module name is in scope.
371 Using one import per line makes it easy to add and delete module imports, but
372 using multiple imports per line uses less screen space.
374 It's good practice if you import modules in the following order:
376 1. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re``)
377 2. third-party library modules (anything installed in Python's site-packages
378 directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc.
379 3. locally-developed modules
381 Never use relative package imports. If you're writing code that's in the
382 ``package.sub.m1`` module and want to import ``package.sub.m2``, do not just
383 write ``import m2``, even though it's legal. Write ``from package.sub import
384 m2`` instead. Relative imports can lead to a module being initialized twice,
385 leading to confusing bugs.
387 It is sometimes necessary to move imports to a function or class to avoid
388 problems with circular imports. Gordon McMillan says:
390 Circular imports are fine where both modules use the "import <module>" form
391 of import. They fail when the 2nd module wants to grab a name out of the
392 first ("from module import name") and the import is at the top level. That's
393 because names in the 1st are not yet available, because the first module is
394 busy importing the 2nd.
396 In this case, if the second module is only used in one function, then the import
397 can easily be moved into that function. By the time the import is called, the
398 first module will have finished initializing, and the second module can do its
401 It may also be necessary to move imports out of the top level of code if some of
402 the modules are platform-specific. In that case, it may not even be possible to
403 import all of the modules at the top of the file. In this case, importing the
404 correct modules in the corresponding platform-specific code is a good option.
406 Only move imports into a local scope, such as inside a function definition, if
407 it's necessary to solve a problem such as avoiding a circular import or are
408 trying to reduce the initialization time of a module. This technique is
409 especially helpful if many of the imports are unnecessary depending on how the
410 program executes. You may also want to move imports into a function if the
411 modules are only ever used in that function. Note that loading a module the
412 first time may be expensive because of the one time initialization of the
413 module, but loading a module multiple times is virtually free, costing only a
414 couple of dictionary lookups. Even if the module name has gone out of scope,
415 the module is probably available in :data:`sys.modules`.
417 If only instances of a specific class use a module, then it is reasonable to
418 import the module in the class's ``__init__`` method and then assign the module
419 to an instance variable so that the module is always available (via that
420 instance variable) during the life of the object. Note that to delay an import
421 until the class is instantiated, the import must be inside a method. Putting
422 the import inside the class but outside of any method still causes the import to
423 occur when the module is initialized.
426 How can I pass optional or keyword parameters from one function to another?
427 ---------------------------------------------------------------------------
429 Collect the arguments using the ``*`` and ``**`` specifiers in the function's
430 parameter list; this gives you the positional arguments as a tuple and the
431 keyword arguments as a dictionary. You can then pass these arguments when
432 calling another function by using ``*`` and ``**``::
434 def f(x, *args, **kwargs):
436 kwargs['width'] = '14.3c'
438 g(x, *args, **kwargs)
440 In the unlikely case that you care about Python versions older than 2.0, use
443 def f(x, *args, **kwargs):
445 kwargs['width'] = '14.3c'
447 apply(g, (x,)+args, kwargs)
450 How do I write a function with output parameters (call by reference)?
451 ---------------------------------------------------------------------
453 Remember that arguments are passed by assignment in Python. Since assignment
454 just creates references to objects, there's no alias between an argument name in
455 the caller and callee, and so no call-by-reference per se. You can achieve the
456 desired effect in a number of ways.
458 1) By returning a tuple of the results::
461 a = 'new-value' # a and b are local names
462 b = b + 1 # assigned to new objects
463 return a, b # return new values
465 x, y = 'old-value', 99
467 print x, y # output: new-value 100
469 This is almost always the clearest solution.
471 2) By using global variables. This isn't thread-safe, and is not recommended.
473 3) By passing a mutable (changeable in-place) object::
476 a[0] = 'new-value' # 'a' references a mutable list
477 a[1] = a[1] + 1 # changes a shared object
479 args = ['old-value', 99]
481 print args[0], args[1] # output: new-value 100
483 4) By passing in a dictionary that gets mutated::
486 args['a'] = 'new-value' # args is a mutable dictionary
487 args['b'] = args['b'] + 1 # change it in-place
489 args = {'a':' old-value', 'b': 99}
491 print args['a'], args['b']
493 5) Or bundle up values in a class instance::
496 def __init__(self, **args):
497 for (key, value) in args.items():
498 setattr(self, key, value)
501 args.a = 'new-value' # args is a mutable callByRef
502 args.b = args.b + 1 # change object in-place
504 args = callByRef(a='old-value', b=99)
509 There's almost never a good reason to get this complicated.
511 Your best choice is to return a tuple containing the multiple results.
514 How do you make a higher order function in Python?
515 --------------------------------------------------
517 You have two choices: you can use nested scopes or you can use callable objects.
518 For example, suppose you wanted to define ``linear(a,b)`` which returns a
519 function ``f(x)`` that computes the value ``a*x+b``. Using nested scopes::
526 Or using a callable object::
530 def __init__(self, a, b):
531 self.a, self.b = a, b
533 def __call__(self, x):
534 return self.a * x + self.b
538 taxes = linear(0.3, 2)
540 gives a callable object where ``taxes(10e6) == 0.3 * 10e6 + 2``.
542 The callable object approach has the disadvantage that it is a bit slower and
543 results in slightly longer code. However, note that a collection of callables
544 can share their signature via inheritance::
546 class exponential(linear):
548 def __call__(self, x):
549 return self.a * (x ** self.b)
551 Object can encapsulate state for several methods::
561 self.value = self.value + 1
564 self.value = self.value - 1
567 inc, dec, reset = count.up, count.down, count.set
569 Here ``inc()``, ``dec()`` and ``reset()`` act like functions which share the
570 same counting variable.
573 How do I copy an object in Python?
574 ----------------------------------
576 In general, try :func:`copy.copy` or :func:`copy.deepcopy` for the general case.
577 Not all objects can be copied, but most can.
579 Some objects can be copied more easily. Dictionaries have a :meth:`~dict.copy`
582 newdict = olddict.copy()
584 Sequences can be copied by slicing::
589 How can I find the methods or attributes of an object?
590 ------------------------------------------------------
592 For an instance x of a user-defined class, ``dir(x)`` returns an alphabetized
593 list of the names containing the instance attributes and methods and attributes
594 defined by its class.
597 How can my code discover the name of an object?
598 -----------------------------------------------
600 Generally speaking, it can't, because objects don't really have names.
601 Essentially, assignment always binds a name to a value; The same is true of
602 ``def`` and ``class`` statements, but in that case the value is a
603 callable. Consider the following code::
613 <__main__.A instance at 016D07CC>
615 <__main__.A instance at 016D07CC>
617 Arguably the class has a name: even though it is bound to two names and invoked
618 through the name B the created instance is still reported as an instance of
619 class A. However, it is impossible to say whether the instance's name is a or
620 b, since both names are bound to the same value.
622 Generally speaking it should not be necessary for your code to "know the names"
623 of particular values. Unless you are deliberately writing introspective
624 programs, this is usually an indication that a change of approach might be
627 In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to
630 The same way as you get the name of that cat you found on your porch: the cat
631 (object) itself cannot tell you its name, and it doesn't really care -- so
632 the only way to find out what it's called is to ask all your neighbours
633 (namespaces) if it's their cat (object)...
635 ....and don't be surprised if you'll find that it's known by many names, or
639 What's up with the comma operator's precedence?
640 -----------------------------------------------
642 Comma is not an operator in Python. Consider this session::
647 Since the comma is not an operator, but a separator between expressions the
648 above is evaluated as if you had entered::
650 >>> ("a" in "b"), "a"
654 >>> "a" in ("5", "a")
656 The same is true of the various assignment operators (``=``, ``+=`` etc). They
657 are not truly operators but syntactic delimiters in assignment statements.
660 Is there an equivalent of C's "?:" ternary operator?
661 ----------------------------------------------------
663 Yes, this feature was added in Python 2.5. The syntax would be as follows::
665 [on_true] if [expression] else [on_false]
669 small = x if x < y else y
671 For versions previous to 2.5 the answer would be 'No'.
675 In many cases you can mimic ``a ? b : c`` with ``a and b or c``, but there's a
676 flaw: if *b* is zero (or empty, or ``None`` -- anything that tests false) then
677 *c* will be selected instead. In many cases you can prove by looking at the
678 code that this can't happen (e.g. because *b* is a constant or has a type that
679 can never be false), but in general this can be a problem.
681 Tim Peters (who wishes it was Steve Majewski) suggested the following solution:
682 ``(a and [b] or [c])[0]``. Because ``[b]`` is a singleton list it is never
683 false, so the wrong path is never taken; then applying ``[0]`` to the whole
684 thing gets the *b* or *c* that you really wanted. Ugly, but it gets you there
685 in the rare cases where it is really inconvenient to rewrite your code using
688 The best course is usually to write a simple ``if...else`` statement. Another
689 solution is to implement the ``?:`` operator as a function::
691 def q(cond, on_true, on_false):
693 if not isfunction(on_true):
696 return apply(on_true)
698 if not isfunction(on_false):
701 return apply(on_false)
703 In most cases you'll pass b and c directly: ``q(a, b, c)``. To avoid evaluating
704 b or c when they shouldn't be, encapsulate them within a lambda function, e.g.:
705 ``q(a, lambda: b, lambda: c)``.
707 It has been asked *why* Python has no if-then-else expression. There are
708 several answers: many languages do just fine without one; it can easily lead to
709 less readable code; no sufficiently "Pythonic" syntax has been discovered; a
710 search of the standard library found remarkably few places where using an
711 if-then-else expression would make the code more understandable.
713 In 2002, :pep:`308` was written proposing several possible syntaxes and the
714 community was asked to vote on the issue. The vote was inconclusive. Most
715 people liked one of the syntaxes, but also hated other syntaxes; many votes
716 implied that people preferred no ternary operator rather than having a syntax
720 Is it possible to write obfuscated one-liners in Python?
721 --------------------------------------------------------
723 Yes. Usually this is done by nesting :keyword:`lambda` within
724 :keyword:`lambda`. See the following three examples, due to Ulf Bartelt::
727 print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
728 map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))
730 # First 10 Fibonacci numbers
731 print map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),
735 print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
736 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
737 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
738 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
739 >=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(
740 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
741 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
742 # \___ ___/ \___ ___/ | | |__ lines on screen
743 # V V | |______ columns on screen
744 # | | |__________ maximum of "iterations"
745 # | |_________________ range on y axis
746 # |____________________________ range on x axis
748 Don't try this at home, kids!
754 How do I specify hexadecimal and octal integers?
755 ------------------------------------------------
757 To specify an octal digit, precede the octal value with a zero. For example, to
758 set the variable "a" to the octal value "10" (8 in decimal), type::
764 Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,
765 and then a lower or uppercase "x". Hexadecimal digits can be specified in lower
766 or uppercase. For example, in the Python interpreter::
776 Why does -22 / 10 return -3?
777 ----------------------------
779 It's primarily driven by the desire that ``i % j`` have the same sign as ``j``.
780 If you want that, and also want::
782 i == (i / j) * j + (i % j)
784 then integer division has to return the floor. C also requires that identity to
785 hold, and then compilers that truncate ``i / j`` need to make ``i % j`` have the
788 There are few real use cases for ``i % j`` when ``j`` is negative. When ``j``
789 is positive, there are many, and in virtually all of them it's more useful for
790 ``i % j`` to be ``>= 0``. If the clock says 10 now, what did it say 200 hours
791 ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to
795 How do I convert a string to a number?
796 --------------------------------------
798 For integers, use the built-in :func:`int` type constructor, e.g. ``int('144')
799 == 144``. Similarly, :func:`float` converts to floating-point,
800 e.g. ``float('144') == 144.0``.
802 By default, these interpret the number as decimal, so that ``int('0144') ==
803 144`` and ``int('0x144')`` raises :exc:`ValueError`. ``int(string, base)`` takes
804 the base to convert from as a second optional argument, so ``int('0x144', 16) ==
805 324``. If the base is specified as 0, the number is interpreted using Python's
806 rules: a leading '0' indicates octal, and '0x' indicates a hex number.
808 Do not use the built-in function :func:`eval` if all you need is to convert
809 strings to numbers. :func:`eval` will be significantly slower and it presents a
810 security risk: someone could pass you a Python expression that might have
811 unwanted side effects. For example, someone could pass
812 ``__import__('os').system("rm -rf $HOME")`` which would erase your home
815 :func:`eval` also has the effect of interpreting numbers as Python expressions,
816 so that e.g. ``eval('09')`` gives a syntax error because Python regards numbers
817 starting with '0' as octal (base 8).
820 How do I convert a number to a string?
821 --------------------------------------
823 To convert, e.g., the number 144 to the string '144', use the built-in type
824 constructor :func:`str`. If you want a hexadecimal or octal representation, use
825 the built-in functions ``hex()`` or ``oct()``. For fancy formatting, use
826 :ref:`the % operator <string-formatting>` on strings, e.g. ``"%04d" % 144``
827 yields ``'0144'`` and ``"%.3f" % (1/3.0)`` yields ``'0.333'``. See the library
828 reference manual for details.
831 How do I modify a string in place?
832 ----------------------------------
834 You can't, because strings are immutable. If you need an object with this
835 ability, try converting the string to a list or use the array module::
837 >>> s = "Hello, world"
840 ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
841 >>> a[7:] = list("there!")
846 >>> a = array.array('c', s)
848 array('c', 'Hello, world')
849 >>> a[0] = 'y' ; print a
850 array('c', 'yello world')
855 How do I use strings to call functions/methods?
856 -----------------------------------------------
858 There are various techniques.
860 * The best is to use a dictionary that maps strings to functions. The primary
861 advantage of this technique is that the strings do not need to match the names
862 of the functions. This is also the primary technique used to emulate a case
871 dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
873 dispatch[get_input()]() # Note trailing parens to call function
875 * Use the built-in function :func:`getattr`::
878 getattr(foo, 'bar')()
880 Note that :func:`getattr` works on any object, including classes, class
881 instances, modules, and so on.
883 This is used in several places in the standard library, like this::
892 f = getattr(foo_instance, 'do_' + opname)
896 * Use :func:`locals` or :func:`eval` to resolve the function name::
909 Note: Using :func:`eval` is slow and dangerous. If you don't have absolute
910 control over the contents of the string, someone could pass a string that
911 resulted in an arbitrary function being executed.
913 Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
914 -------------------------------------------------------------------------------------
916 Starting with Python 2.2, you can use ``S.rstrip("\r\n")`` to remove all
917 occurences of any line terminator from the end of the string ``S`` without
918 removing other trailing whitespace. If the string ``S`` represents more than
919 one line, with several empty lines at the end, the line terminators for all the
920 blank lines will be removed::
922 >>> lines = ("line 1 \r\n"
925 >>> lines.rstrip("\n\r")
928 Since this is typically only desired when reading text one line at a time, using
929 ``S.rstrip()`` this way works well.
931 For older versions of Python, There are two partial substitutes:
933 - If you want to remove all trailing whitespace, use the ``rstrip()`` method of
934 string objects. This removes all trailing whitespace, not just a single
937 - Otherwise, if there is only one line in the string ``S``, use
938 ``S.splitlines()[0]``.
941 Is there a scanf() or sscanf() equivalent?
942 ------------------------------------------
946 For simple input parsing, the easiest approach is usually to split the line into
947 whitespace-delimited words using the :meth:`~str.split` method of string objects
948 and then convert decimal strings to numeric values using :func:`int` or
949 :func:`float`. ``split()`` supports an optional "sep" parameter which is useful
950 if the line uses something other than whitespace as a separator.
952 For more complicated input parsing, regular expressions more powerful than C's
953 :cfunc:`sscanf` and better suited for the task.
956 What does 'UnicodeError: ASCII [decoding,encoding] error: ordinal not in range(128)' mean?
957 ------------------------------------------------------------------------------------------
959 This error indicates that your Python installation can handle only 7-bit ASCII
960 strings. There are a couple ways to fix or work around the problem.
962 If your programs must handle data in arbitrary character set encodings, the
963 environment the application runs in will generally identify the encoding of the
964 data it is handing you. You need to convert the input to Unicode data using
965 that encoding. For example, a program that handles email or web input will
966 typically find character set encoding information in Content-Type headers. This
967 can then be used to properly convert input data to Unicode. Assuming the string
968 referred to by ``value`` is encoded as UTF-8::
970 value = unicode(value, "utf-8")
972 will return a Unicode object. If the data is not correctly encoded as UTF-8,
973 the above call will raise a :exc:`UnicodeError` exception.
975 If you only want strings converted to Unicode which have non-ASCII data, you can
976 try converting them first assuming an ASCII encoding, and then generate Unicode
977 objects if that fails::
980 x = unicode(value, "ascii")
982 value = unicode(value, "utf-8")
984 # value was valid ASCII data
987 It's possible to set a default encoding in a file called ``sitecustomize.py``
988 that's part of the Python library. However, this isn't recommended because
989 changing the Python-wide default encoding may cause third-party extension
992 Note that on Windows, there is an encoding known as "mbcs", which uses an
993 encoding specific to your current locale. In many cases, and particularly when
994 working with COM, this may be an appropriate default encoding to use.
997 Sequences (Tuples/Lists)
998 ========================
1000 How do I convert between tuples and lists?
1001 ------------------------------------------
1003 The type constructor ``tuple(seq)`` converts any sequence (actually, any
1004 iterable) into a tuple with the same items in the same order.
1006 For example, ``tuple([1, 2, 3])`` yields ``(1, 2, 3)`` and ``tuple('abc')``
1007 yields ``('a', 'b', 'c')``. If the argument is a tuple, it does not make a copy
1008 but returns the same object, so it is cheap to call :func:`tuple` when you
1009 aren't sure that an object is already a tuple.
1011 The type constructor ``list(seq)`` converts any sequence or iterable into a list
1012 with the same items in the same order. For example, ``list((1, 2, 3))`` yields
1013 ``[1, 2, 3]`` and ``list('abc')`` yields ``['a', 'b', 'c']``. If the argument
1014 is a list, it makes a copy just like ``seq[:]`` would.
1017 What's a negative index?
1018 ------------------------
1020 Python sequences are indexed with positive numbers and negative numbers. For
1021 positive numbers 0 is the first index 1 is the second index and so forth. For
1022 negative indices -1 is the last index and -2 is the penultimate (next to last)
1023 index and so forth. Think of ``seq[-n]`` as the same as ``seq[len(seq)-n]``.
1025 Using negative indices can be very convenient. For example ``S[:-1]`` is all of
1026 the string except for its last character, which is useful for removing the
1027 trailing newline from a string.
1030 How do I iterate over a sequence in reverse order?
1031 --------------------------------------------------
1033 Use the :func:`reversed` builtin function, which is new in Python 2.4::
1035 for x in reversed(sequence):
1036 ... # do something with x...
1038 This won't touch your original sequence, but build a new copy with reversed
1039 order to iterate over.
1041 With Python 2.3, you can use an extended slice syntax::
1043 for x in sequence[::-1]:
1044 ... # do something with x...
1047 How do you remove duplicates from a list?
1048 -----------------------------------------
1050 See the Python Cookbook for a long discussion of many ways to do this:
1052 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
1054 If you don't mind reordering the list, sort it and then scan from the end of the
1055 list, deleting duplicates as you go::
1060 for i in range(len(List)-2, -1, -1):
1066 If all elements of the list may be used as dictionary keys (i.e. they are all
1067 hashable) this is often faster ::
1074 In Python 2.5 and later, the following is possible instead::
1076 List = list(set(List))
1078 This converts the list into a set, thereby removing duplicates, and then back
1082 How do you make an array in Python?
1083 -----------------------------------
1087 ["this", 1, "is", "an", "array"]
1089 Lists are equivalent to C or Pascal arrays in their time complexity; the primary
1090 difference is that a Python list can contain objects of many different types.
1092 The ``array`` module also provides methods for creating arrays of fixed types
1093 with compact representations, but they are slower to index than lists. Also
1094 note that the Numeric extensions and others define array-like structures with
1095 various characteristics as well.
1097 To get Lisp-style linked lists, you can emulate cons cells using tuples::
1099 lisp_list = ("like", ("this", ("example", None) ) )
1101 If mutability is desired, you could use lists instead of tuples. Here the
1102 analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is
1103 ``lisp_list[1]``. Only do this if you're sure you really need to, because it's
1104 usually a lot slower than using Python lists.
1107 How do I create a multidimensional list?
1108 ----------------------------------------
1110 You probably tried to make a multidimensional array like this::
1112 A = [[None] * 2] * 3
1114 This looks correct if you print it::
1117 [[None, None], [None, None], [None, None]]
1119 But when you assign a value, it shows up in multiple places:
1123 [[5, None], [5, None], [5, None]]
1125 The reason is that replicating a list with ``*`` doesn't create copies, it only
1126 creates references to the existing objects. The ``*3`` creates a list
1127 containing 3 references to the same list of length two. Changes to one row will
1128 show in all rows, which is almost certainly not what you want.
1130 The suggested approach is to create a list of the desired length first and then
1131 fill in each element with a newly created list::
1137 This generates a list containing 3 different lists of length two. You can also
1138 use a list comprehension::
1141 A = [[None] * w for i in range(h)]
1143 Or, you can use an extension that provides a matrix datatype; `Numeric Python
1144 <http://numpy.scipy.org/>`_ is the best known.
1147 How do I apply a method to a sequence of objects?
1148 -------------------------------------------------
1150 Use a list comprehension::
1152 result = [obj.method() for obj in List]
1154 More generically, you can try the following function::
1156 def method_map(objects, method, arguments):
1157 """method_map([a,b], "meth", (1,2)) gives [a.meth(1,2), b.meth(1,2)]"""
1158 nobjects = len(objects)
1159 methods = map(getattr, objects, [method]*nobjects)
1160 return map(apply, methods, [arguments]*nobjects)
1166 How can I get a dictionary to display its keys in a consistent order?
1167 ---------------------------------------------------------------------
1169 You can't. Dictionaries store their keys in an unpredictable order, so the
1170 display order of a dictionary's elements will be similarly unpredictable.
1172 This can be frustrating if you want to save a printable version to a file, make
1173 some changes and then compare it with some other printed dictionary. In this
1174 case, use the ``pprint`` module to pretty-print the dictionary; the items will
1175 be presented in order sorted by the key.
1177 A more complicated solution is to subclass ``UserDict.UserDict`` to create a
1178 ``SortedDict`` class that prints itself in a predictable order. Here's one
1179 simpleminded implementation of such a class::
1181 import UserDict, string
1183 class SortedDict(UserDict.UserDict):
1186 append = result.append
1187 keys = self.data.keys()
1190 append("%s: %s" % (`k`, `self.data[k]`))
1191 return "{%s}" % string.join(result, ", ")
1195 This will work for many common situations you might encounter, though it's far
1196 from a perfect solution. The largest flaw is that if some values in the
1197 dictionary are also dictionaries, their values won't be presented in any
1201 I want to do a complicated sort: can you do a Schwartzian Transform in Python?
1202 ------------------------------------------------------------------------------
1204 The technique, attributed to Randal Schwartz of the Perl community, sorts the
1205 elements of a list by a metric which maps each element to its "sort value". In
1206 Python, just use the ``key`` argument for the ``sort()`` method::
1209 Isorted.sort(key=lambda s: int(s[10:15]))
1211 The ``key`` argument is new in Python 2.4, for older versions this kind of
1212 sorting is quite simple to do with list comprehensions. To sort a list of
1213 strings by their uppercase values::
1215 tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform
1217 Usorted = [x[1] for x in tmp1]
1219 To sort by the integer value of a subfield extending from positions 10-15 in
1222 tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform
1224 Isorted = [x[1] for x in tmp2]
1226 Note that Isorted may also be computed by ::
1229 return int(s[10:15])
1232 return cmp(intfield(s1), intfield(s2))
1237 but since this method calls ``intfield()`` many times for each element of L, it
1238 is slower than the Schwartzian Transform.
1241 How can I sort one list by values from another list?
1242 ----------------------------------------------------
1244 Merge them into a single list of tuples, sort the resulting list, and then pick
1245 out the element you want. ::
1247 >>> list1 = ["what", "I'm", "sorting", "by"]
1248 >>> list2 = ["something", "else", "to", "sort"]
1249 >>> pairs = zip(list1, list2)
1251 [('what', 'something'), ("I'm", 'else'), ('sorting', 'to'), ('by', 'sort')]
1253 >>> result = [ x[1] for x in pairs ]
1255 ['else', 'sort', 'to', 'something']
1257 An alternative for the last step is::
1260 for p in pairs: result.append(p[1])
1262 If you find this more legible, you might prefer to use this instead of the final
1263 list comprehension. However, it is almost twice as slow for long lists. Why?
1264 First, the ``append()`` operation has to reallocate memory, and while it uses
1265 some tricks to avoid doing that each time, it still has to do it occasionally,
1266 and that costs quite a bit. Second, the expression "result.append" requires an
1267 extra attribute lookup, and third, there's a speed reduction from having to make
1268 all those function calls.
1277 A class is the particular object type created by executing a class statement.
1278 Class objects are used as templates to create instance objects, which embody
1279 both the data (attributes) and code (methods) specific to a datatype.
1281 A class can be based on one or more other classes, called its base class(es). It
1282 then inherits the attributes and methods of its base classes. This allows an
1283 object model to be successively refined by inheritance. You might have a
1284 generic ``Mailbox`` class that provides basic accessor methods for a mailbox,
1285 and subclasses such as ``MboxMailbox``, ``MaildirMailbox``, ``OutlookMailbox``
1286 that handle various specific mailbox formats.
1292 A method is a function on some object ``x`` that you normally call as
1293 ``x.name(arguments...)``. Methods are defined as functions inside the class
1297 def meth (self, arg):
1298 return arg * 2 + self.attribute
1304 Self is merely a conventional name for the first argument of a method. A method
1305 defined as ``meth(self, a, b, c)`` should be called as ``x.meth(a, b, c)`` for
1306 some instance ``x`` of the class in which the definition occurs; the called
1307 method will think it is called as ``meth(x, a, b, c)``.
1309 See also :ref:`why-self`.
1312 How do I check if an object is an instance of a given class or of a subclass of it?
1313 -----------------------------------------------------------------------------------
1315 Use the built-in function ``isinstance(obj, cls)``. You can check if an object
1316 is an instance of any of a number of classes by providing a tuple instead of a
1317 single class, e.g. ``isinstance(obj, (class1, class2, ...))``, and can also
1318 check whether an object is one of Python's built-in types, e.g.
1319 ``isinstance(obj, str)`` or ``isinstance(obj, (int, long, float, complex))``.
1321 Note that most programs do not use :func:`isinstance` on user-defined classes
1322 very often. If you are developing the classes yourself, a more proper
1323 object-oriented style is to define methods on the classes that encapsulate a
1324 particular behaviour, instead of checking the object's class and doing a
1325 different thing based on what class it is. For example, if you have a function
1326 that does something::
1329 if isinstance(obj, Mailbox):
1330 # ... code to search a mailbox
1331 elif isinstance(obj, Document):
1332 # ... code to search a document
1335 A better approach is to define a ``search()`` method on all the classes and just
1340 # ... code to search a mailbox
1344 # ... code to search a document
1352 Delegation is an object oriented technique (also called a design pattern).
1353 Let's say you have an object ``x`` and want to change the behaviour of just one
1354 of its methods. You can create a new class that provides a new implementation
1355 of the method you're interested in changing and delegates all other methods to
1356 the corresponding method of ``x``.
1358 Python programmers can easily implement delegation. For example, the following
1359 class implements a class that behaves like a file but converts all written data
1364 def __init__(self, outfile):
1365 self._outfile = outfile
1368 self._outfile.write(s.upper())
1370 def __getattr__(self, name):
1371 return getattr(self._outfile, name)
1373 Here the ``UpperOut`` class redefines the ``write()`` method to convert the
1374 argument string to uppercase before calling the underlying
1375 ``self.__outfile.write()`` method. All other methods are delegated to the
1376 underlying ``self.__outfile`` object. The delegation is accomplished via the
1377 ``__getattr__`` method; consult :ref:`the language reference <attribute-access>`
1378 for more information about controlling attribute access.
1380 Note that for more general cases delegation can get trickier. When attributes
1381 must be set as well as retrieved, the class must define a :meth:`__setattr__`
1382 method too, and it must do so carefully. The basic implementation of
1383 :meth:`__setattr__` is roughly equivalent to the following::
1387 def __setattr__(self, name, value):
1388 self.__dict__[name] = value
1391 Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store
1392 local state for self without causing an infinite recursion.
1395 How do I call a method defined in a base class from a derived class that overrides it?
1396 --------------------------------------------------------------------------------------
1398 If you're using new-style classes, use the built-in :func:`super` function::
1400 class Derived(Base):
1402 super(Derived, self).meth()
1404 If you're using classic classes: For a class definition such as ``class
1405 Derived(Base): ...`` you can call method ``meth()`` defined in ``Base`` (or one
1406 of ``Base``'s base classes) as ``Base.meth(self, arguments...)``. Here,
1407 ``Base.meth`` is an unbound method, so you need to provide the ``self``
1411 How can I organize my code to make it easier to change the base class?
1412 ----------------------------------------------------------------------
1414 You could define an alias for the base class, assign the real base class to it
1415 before your class definition, and use the alias throughout your class. Then all
1416 you have to change is the value assigned to the alias. Incidentally, this trick
1417 is also handy if you want to decide dynamically (e.g. depending on availability
1418 of resources) which base class to use. Example::
1420 BaseAlias = <real base class>
1422 class Derived(BaseAlias):
1424 BaseAlias.meth(self)
1428 How do I create static class data and static class methods?
1429 -----------------------------------------------------------
1431 Static data (in the sense of C++ or Java) is easy; static methods (again in the
1432 sense of C++ or Java) are not supported directly.
1434 For static data, simply define a class attribute. To assign a new value to the
1435 attribute, you have to explicitly use the class name in the assignment::
1438 count = 0 # number of times C.__init__ called
1441 C.count = C.count + 1
1444 return C.count # or return self.count
1446 ``c.count`` also refers to ``C.count`` for any ``c`` such that ``isinstance(c,
1447 C)`` holds, unless overridden by ``c`` itself or by some class on the base-class
1448 search path from ``c.__class__`` back to ``C``.
1450 Caution: within a method of C, an assignment like ``self.count = 42`` creates a
1451 new and unrelated instance vrbl named "count" in ``self``'s own dict. Rebinding
1452 of a class-static data name must always specify the class whether inside a
1457 Static methods are possible since Python 2.2::
1460 def static(arg1, arg2, arg3):
1461 # No 'self' parameter!
1463 static = staticmethod(static)
1465 With Python 2.4's decorators, this can also be written as ::
1469 def static(arg1, arg2, arg3):
1470 # No 'self' parameter!
1473 However, a far more straightforward way to get the effect of a static method is
1474 via a simple module-level function::
1479 If your code is structured so as to define one class (or tightly related class
1480 hierarchy) per module, this supplies the desired encapsulation.
1483 How can I overload constructors (or methods) in Python?
1484 -------------------------------------------------------
1486 This answer actually applies to all methods, but the question usually comes up
1487 first in the context of constructors.
1494 C() { cout << "No arguments\n"; }
1495 C(int i) { cout << "Argument is " << i << "\n"; }
1498 In Python you have to write a single constructor that catches all cases using
1499 default arguments. For example::
1502 def __init__(self, i=None):
1504 print "No arguments"
1506 print "Argument is", i
1508 This is not entirely equivalent, but close enough in practice.
1510 You could also try a variable-length argument list, e.g. ::
1512 def __init__(self, *args):
1515 The same approach works for all method definitions.
1518 I try to use __spam and I get an error about _SomeClassName__spam.
1519 ------------------------------------------------------------------
1521 Variable names with double leading underscores are "mangled" to provide a simple
1522 but effective way to define class private variables. Any identifier of the form
1523 ``__spam`` (at least two leading underscores, at most one trailing underscore)
1524 is textually replaced with ``_classname__spam``, where ``classname`` is the
1525 current class name with any leading underscores stripped.
1527 This doesn't guarantee privacy: an outside user can still deliberately access
1528 the "_classname__spam" attribute, and private values are visible in the object's
1529 ``__dict__``. Many Python programmers never bother to use private variable
1533 My class defines __del__ but it is not called when I delete the object.
1534 -----------------------------------------------------------------------
1536 There are several possible reasons for this.
1538 The del statement does not necessarily call :meth:`__del__` -- it simply
1539 decrements the object's reference count, and if this reaches zero
1540 :meth:`__del__` is called.
1542 If your data structures contain circular links (e.g. a tree where each child has
1543 a parent reference and each parent has a list of children) the reference counts
1544 will never go back to zero. Once in a while Python runs an algorithm to detect
1545 such cycles, but the garbage collector might run some time after the last
1546 reference to your data structure vanishes, so your :meth:`__del__` method may be
1547 called at an inconvenient and random time. This is inconvenient if you're trying
1548 to reproduce a problem. Worse, the order in which object's :meth:`__del__`
1549 methods are executed is arbitrary. You can run :func:`gc.collect` to force a
1550 collection, but there *are* pathological cases where objects will never be
1553 Despite the cycle collector, it's still a good idea to define an explicit
1554 ``close()`` method on objects to be called whenever you're done with them. The
1555 ``close()`` method can then remove attributes that refer to subobjecs. Don't
1556 call :meth:`__del__` directly -- :meth:`__del__` should call ``close()`` and
1557 ``close()`` should make sure that it can be called more than once for the same
1560 Another way to avoid cyclical references is to use the :mod:`weakref` module,
1561 which allows you to point to objects without incrementing their reference count.
1562 Tree data structures, for instance, should use weak references for their parent
1563 and sibling references (if they need them!).
1565 If the object has ever been a local variable in a function that caught an
1566 expression in an except clause, chances are that a reference to the object still
1567 exists in that function's stack frame as contained in the stack trace.
1568 Normally, calling :func:`sys.exc_clear` will take care of this by clearing the
1569 last recorded exception.
1571 Finally, if your :meth:`__del__` method raises an exception, a warning message
1572 is printed to :data:`sys.stderr`.
1575 How do I get a list of all instances of a given class?
1576 ------------------------------------------------------
1578 Python does not keep track of all instances of a class (or of a built-in type).
1579 You can program the class's constructor to keep track of all instances by
1580 keeping a list of weak references to each instance.
1586 How do I create a .pyc file?
1587 ----------------------------
1589 When a module is imported for the first time (or when the source is more recent
1590 than the current compiled file) a ``.pyc`` file containing the compiled code
1591 should be created in the same directory as the ``.py`` file.
1593 One reason that a ``.pyc`` file may not be created is permissions problems with
1594 the directory. This can happen, for example, if you develop as one user but run
1595 as another, such as if you are testing with a web server. Creation of a .pyc
1596 file is automatic if you're importing a module and Python has the ability
1597 (permissions, free space, etc...) to write the compiled module back to the
1600 Running Python on a top level script is not considered an import and no ``.pyc``
1601 will be created. For example, if you have a top-level module ``abc.py`` that
1602 imports another module ``xyz.py``, when you run abc, ``xyz.pyc`` will be created
1603 since xyz is imported, but no ``abc.pyc`` file will be created since ``abc.py``
1604 isn't being imported.
1606 If you need to create abc.pyc -- that is, to create a .pyc file for a module
1607 that is not imported -- you can, using the :mod:`py_compile` and
1608 :mod:`compileall` modules.
1610 The :mod:`py_compile` module can manually compile any module. One way is to use
1611 the ``compile()`` function in that module interactively::
1613 >>> import py_compile
1614 >>> py_compile.compile('abc.py')
1616 This will write the ``.pyc`` to the same location as ``abc.py`` (or you can
1617 override that with the optional parameter ``cfile``).
1619 You can also automatically compile all files in a directory or directories using
1620 the :mod:`compileall` module. You can do it from the shell prompt by running
1621 ``compileall.py`` and providing the path of a directory containing Python files
1624 python -m compileall .
1627 How do I find the current module name?
1628 --------------------------------------
1630 A module can find out its own module name by looking at the predefined global
1631 variable ``__name__``. If this has the value ``'__main__'``, the program is
1632 running as a script. Many modules that are usually used by importing them also
1633 provide a command-line interface or a self-test, and only execute this code
1634 after checking ``__name__``::
1637 print 'Running test...'
1640 if __name__ == '__main__':
1644 How can I have modules that mutually import each other?
1645 -------------------------------------------------------
1647 Suppose you have the following modules:
1651 from bar import bar_var
1656 from foo import foo_var
1659 The problem is that the interpreter will perform the following steps:
1662 * Empty globals for foo are created
1663 * foo is compiled and starts executing
1665 * Empty globals for bar are created
1666 * bar is compiled and starts executing
1667 * bar imports foo (which is a no-op since there already is a module named foo)
1668 * bar.foo_var = foo.foo_var
1670 The last step fails, because Python isn't done with interpreting ``foo`` yet and
1671 the global symbol dictionary for ``foo`` is still empty.
1673 The same thing happens when you use ``import foo``, and then try to access
1674 ``foo.foo_var`` in global code.
1676 There are (at least) three possible workarounds for this problem.
1678 Guido van Rossum recommends avoiding all uses of ``from <module> import ...``,
1679 and placing all code inside functions. Initializations of global variables and
1680 class variables should use constants or built-in functions only. This means
1681 everything from an imported module is referenced as ``<module>.<name>``.
1683 Jim Roskind suggests performing steps in the following order in each module:
1685 * exports (globals, functions, and classes that don't need imported base
1687 * ``import`` statements
1688 * active code (including globals that are initialized from imported values).
1690 van Rossum doesn't like this approach much because the imports appear in a
1691 strange place, but it does work.
1693 Matthias Urlichs recommends restructuring your code so that the recursive import
1694 is not necessary in the first place.
1696 These solutions are not mutually exclusive.
1699 __import__('x.y.z') returns <module 'x'>; how do I get z?
1700 ---------------------------------------------------------
1704 __import__('x.y.z').y.z
1706 For more realistic situations, you may have to do something like ::
1709 for i in s.split(".")[1:]:
1712 See :mod:`importlib` for a convenience function called
1713 :func:`~importlib.import_module`.
1717 When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
1718 -------------------------------------------------------------------------------------------------
1720 For reasons of efficiency as well as consistency, Python only reads the module
1721 file on the first time a module is imported. If it didn't, in a program
1722 consisting of many modules where each one imports the same basic module, the
1723 basic module would be parsed and re-parsed many times. To force rereading of a
1724 changed module, do this::
1729 Warning: this technique is not 100% fool-proof. In particular, modules
1730 containing statements like ::
1732 from modname import some_objects
1734 will continue to work with the old version of the imported objects. If the
1735 module contains class definitions, existing class instances will *not* be
1736 updated to use the new class definition. This can result in the following
1737 paradoxical behaviour:
1740 >>> c = cls.C() # Create an instance of C
1742 <module 'cls' from 'cls.pyc'>
1743 >>> isinstance(c, cls.C) # isinstance is false?!?
1746 The nature of the problem is made clear if you print out the class objects:
1749 <class cls.C at 0x7352a0>
1751 <class cls.C at 0x4198d0>