blob: ed03c49438880593711ec053b8bc91e7aabc6d62 [file] [log] [blame]
Georg Brandld7413152009-10-11 21:25:26 +00001:tocdepth: 2
2
3===============
4Programming FAQ
5===============
6
Georg Brandl44ea77b2013-03-28 13:28:44 +01007.. only:: html
8
9 .. contents::
Georg Brandld7413152009-10-11 21:25:26 +000010
11General Questions
12=================
13
14Is there a source code level debugger with breakpoints, single-stepping, etc.?
15------------------------------------------------------------------------------
16
17Yes.
18
Andre Delfinocf48e552019-05-03 13:53:22 -030019Several debuggers for Python are described below, and the built-in function
20:func:`breakpoint` allows you to drop into any of them.
21
Georg Brandld7413152009-10-11 21:25:26 +000022The pdb module is a simple but adequate console-mode debugger for Python. It is
23part of the standard Python library, and is :mod:`documented in the Library
24Reference Manual <pdb>`. You can also write your own debugger by using the code
25for pdb as an example.
26
27The IDLE interactive development environment, which is part of the standard
28Python distribution (normally available as Tools/scripts/idle), includes a
Georg Brandl5e722f62014-10-29 08:55:14 +010029graphical debugger.
Georg Brandld7413152009-10-11 21:25:26 +000030
31PythonWin is a Python IDE that includes a GUI debugger based on pdb. The
Andre Delfino08a48032021-04-28 22:06:53 -030032PythonWin debugger colors breakpoints and has quite a few cool features such as
33debugging non-PythonWin programs. PythonWin is available as part of
34`pywin32 <https://github.com/mhammond/pywin32>`_ project and
35as a part of the
36`ActivePython <https://www.activestate.com/products/python/>`_ distribution.
Georg Brandld7413152009-10-11 21:25:26 +000037
Georg Brandl77fe77d2014-10-29 09:24:54 +010038`Eric <http://eric-ide.python-projects.org/>`_ is an IDE built on PyQt
Georg Brandld7413152009-10-11 21:25:26 +000039and the Scintilla editing component.
40
Andre Delfino08a48032021-04-28 22:06:53 -030041`trepan3k <https://github.com/rocky/python3-trepan/>`_ is a gdb-like debugger.
42
43`Visual Studio Code <https://code.visualstudio.com/>`_ is an IDE with debugging
44tools that integrates with version-control software.
Georg Brandld7413152009-10-11 21:25:26 +000045
46There are a number of commercial Python IDEs that include graphical debuggers.
47They include:
48
Andre Delfino08a48032021-04-28 22:06:53 -030049* `Wing IDE <https://wingware.com/>`_
50* `Komodo IDE <https://www.activestate.com/products/komodo-ide/>`_
51* `PyCharm <https://www.jetbrains.com/pycharm/>`_
Georg Brandld7413152009-10-11 21:25:26 +000052
53
Andre Delfinodea82b62020-09-02 00:21:12 -030054Are there tools to help find bugs or perform static analysis?
Georg Brandld7413152009-10-11 21:25:26 +000055-------------------------------------------------------------
56
57Yes.
58
Andre Delfinodea82b62020-09-02 00:21:12 -030059`Pylint <https://www.pylint.org/>`_ and
60`Pyflakes <https://github.com/PyCQA/pyflakes>`_ do basic checking that will
61help you catch bugs sooner.
Georg Brandld7413152009-10-11 21:25:26 +000062
Andrés Delfinoa3782542018-09-11 02:12:41 -030063Static type checkers such as `Mypy <http://mypy-lang.org/>`_,
64`Pyre <https://pyre-check.org/>`_, and
65`Pytype <https://github.com/google/pytype>`_ can check type hints in Python
66source code.
67
Georg Brandld7413152009-10-11 21:25:26 +000068
69How can I create a stand-alone binary from a Python script?
70-----------------------------------------------------------
71
72You don't need the ability to compile Python to C code if all you want is a
73stand-alone program that users can download and run without having to install
74the Python distribution first. There are a number of tools that determine the
75set of modules required by a program and bind these modules together with a
76Python binary to produce a single executable.
77
78One is to use the freeze tool, which is included in the Python source tree as
79``Tools/freeze``. It converts Python byte code to C arrays; a C compiler you can
80embed all your modules into a new program, which is then linked with the
81standard Python modules.
82
83It works by scanning your source recursively for import statements (in both
84forms) and looking for the modules in the standard Python path as well as in the
85source directory (for built-in modules). It then turns the bytecode for modules
86written in Python into C code (array initializers that can be turned into code
87objects using the marshal module) and creates a custom-made config file that
88only contains those built-in modules which are actually used in the program. It
89then compiles the generated C code and links it with the rest of the Python
90interpreter to form a self-contained binary which acts exactly like your script.
91
92Obviously, freeze requires a C compiler. There are several other utilities
Andre Delfinod28b3462021-04-25 22:10:05 -030093which don't:
Georg Brandld7413152009-10-11 21:25:26 +000094
Andre Delfinod28b3462021-04-25 22:10:05 -030095* `py2exe <http://www.py2exe.org/>`_ for Windows binaries
96* `py2app <https://github.com/ronaldoussoren/py2app>`_ for Mac OS X binaries
97* `cx_Freeze <https://cx-freeze.readthedocs.io/en/latest/>`_ for cross-platform
98 binaries
Georg Brandld7413152009-10-11 21:25:26 +000099
100
101Are there coding standards or a style guide for Python programs?
102----------------------------------------------------------------
103
104Yes. The coding style required for standard library modules is documented as
105:pep:`8`.
106
107
Georg Brandld7413152009-10-11 21:25:26 +0000108Core Language
109=============
110
R. David Murrayc04a6942009-11-14 22:21:32 +0000111Why am I getting an UnboundLocalError when the variable has a value?
112--------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000113
R. David Murrayc04a6942009-11-14 22:21:32 +0000114It can be a surprise to get the UnboundLocalError in previously working
115code when it is modified by adding an assignment statement somewhere in
116the body of a function.
Georg Brandld7413152009-10-11 21:25:26 +0000117
R. David Murrayc04a6942009-11-14 22:21:32 +0000118This code:
Georg Brandld7413152009-10-11 21:25:26 +0000119
R. David Murrayc04a6942009-11-14 22:21:32 +0000120 >>> x = 10
121 >>> def bar():
122 ... print(x)
123 >>> bar()
124 10
Georg Brandld7413152009-10-11 21:25:26 +0000125
R. David Murrayc04a6942009-11-14 22:21:32 +0000126works, but this code:
Georg Brandld7413152009-10-11 21:25:26 +0000127
R. David Murrayc04a6942009-11-14 22:21:32 +0000128 >>> x = 10
129 >>> def foo():
130 ... print(x)
131 ... x += 1
Georg Brandld7413152009-10-11 21:25:26 +0000132
R. David Murrayc04a6942009-11-14 22:21:32 +0000133results in an UnboundLocalError:
Georg Brandld7413152009-10-11 21:25:26 +0000134
R. David Murrayc04a6942009-11-14 22:21:32 +0000135 >>> foo()
136 Traceback (most recent call last):
137 ...
138 UnboundLocalError: local variable 'x' referenced before assignment
139
140This is because when you make an assignment to a variable in a scope, that
141variable becomes local to that scope and shadows any similarly named variable
142in the outer scope. Since the last statement in foo assigns a new value to
143``x``, the compiler recognizes it as a local variable. Consequently when the
R. David Murray18163c32009-11-14 22:27:22 +0000144earlier ``print(x)`` attempts to print the uninitialized local variable and
R. David Murrayc04a6942009-11-14 22:21:32 +0000145an error results.
146
147In the example above you can access the outer scope variable by declaring it
148global:
149
150 >>> x = 10
151 >>> def foobar():
152 ... global x
153 ... print(x)
154 ... x += 1
155 >>> foobar()
156 10
157
158This explicit declaration is required in order to remind you that (unlike the
159superficially analogous situation with class and instance variables) you are
160actually modifying the value of the variable in the outer scope:
161
162 >>> print(x)
163 11
164
165You can do a similar thing in a nested scope using the :keyword:`nonlocal`
166keyword:
167
168 >>> def foo():
169 ... x = 10
170 ... def bar():
171 ... nonlocal x
172 ... print(x)
173 ... x += 1
174 ... bar()
175 ... print(x)
176 >>> foo()
177 10
178 11
Georg Brandld7413152009-10-11 21:25:26 +0000179
180
181What are the rules for local and global variables in Python?
182------------------------------------------------------------
183
184In Python, variables that are only referenced inside a function are implicitly
Robert Collinsbd4dd542015-07-30 06:14:32 +1200185global. If a variable is assigned a value anywhere within the function's body,
186it's assumed to be a local unless explicitly declared as global.
Georg Brandld7413152009-10-11 21:25:26 +0000187
188Though a bit surprising at first, a moment's consideration explains this. On
189one hand, requiring :keyword:`global` for assigned variables provides a bar
190against unintended side-effects. On the other hand, if ``global`` was required
191for all global references, you'd be using ``global`` all the time. You'd have
Georg Brandlc4a55fc2010-02-06 18:46:57 +0000192to declare as global every reference to a built-in function or to a component of
Georg Brandld7413152009-10-11 21:25:26 +0000193an imported module. This clutter would defeat the usefulness of the ``global``
194declaration for identifying side-effects.
195
196
Ezio Melotticad8b0f2013-01-05 00:50:46 +0200197Why do lambdas defined in a loop with different values all return the same result?
198----------------------------------------------------------------------------------
199
200Assume you use a for loop to define a few different lambdas (or even plain
201functions), e.g.::
202
R David Murrayfdf95032013-06-19 16:58:26 -0400203 >>> squares = []
204 >>> for x in range(5):
Serhiy Storchakadba90392016-05-10 12:01:23 +0300205 ... squares.append(lambda: x**2)
Ezio Melotticad8b0f2013-01-05 00:50:46 +0200206
207This gives you a list that contains 5 lambdas that calculate ``x**2``. You
208might expect that, when called, they would return, respectively, ``0``, ``1``,
209``4``, ``9``, and ``16``. However, when you actually try you will see that
210they all return ``16``::
211
212 >>> squares[2]()
213 16
214 >>> squares[4]()
215 16
216
217This happens because ``x`` is not local to the lambdas, but is defined in
218the outer scope, and it is accessed when the lambda is called --- not when it
219is defined. At the end of the loop, the value of ``x`` is ``4``, so all the
220functions now return ``4**2``, i.e. ``16``. You can also verify this by
221changing the value of ``x`` and see how the results of the lambdas change::
222
223 >>> x = 8
224 >>> squares[2]()
225 64
226
227In order to avoid this, you need to save the values in variables local to the
228lambdas, so that they don't rely on the value of the global ``x``::
229
R David Murrayfdf95032013-06-19 16:58:26 -0400230 >>> squares = []
231 >>> for x in range(5):
Serhiy Storchakadba90392016-05-10 12:01:23 +0300232 ... squares.append(lambda n=x: n**2)
Ezio Melotticad8b0f2013-01-05 00:50:46 +0200233
234Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed
235when the lambda is defined so that it has the same value that ``x`` had at
236that point in the loop. This means that the value of ``n`` will be ``0``
237in the first lambda, ``1`` in the second, ``2`` in the third, and so on.
238Therefore each lambda will now return the correct result::
239
240 >>> squares[2]()
241 4
242 >>> squares[4]()
243 16
244
245Note that this behaviour is not peculiar to lambdas, but applies to regular
246functions too.
247
248
Georg Brandld7413152009-10-11 21:25:26 +0000249How do I share global variables across modules?
250------------------------------------------------
251
252The canonical way to share information across modules within a single program is
253to create a special module (often called config or cfg). Just import the config
254module in all modules of your application; the module then becomes available as
255a global name. Because there is only one instance of each module, any changes
256made to the module object get reflected everywhere. For example:
257
258config.py::
259
260 x = 0 # Default value of the 'x' configuration setting
261
262mod.py::
263
264 import config
265 config.x = 1
266
267main.py::
268
269 import config
270 import mod
Georg Brandl62eaaf62009-12-19 17:51:41 +0000271 print(config.x)
Georg Brandld7413152009-10-11 21:25:26 +0000272
273Note that using a module is also the basis for implementing the Singleton design
274pattern, for the same reason.
275
276
277What are the "best practices" for using import in a module?
278-----------------------------------------------------------
279
280In general, don't use ``from modulename import *``. Doing so clutters the
Georg Brandla94ad1e2014-10-06 16:02:09 +0200281importer's namespace, and makes it much harder for linters to detect undefined
282names.
Georg Brandld7413152009-10-11 21:25:26 +0000283
284Import modules at the top of a file. Doing so makes it clear what other modules
285your code requires and avoids questions of whether the module name is in scope.
286Using one import per line makes it easy to add and delete module imports, but
287using multiple imports per line uses less screen space.
288
289It's good practice if you import modules in the following order:
290
Georg Brandl62eaaf62009-12-19 17:51:41 +00002911. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re``
Georg Brandld7413152009-10-11 21:25:26 +00002922. third-party library modules (anything installed in Python's site-packages
293 directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc.
2943. locally-developed modules
295
Georg Brandld7413152009-10-11 21:25:26 +0000296It is sometimes necessary to move imports to a function or class to avoid
297problems with circular imports. Gordon McMillan says:
298
299 Circular imports are fine where both modules use the "import <module>" form
300 of import. They fail when the 2nd module wants to grab a name out of the
301 first ("from module import name") and the import is at the top level. That's
302 because names in the 1st are not yet available, because the first module is
303 busy importing the 2nd.
304
305In this case, if the second module is only used in one function, then the import
306can easily be moved into that function. By the time the import is called, the
307first module will have finished initializing, and the second module can do its
308import.
309
310It may also be necessary to move imports out of the top level of code if some of
311the modules are platform-specific. In that case, it may not even be possible to
312import all of the modules at the top of the file. In this case, importing the
313correct modules in the corresponding platform-specific code is a good option.
314
315Only move imports into a local scope, such as inside a function definition, if
316it's necessary to solve a problem such as avoiding a circular import or are
317trying to reduce the initialization time of a module. This technique is
318especially helpful if many of the imports are unnecessary depending on how the
319program executes. You may also want to move imports into a function if the
320modules are only ever used in that function. Note that loading a module the
321first time may be expensive because of the one time initialization of the
322module, but loading a module multiple times is virtually free, costing only a
323couple of dictionary lookups. Even if the module name has gone out of scope,
324the module is probably available in :data:`sys.modules`.
325
Georg Brandld7413152009-10-11 21:25:26 +0000326
Ezio Melotti898eb822014-07-06 20:53:27 +0300327Why are default values shared between objects?
328----------------------------------------------
329
330This type of bug commonly bites neophyte programmers. Consider this function::
331
332 def foo(mydict={}): # Danger: shared reference to one dict for all calls
333 ... compute something ...
334 mydict[key] = value
335 return mydict
336
337The first time you call this function, ``mydict`` contains a single item. The
338second time, ``mydict`` contains two items because when ``foo()`` begins
339executing, ``mydict`` starts out with an item already in it.
340
341It is often expected that a function call creates new objects for default
342values. This is not what happens. Default values are created exactly once, when
343the function is defined. If that object is changed, like the dictionary in this
344example, subsequent calls to the function will refer to this changed object.
345
346By definition, immutable objects such as numbers, strings, tuples, and ``None``,
347are safe from change. Changes to mutable objects such as dictionaries, lists,
348and class instances can lead to confusion.
349
350Because of this feature, it is good programming practice to not use mutable
351objects as default values. Instead, use ``None`` as the default value and
352inside the function, check if the parameter is ``None`` and create a new
353list/dictionary/whatever if it is. For example, don't write::
354
355 def foo(mydict={}):
356 ...
357
358but::
359
360 def foo(mydict=None):
361 if mydict is None:
362 mydict = {} # create a new dict for local namespace
363
364This feature can be useful. When you have a function that's time-consuming to
365compute, a common technique is to cache the parameters and the resulting value
366of each call to the function, and return the cached value if the same value is
367requested again. This is called "memoizing", and can be implemented like this::
368
Noah Haasis2707e412018-06-16 05:29:11 +0200369 # Callers can only provide two parameters and optionally pass _cache by keyword
370 def expensive(arg1, arg2, *, _cache={}):
Ezio Melotti898eb822014-07-06 20:53:27 +0300371 if (arg1, arg2) in _cache:
372 return _cache[(arg1, arg2)]
373
374 # Calculate the value
375 result = ... expensive computation ...
R David Murray623ae292014-09-28 11:01:11 -0400376 _cache[(arg1, arg2)] = result # Store result in the cache
Ezio Melotti898eb822014-07-06 20:53:27 +0300377 return result
378
379You could use a global variable containing a dictionary instead of the default
380value; it's a matter of taste.
381
382
Georg Brandld7413152009-10-11 21:25:26 +0000383How can I pass optional or keyword parameters from one function to another?
384---------------------------------------------------------------------------
385
386Collect the arguments using the ``*`` and ``**`` specifiers in the function's
387parameter list; this gives you the positional arguments as a tuple and the
388keyword arguments as a dictionary. You can then pass these arguments when
389calling another function by using ``*`` and ``**``::
390
391 def f(x, *args, **kwargs):
392 ...
393 kwargs['width'] = '14.3c'
394 ...
395 g(x, *args, **kwargs)
396
Georg Brandld7413152009-10-11 21:25:26 +0000397
Chris Jerdonekb4309942012-12-25 14:54:44 -0800398.. index::
399 single: argument; difference from parameter
400 single: parameter; difference from argument
401
Chris Jerdonekc2a7fd62012-11-28 02:29:33 -0800402.. _faq-argument-vs-parameter:
403
404What is the difference between arguments and parameters?
405--------------------------------------------------------
406
407:term:`Parameters <parameter>` are defined by the names that appear in a
408function definition, whereas :term:`arguments <argument>` are the values
409actually passed to a function when calling it. Parameters define what types of
410arguments a function can accept. For example, given the function definition::
411
412 def func(foo, bar=None, **kwargs):
413 pass
414
415*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling
416``func``, for example::
417
418 func(42, bar=314, extra=somevar)
419
420the values ``42``, ``314``, and ``somevar`` are arguments.
421
422
R David Murray623ae292014-09-28 11:01:11 -0400423Why did changing list 'y' also change list 'x'?
424------------------------------------------------
425
426If you wrote code like::
427
428 >>> x = []
429 >>> y = x
430 >>> y.append(10)
431 >>> y
432 [10]
433 >>> x
434 [10]
435
436you might be wondering why appending an element to ``y`` changed ``x`` too.
437
438There are two factors that produce this result:
439
4401) Variables are simply names that refer to objects. Doing ``y = x`` doesn't
441 create a copy of the list -- it creates a new variable ``y`` that refers to
442 the same object ``x`` refers to. This means that there is only one object
443 (the list), and both ``x`` and ``y`` refer to it.
4442) Lists are :term:`mutable`, which means that you can change their content.
445
446After the call to :meth:`~list.append`, the content of the mutable object has
447changed from ``[]`` to ``[10]``. Since both the variables refer to the same
R David Murray12dc0d92014-09-29 10:17:28 -0400448object, using either name accesses the modified value ``[10]``.
R David Murray623ae292014-09-28 11:01:11 -0400449
450If we instead assign an immutable object to ``x``::
451
452 >>> x = 5 # ints are immutable
453 >>> y = x
454 >>> x = x + 1 # 5 can't be mutated, we are creating a new object here
455 >>> x
456 6
457 >>> y
458 5
459
460we can see that in this case ``x`` and ``y`` are not equal anymore. This is
461because integers are :term:`immutable`, and when we do ``x = x + 1`` we are not
462mutating the int ``5`` by incrementing its value; instead, we are creating a
463new object (the int ``6``) and assigning it to ``x`` (that is, changing which
464object ``x`` refers to). After this assignment we have two objects (the ints
465``6`` and ``5``) and two variables that refer to them (``x`` now refers to
466``6`` but ``y`` still refers to ``5``).
467
468Some operations (for example ``y.append(10)`` and ``y.sort()``) mutate the
469object, whereas superficially similar operations (for example ``y = y + [10]``
470and ``sorted(y)``) create a new object. In general in Python (and in all cases
471in the standard library) a method that mutates an object will return ``None``
472to help avoid getting the two types of operations confused. So if you
473mistakenly write ``y.sort()`` thinking it will give you a sorted copy of ``y``,
474you'll instead end up with ``None``, which will likely cause your program to
475generate an easily diagnosed error.
476
477However, there is one class of operations where the same operation sometimes
478has different behaviors with different types: the augmented assignment
479operators. For example, ``+=`` mutates lists but not tuples or ints (``a_list
480+= [1, 2, 3]`` is equivalent to ``a_list.extend([1, 2, 3])`` and mutates
481``a_list``, whereas ``some_tuple += (1, 2, 3)`` and ``some_int += 1`` create
482new objects).
483
484In other words:
485
486* If we have a mutable object (:class:`list`, :class:`dict`, :class:`set`,
487 etc.), we can use some specific operations to mutate it and all the variables
488 that refer to it will see the change.
489* If we have an immutable object (:class:`str`, :class:`int`, :class:`tuple`,
490 etc.), all the variables that refer to it will always see the same value,
491 but operations that transform that value into a new value always return a new
492 object.
493
494If you want to know if two variables refer to the same object or not, you can
495use the :keyword:`is` operator, or the built-in function :func:`id`.
496
497
Georg Brandld7413152009-10-11 21:25:26 +0000498How do I write a function with output parameters (call by reference)?
499---------------------------------------------------------------------
500
501Remember that arguments are passed by assignment in Python. Since assignment
502just creates references to objects, there's no alias between an argument name in
503the caller and callee, and so no call-by-reference per se. You can achieve the
504desired effect in a number of ways.
505
5061) By returning a tuple of the results::
507
Jiajie Zhong67acf742020-08-09 03:29:03 +0800508 >>> def func1(a, b):
509 ... a = 'new-value' # a and b are local names
510 ... b = b + 1 # assigned to new objects
511 ... return a, b # return new values
512 ...
513 >>> x, y = 'old-value', 99
514 >>> func1(x, y)
515 ('new-value', 100)
Georg Brandld7413152009-10-11 21:25:26 +0000516
517 This is almost always the clearest solution.
518
5192) By using global variables. This isn't thread-safe, and is not recommended.
520
5213) By passing a mutable (changeable in-place) object::
522
Jiajie Zhong67acf742020-08-09 03:29:03 +0800523 >>> def func2(a):
524 ... a[0] = 'new-value' # 'a' references a mutable list
525 ... a[1] = a[1] + 1 # changes a shared object
526 ...
527 >>> args = ['old-value', 99]
528 >>> func2(args)
529 >>> args
530 ['new-value', 100]
Georg Brandld7413152009-10-11 21:25:26 +0000531
5324) By passing in a dictionary that gets mutated::
533
Jiajie Zhong67acf742020-08-09 03:29:03 +0800534 >>> def func3(args):
535 ... args['a'] = 'new-value' # args is a mutable dictionary
536 ... args['b'] = args['b'] + 1 # change it in-place
537 ...
538 >>> args = {'a': 'old-value', 'b': 99}
539 >>> func3(args)
540 >>> args
541 {'a': 'new-value', 'b': 100}
Georg Brandld7413152009-10-11 21:25:26 +0000542
5435) Or bundle up values in a class instance::
544
Jiajie Zhong67acf742020-08-09 03:29:03 +0800545 >>> class Namespace:
546 ... def __init__(self, /, **args):
547 ... for key, value in args.items():
548 ... setattr(self, key, value)
549 ...
550 >>> def func4(args):
551 ... args.a = 'new-value' # args is a mutable Namespace
552 ... args.b = args.b + 1 # change object in-place
553 ...
554 >>> args = Namespace(a='old-value', b=99)
555 >>> func4(args)
556 >>> vars(args)
557 {'a': 'new-value', 'b': 100}
Georg Brandld7413152009-10-11 21:25:26 +0000558
559
560 There's almost never a good reason to get this complicated.
561
562Your best choice is to return a tuple containing the multiple results.
563
564
565How do you make a higher order function in Python?
566--------------------------------------------------
567
568You have two choices: you can use nested scopes or you can use callable objects.
569For example, suppose you wanted to define ``linear(a,b)`` which returns a
570function ``f(x)`` that computes the value ``a*x+b``. Using nested scopes::
571
572 def linear(a, b):
573 def result(x):
574 return a * x + b
575 return result
576
577Or using a callable object::
578
579 class linear:
580
581 def __init__(self, a, b):
582 self.a, self.b = a, b
583
584 def __call__(self, x):
585 return self.a * x + self.b
586
587In both cases, ::
588
589 taxes = linear(0.3, 2)
590
591gives a callable object where ``taxes(10e6) == 0.3 * 10e6 + 2``.
592
593The callable object approach has the disadvantage that it is a bit slower and
594results in slightly longer code. However, note that a collection of callables
595can share their signature via inheritance::
596
597 class exponential(linear):
598 # __init__ inherited
599 def __call__(self, x):
600 return self.a * (x ** self.b)
601
602Object can encapsulate state for several methods::
603
604 class counter:
605
606 value = 0
607
608 def set(self, x):
609 self.value = x
610
611 def up(self):
612 self.value = self.value + 1
613
614 def down(self):
615 self.value = self.value - 1
616
617 count = counter()
618 inc, dec, reset = count.up, count.down, count.set
619
620Here ``inc()``, ``dec()`` and ``reset()`` act like functions which share the
621same counting variable.
622
623
624How do I copy an object in Python?
625----------------------------------
626
627In general, try :func:`copy.copy` or :func:`copy.deepcopy` for the general case.
628Not all objects can be copied, but most can.
629
630Some objects can be copied more easily. Dictionaries have a :meth:`~dict.copy`
631method::
632
633 newdict = olddict.copy()
634
635Sequences can be copied by slicing::
636
637 new_l = l[:]
638
639
640How can I find the methods or attributes of an object?
641------------------------------------------------------
642
643For an instance x of a user-defined class, ``dir(x)`` returns an alphabetized
644list of the names containing the instance attributes and methods and attributes
645defined by its class.
646
647
648How can my code discover the name of an object?
649-----------------------------------------------
650
651Generally speaking, it can't, because objects don't really have names.
avinassh3aa48b82019-08-29 11:10:50 +0530652Essentially, assignment always binds a name to a value; the same is true of
Georg Brandld7413152009-10-11 21:25:26 +0000653``def`` and ``class`` statements, but in that case the value is a
654callable. Consider the following code::
655
Serhiy Storchakadba90392016-05-10 12:01:23 +0300656 >>> class A:
657 ... pass
658 ...
659 >>> B = A
660 >>> a = B()
661 >>> b = a
662 >>> print(b)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000663 <__main__.A object at 0x16D07CC>
Serhiy Storchakadba90392016-05-10 12:01:23 +0300664 >>> print(a)
Georg Brandl62eaaf62009-12-19 17:51:41 +0000665 <__main__.A object at 0x16D07CC>
Georg Brandld7413152009-10-11 21:25:26 +0000666
667Arguably the class has a name: even though it is bound to two names and invoked
668through the name B the created instance is still reported as an instance of
669class A. However, it is impossible to say whether the instance's name is a or
670b, since both names are bound to the same value.
671
672Generally speaking it should not be necessary for your code to "know the names"
673of particular values. Unless you are deliberately writing introspective
674programs, this is usually an indication that a change of approach might be
675beneficial.
676
677In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to
678this question:
679
680 The same way as you get the name of that cat you found on your porch: the cat
681 (object) itself cannot tell you its name, and it doesn't really care -- so
682 the only way to find out what it's called is to ask all your neighbours
683 (namespaces) if it's their cat (object)...
684
685 ....and don't be surprised if you'll find that it's known by many names, or
686 no name at all!
687
688
689What's up with the comma operator's precedence?
690-----------------------------------------------
691
692Comma is not an operator in Python. Consider this session::
693
694 >>> "a" in "b", "a"
Georg Brandl62eaaf62009-12-19 17:51:41 +0000695 (False, 'a')
Georg Brandld7413152009-10-11 21:25:26 +0000696
697Since the comma is not an operator, but a separator between expressions the
698above is evaluated as if you had entered::
699
R David Murrayfdf95032013-06-19 16:58:26 -0400700 ("a" in "b"), "a"
Georg Brandld7413152009-10-11 21:25:26 +0000701
702not::
703
R David Murrayfdf95032013-06-19 16:58:26 -0400704 "a" in ("b", "a")
Georg Brandld7413152009-10-11 21:25:26 +0000705
706The same is true of the various assignment operators (``=``, ``+=`` etc). They
707are not truly operators but syntactic delimiters in assignment statements.
708
709
710Is there an equivalent of C's "?:" ternary operator?
711----------------------------------------------------
712
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100713Yes, there is. The syntax is as follows::
Georg Brandld7413152009-10-11 21:25:26 +0000714
715 [on_true] if [expression] else [on_false]
716
717 x, y = 50, 25
Georg Brandld7413152009-10-11 21:25:26 +0000718 small = x if x < y else y
719
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100720Before this syntax was introduced in Python 2.5, a common idiom was to use
721logical operators::
Georg Brandld7413152009-10-11 21:25:26 +0000722
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100723 [expression] and [on_true] or [on_false]
Georg Brandld7413152009-10-11 21:25:26 +0000724
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100725However, this idiom is unsafe, as it can give wrong results when *on_true*
726has a false boolean value. Therefore, it is always better to use
727the ``... if ... else ...`` form.
Georg Brandld7413152009-10-11 21:25:26 +0000728
729
730Is it possible to write obfuscated one-liners in Python?
731--------------------------------------------------------
732
733Yes. Usually this is done by nesting :keyword:`lambda` within
Serhiy Storchaka2b57c432018-12-19 08:09:46 +0200734:keyword:`!lambda`. See the following three examples, due to Ulf Bartelt::
Georg Brandld7413152009-10-11 21:25:26 +0000735
Georg Brandl62eaaf62009-12-19 17:51:41 +0000736 from functools import reduce
737
Georg Brandld7413152009-10-11 21:25:26 +0000738 # Primes < 1000
Georg Brandl62eaaf62009-12-19 17:51:41 +0000739 print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
740 map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))))
Georg Brandld7413152009-10-11 21:25:26 +0000741
742 # First 10 Fibonacci numbers
Georg Brandl62eaaf62009-12-19 17:51:41 +0000743 print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1:
744 f(x,f), range(10))))
Georg Brandld7413152009-10-11 21:25:26 +0000745
746 # Mandelbrot set
Georg Brandl62eaaf62009-12-19 17:51:41 +0000747 print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
Georg Brandld7413152009-10-11 21:25:26 +0000748 Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
749 Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
750 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
751 >=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(
752 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
Georg Brandl62eaaf62009-12-19 17:51:41 +0000753 ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24))
Georg Brandld7413152009-10-11 21:25:26 +0000754 # \___ ___/ \___ ___/ | | |__ lines on screen
755 # V V | |______ columns on screen
756 # | | |__________ maximum of "iterations"
757 # | |_________________ range on y axis
758 # |____________________________ range on x axis
759
760Don't try this at home, kids!
761
762
Lysandros Nikolaou1aeeaeb2019-03-10 12:30:11 +0100763.. _faq-positional-only-arguments:
764
765What does the slash(/) in the parameter list of a function mean?
766----------------------------------------------------------------
767
768A slash in the argument list of a function denotes that the parameters prior to
769it are positional-only. Positional-only parameters are the ones without an
770externally-usable name. Upon calling a function that accepts positional-only
771parameters, arguments are mapped to parameters based solely on their position.
Ammar Askar87d6cd32019-09-21 00:28:49 -0400772For example, :func:`divmod` is a function that accepts positional-only
773parameters. Its documentation looks like this::
Lysandros Nikolaou1aeeaeb2019-03-10 12:30:11 +0100774
Ammar Askar87d6cd32019-09-21 00:28:49 -0400775 >>> help(divmod)
776 Help on built-in function divmod in module builtins:
Lysandros Nikolaou1aeeaeb2019-03-10 12:30:11 +0100777
Ammar Askar87d6cd32019-09-21 00:28:49 -0400778 divmod(x, y, /)
779 Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
Lysandros Nikolaou1aeeaeb2019-03-10 12:30:11 +0100780
Ammar Askar87d6cd32019-09-21 00:28:49 -0400781The slash at the end of the parameter list means that both parameters are
782positional-only. Thus, calling :func:`divmod` with keyword arguments would lead
783to an error::
Lysandros Nikolaou1aeeaeb2019-03-10 12:30:11 +0100784
Ammar Askar87d6cd32019-09-21 00:28:49 -0400785 >>> divmod(x=3, y=4)
Lysandros Nikolaou1aeeaeb2019-03-10 12:30:11 +0100786 Traceback (most recent call last):
787 File "<stdin>", line 1, in <module>
Ammar Askar87d6cd32019-09-21 00:28:49 -0400788 TypeError: divmod() takes no keyword arguments
Lysandros Nikolaou1aeeaeb2019-03-10 12:30:11 +0100789
Lysandros Nikolaou1aeeaeb2019-03-10 12:30:11 +0100790
Georg Brandld7413152009-10-11 21:25:26 +0000791Numbers and strings
792===================
793
794How do I specify hexadecimal and octal integers?
795------------------------------------------------
796
Georg Brandl62eaaf62009-12-19 17:51:41 +0000797To specify an octal digit, precede the octal value with a zero, and then a lower
798or uppercase "o". For example, to set the variable "a" to the octal value "10"
799(8 in decimal), type::
Georg Brandld7413152009-10-11 21:25:26 +0000800
Georg Brandl62eaaf62009-12-19 17:51:41 +0000801 >>> a = 0o10
Georg Brandld7413152009-10-11 21:25:26 +0000802 >>> a
803 8
804
805Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero,
806and then a lower or uppercase "x". Hexadecimal digits can be specified in lower
807or uppercase. For example, in the Python interpreter::
808
809 >>> a = 0xa5
810 >>> a
811 165
812 >>> b = 0XB2
813 >>> b
814 178
815
816
Georg Brandl62eaaf62009-12-19 17:51:41 +0000817Why does -22 // 10 return -3?
818-----------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000819
820It's primarily driven by the desire that ``i % j`` have the same sign as ``j``.
821If you want that, and also want::
822
Georg Brandl62eaaf62009-12-19 17:51:41 +0000823 i == (i // j) * j + (i % j)
Georg Brandld7413152009-10-11 21:25:26 +0000824
825then integer division has to return the floor. C also requires that identity to
Georg Brandl62eaaf62009-12-19 17:51:41 +0000826hold, and then compilers that truncate ``i // j`` need to make ``i % j`` have
827the same sign as ``i``.
Georg Brandld7413152009-10-11 21:25:26 +0000828
829There are few real use cases for ``i % j`` when ``j`` is negative. When ``j``
830is positive, there are many, and in virtually all of them it's more useful for
831``i % j`` to be ``>= 0``. If the clock says 10 now, what did it say 200 hours
832ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to
833bite.
834
835
836How do I convert a string to a number?
837--------------------------------------
838
839For integers, use the built-in :func:`int` type constructor, e.g. ``int('144')
840== 144``. Similarly, :func:`float` converts to floating-point,
841e.g. ``float('144') == 144.0``.
842
843By default, these interpret the number as decimal, so that ``int('0144') ==
Cajetan Rodrigues5aafa542020-04-25 01:39:04 +0200844144`` holds true, and ``int('0x144')`` raises :exc:`ValueError`. ``int(string,
845base)`` takes the base to convert from as a second optional argument, so ``int(
846'0x144', 16) == 324``. If the base is specified as 0, the number is interpreted
847using Python's rules: a leading '0o' indicates octal, and '0x' indicates a hex
848number.
Georg Brandld7413152009-10-11 21:25:26 +0000849
850Do not use the built-in function :func:`eval` if all you need is to convert
851strings to numbers. :func:`eval` will be significantly slower and it presents a
852security risk: someone could pass you a Python expression that might have
853unwanted side effects. For example, someone could pass
854``__import__('os').system("rm -rf $HOME")`` which would erase your home
855directory.
856
857:func:`eval` also has the effect of interpreting numbers as Python expressions,
Georg Brandl62eaaf62009-12-19 17:51:41 +0000858so that e.g. ``eval('09')`` gives a syntax error because Python does not allow
859leading '0' in a decimal number (except '0').
Georg Brandld7413152009-10-11 21:25:26 +0000860
861
862How do I convert a number to a string?
863--------------------------------------
864
865To convert, e.g., the number 144 to the string '144', use the built-in type
866constructor :func:`str`. If you want a hexadecimal or octal representation, use
Georg Brandl62eaaf62009-12-19 17:51:41 +0000867the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see
Martin Panterbc1ee462016-02-13 00:41:37 +0000868the :ref:`f-strings` and :ref:`formatstrings` sections,
869e.g. ``"{:04d}".format(144)`` yields
Eric V. Smith04d8a242014-04-14 07:52:53 -0400870``'0144'`` and ``"{:.3f}".format(1.0/3.0)`` yields ``'0.333'``.
Georg Brandld7413152009-10-11 21:25:26 +0000871
872
873How do I modify a string in place?
874----------------------------------
875
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100876You can't, because strings are immutable. In most situations, you should
877simply construct a new string from the various parts you want to assemble
878it from. However, if you need an object with the ability to modify in-place
Martin Panter7462b6492015-11-02 03:37:02 +0000879unicode data, try using an :class:`io.StringIO` object or the :mod:`array`
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100880module::
Georg Brandld7413152009-10-11 21:25:26 +0000881
R David Murrayfdf95032013-06-19 16:58:26 -0400882 >>> import io
Georg Brandld7413152009-10-11 21:25:26 +0000883 >>> s = "Hello, world"
Antoine Pitrouc5b266e2011-12-03 22:11:11 +0100884 >>> sio = io.StringIO(s)
885 >>> sio.getvalue()
886 'Hello, world'
887 >>> sio.seek(7)
888 7
889 >>> sio.write("there!")
890 6
891 >>> sio.getvalue()
Georg Brandld7413152009-10-11 21:25:26 +0000892 'Hello, there!'
893
894 >>> import array
Georg Brandl62eaaf62009-12-19 17:51:41 +0000895 >>> a = array.array('u', s)
896 >>> print(a)
897 array('u', 'Hello, world')
898 >>> a[0] = 'y'
899 >>> print(a)
R David Murrayfdf95032013-06-19 16:58:26 -0400900 array('u', 'yello, world')
Georg Brandl62eaaf62009-12-19 17:51:41 +0000901 >>> a.tounicode()
Georg Brandld7413152009-10-11 21:25:26 +0000902 'yello, world'
903
904
905How do I use strings to call functions/methods?
906-----------------------------------------------
907
908There are various techniques.
909
910* The best is to use a dictionary that maps strings to functions. The primary
911 advantage of this technique is that the strings do not need to match the names
912 of the functions. This is also the primary technique used to emulate a case
913 construct::
914
915 def a():
916 pass
917
918 def b():
919 pass
920
921 dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
922
923 dispatch[get_input()]() # Note trailing parens to call function
924
925* Use the built-in function :func:`getattr`::
926
927 import foo
928 getattr(foo, 'bar')()
929
930 Note that :func:`getattr` works on any object, including classes, class
931 instances, modules, and so on.
932
933 This is used in several places in the standard library, like this::
934
935 class Foo:
936 def do_foo(self):
937 ...
938
939 def do_bar(self):
940 ...
941
942 f = getattr(foo_instance, 'do_' + opname)
943 f()
944
945
Zackery Spytza22a19f2020-10-16 12:44:17 -0600946* Use :func:`locals` to resolve the function name::
Georg Brandld7413152009-10-11 21:25:26 +0000947
948 def myFunc():
Georg Brandl62eaaf62009-12-19 17:51:41 +0000949 print("hello")
Georg Brandld7413152009-10-11 21:25:26 +0000950
951 fname = "myFunc"
952
953 f = locals()[fname]
954 f()
955
Georg Brandld7413152009-10-11 21:25:26 +0000956
957Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
958-------------------------------------------------------------------------------------
959
Antoine Pitrouf3520402011-12-03 22:19:55 +0100960You can use ``S.rstrip("\r\n")`` to remove all occurrences of any line
961terminator from the end of the string ``S`` without removing other trailing
962whitespace. If the string ``S`` represents more than one line, with several
963empty lines at the end, the line terminators for all the blank lines will
964be removed::
Georg Brandld7413152009-10-11 21:25:26 +0000965
966 >>> lines = ("line 1 \r\n"
967 ... "\r\n"
968 ... "\r\n")
969 >>> lines.rstrip("\n\r")
Georg Brandl62eaaf62009-12-19 17:51:41 +0000970 'line 1 '
Georg Brandld7413152009-10-11 21:25:26 +0000971
972Since this is typically only desired when reading text one line at a time, using
973``S.rstrip()`` this way works well.
974
Georg Brandld7413152009-10-11 21:25:26 +0000975
976Is there a scanf() or sscanf() equivalent?
977------------------------------------------
978
979Not as such.
980
981For simple input parsing, the easiest approach is usually to split the line into
982whitespace-delimited words using the :meth:`~str.split` method of string objects
983and then convert decimal strings to numeric values using :func:`int` or
984:func:`float`. ``split()`` supports an optional "sep" parameter which is useful
985if the line uses something other than whitespace as a separator.
986
Brian Curtin5a7a52f2010-09-23 13:45:21 +0000987For more complicated input parsing, regular expressions are more powerful
Georg Brandl60203b42010-10-06 10:11:56 +0000988than C's :c:func:`sscanf` and better suited for the task.
Georg Brandld7413152009-10-11 21:25:26 +0000989
990
Georg Brandl62eaaf62009-12-19 17:51:41 +0000991What does 'UnicodeDecodeError' or 'UnicodeEncodeError' error mean?
992-------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +0000993
Georg Brandl62eaaf62009-12-19 17:51:41 +0000994See the :ref:`unicode-howto`.
Georg Brandld7413152009-10-11 21:25:26 +0000995
996
Antoine Pitrou432259f2011-12-09 23:10:31 +0100997Performance
998===========
999
1000My program is too slow. How do I speed it up?
1001---------------------------------------------
1002
1003That's a tough one, in general. First, here are a list of things to
1004remember before diving further:
1005
Georg Brandl300a6912012-03-14 22:40:08 +01001006* Performance characteristics vary across Python implementations. This FAQ
Gurupad Hegde6c7bb382019-12-28 17:16:02 -05001007 focuses on :term:`CPython`.
Georg Brandl300a6912012-03-14 22:40:08 +01001008* Behaviour can vary across operating systems, especially when talking about
Antoine Pitrou432259f2011-12-09 23:10:31 +01001009 I/O or multi-threading.
1010* You should always find the hot spots in your program *before* attempting to
1011 optimize any code (see the :mod:`profile` module).
1012* Writing benchmark scripts will allow you to iterate quickly when searching
1013 for improvements (see the :mod:`timeit` module).
1014* It is highly recommended to have good code coverage (through unit testing
1015 or any other technique) before potentially introducing regressions hidden
1016 in sophisticated optimizations.
1017
1018That being said, there are many tricks to speed up Python code. Here are
1019some general principles which go a long way towards reaching acceptable
1020performance levels:
1021
1022* Making your algorithms faster (or changing to faster ones) can yield
1023 much larger benefits than trying to sprinkle micro-optimization tricks
1024 all over your code.
1025
1026* Use the right data structures. Study documentation for the :ref:`bltin-types`
1027 and the :mod:`collections` module.
1028
1029* When the standard library provides a primitive for doing something, it is
1030 likely (although not guaranteed) to be faster than any alternative you
1031 may come up with. This is doubly true for primitives written in C, such
1032 as builtins and some extension types. For example, be sure to use
1033 either the :meth:`list.sort` built-in method or the related :func:`sorted`
Senthil Kumarand03d1d42016-01-01 23:25:58 -08001034 function to do sorting (and see the :ref:`sortinghowto` for examples
Antoine Pitrou432259f2011-12-09 23:10:31 +01001035 of moderately advanced usage).
1036
1037* Abstractions tend to create indirections and force the interpreter to work
1038 more. If the levels of indirection outweigh the amount of useful work
1039 done, your program will be slower. You should avoid excessive abstraction,
1040 especially under the form of tiny functions or methods (which are also often
1041 detrimental to readability).
1042
1043If you have reached the limit of what pure Python can allow, there are tools
1044to take you further away. For example, `Cython <http://cython.org>`_ can
1045compile a slightly modified version of Python code into a C extension, and
1046can be used on many different platforms. Cython can take advantage of
1047compilation (and optional type annotations) to make your code significantly
1048faster than when interpreted. If you are confident in your C programming
1049skills, you can also :ref:`write a C extension module <extending-index>`
1050yourself.
1051
1052.. seealso::
1053 The wiki page devoted to `performance tips
Georg Brandle73778c2014-10-29 08:36:35 +01001054 <https://wiki.python.org/moin/PythonSpeed/PerformanceTips>`_.
Antoine Pitrou432259f2011-12-09 23:10:31 +01001055
1056.. _efficient_string_concatenation:
1057
Antoine Pitroufd9ebd42011-11-25 16:33:53 +01001058What is the most efficient way to concatenate many strings together?
1059--------------------------------------------------------------------
1060
1061:class:`str` and :class:`bytes` objects are immutable, therefore concatenating
1062many strings together is inefficient as each concatenation creates a new
1063object. In the general case, the total runtime cost is quadratic in the
1064total string length.
1065
1066To accumulate many :class:`str` objects, the recommended idiom is to place
1067them into a list and call :meth:`str.join` at the end::
1068
1069 chunks = []
1070 for s in my_strings:
1071 chunks.append(s)
1072 result = ''.join(chunks)
1073
1074(another reasonably efficient idiom is to use :class:`io.StringIO`)
1075
1076To accumulate many :class:`bytes` objects, the recommended idiom is to extend
1077a :class:`bytearray` object using in-place concatenation (the ``+=`` operator)::
1078
1079 result = bytearray()
1080 for b in my_bytes_objects:
1081 result += b
1082
1083
Georg Brandld7413152009-10-11 21:25:26 +00001084Sequences (Tuples/Lists)
1085========================
1086
1087How do I convert between tuples and lists?
1088------------------------------------------
1089
1090The type constructor ``tuple(seq)`` converts any sequence (actually, any
1091iterable) into a tuple with the same items in the same order.
1092
1093For example, ``tuple([1, 2, 3])`` yields ``(1, 2, 3)`` and ``tuple('abc')``
1094yields ``('a', 'b', 'c')``. If the argument is a tuple, it does not make a copy
1095but returns the same object, so it is cheap to call :func:`tuple` when you
1096aren't sure that an object is already a tuple.
1097
1098The type constructor ``list(seq)`` converts any sequence or iterable into a list
1099with the same items in the same order. For example, ``list((1, 2, 3))`` yields
1100``[1, 2, 3]`` and ``list('abc')`` yields ``['a', 'b', 'c']``. If the argument
1101is a list, it makes a copy just like ``seq[:]`` would.
1102
1103
1104What's a negative index?
1105------------------------
1106
1107Python sequences are indexed with positive numbers and negative numbers. For
1108positive numbers 0 is the first index 1 is the second index and so forth. For
1109negative indices -1 is the last index and -2 is the penultimate (next to last)
1110index and so forth. Think of ``seq[-n]`` as the same as ``seq[len(seq)-n]``.
1111
1112Using negative indices can be very convenient. For example ``S[:-1]`` is all of
1113the string except for its last character, which is useful for removing the
1114trailing newline from a string.
1115
1116
1117How do I iterate over a sequence in reverse order?
1118--------------------------------------------------
1119
Andre Delfinofb2e9462020-10-21 05:25:07 -03001120Use the :func:`reversed` built-in function::
Georg Brandld7413152009-10-11 21:25:26 +00001121
1122 for x in reversed(sequence):
Serhiy Storchakadba90392016-05-10 12:01:23 +03001123 ... # do something with x ...
Georg Brandld7413152009-10-11 21:25:26 +00001124
1125This won't touch your original sequence, but build a new copy with reversed
1126order to iterate over.
1127
Georg Brandld7413152009-10-11 21:25:26 +00001128
1129How do you remove duplicates from a list?
1130-----------------------------------------
1131
1132See the Python Cookbook for a long discussion of many ways to do this:
1133
Andre Delfinoe8a20762020-09-26 21:47:25 -03001134 https://code.activestate.com/recipes/52560/
Georg Brandld7413152009-10-11 21:25:26 +00001135
1136If you don't mind reordering the list, sort it and then scan from the end of the
1137list, deleting duplicates as you go::
1138
Georg Brandl62eaaf62009-12-19 17:51:41 +00001139 if mylist:
1140 mylist.sort()
1141 last = mylist[-1]
1142 for i in range(len(mylist)-2, -1, -1):
1143 if last == mylist[i]:
1144 del mylist[i]
Georg Brandld7413152009-10-11 21:25:26 +00001145 else:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001146 last = mylist[i]
Georg Brandld7413152009-10-11 21:25:26 +00001147
Antoine Pitrouf3520402011-12-03 22:19:55 +01001148If all elements of the list may be used as set keys (i.e. they are all
1149:term:`hashable`) this is often faster ::
Georg Brandld7413152009-10-11 21:25:26 +00001150
Georg Brandl62eaaf62009-12-19 17:51:41 +00001151 mylist = list(set(mylist))
Georg Brandld7413152009-10-11 21:25:26 +00001152
1153This converts the list into a set, thereby removing duplicates, and then back
1154into a list.
1155
1156
Terry Jan Reedy5b0181d2020-09-29 01:02:44 -04001157How do you remove multiple items from a list
1158--------------------------------------------
1159
1160As with removing duplicates, explicitly iterating in reverse with a
1161delete condition is one possibility. However, it is easier and faster
1162to use slice replacement with an implicit or explicit forward iteration.
1163Here are three variations.::
1164
1165 mylist[:] = filter(keep_function, mylist)
1166 mylist[:] = (x for x in mylist if keep_condition)
1167 mylist[:] = [x for x in mylist if keep_condition]
1168
Terry Jan Reedy060937d2020-10-05 10:31:44 -04001169The list comprehension may be fastest.
Terry Jan Reedy5b0181d2020-09-29 01:02:44 -04001170
1171
Georg Brandld7413152009-10-11 21:25:26 +00001172How do you make an array in Python?
1173-----------------------------------
1174
1175Use a list::
1176
1177 ["this", 1, "is", "an", "array"]
1178
1179Lists are equivalent to C or Pascal arrays in their time complexity; the primary
1180difference is that a Python list can contain objects of many different types.
1181
1182The ``array`` module also provides methods for creating arrays of fixed types
1183with compact representations, but they are slower to index than lists. Also
Andre Delfinoc8bb2412020-10-01 20:22:14 -03001184note that NumPy and other third party packages define array-like structures with
Georg Brandld7413152009-10-11 21:25:26 +00001185various characteristics as well.
1186
1187To get Lisp-style linked lists, you can emulate cons cells using tuples::
1188
1189 lisp_list = ("like", ("this", ("example", None) ) )
1190
1191If mutability is desired, you could use lists instead of tuples. Here the
1192analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is
1193``lisp_list[1]``. Only do this if you're sure you really need to, because it's
1194usually a lot slower than using Python lists.
1195
1196
Martin Panter7f02d6d2015-09-07 02:08:55 +00001197.. _faq-multidimensional-list:
1198
Georg Brandld7413152009-10-11 21:25:26 +00001199How do I create a multidimensional list?
1200----------------------------------------
1201
1202You probably tried to make a multidimensional array like this::
1203
R David Murrayfdf95032013-06-19 16:58:26 -04001204 >>> A = [[None] * 2] * 3
Georg Brandld7413152009-10-11 21:25:26 +00001205
Senthil Kumaran77493202016-06-04 20:07:34 -07001206This looks correct if you print it:
1207
1208.. testsetup::
1209
1210 A = [[None] * 2] * 3
1211
1212.. doctest::
Georg Brandld7413152009-10-11 21:25:26 +00001213
1214 >>> A
1215 [[None, None], [None, None], [None, None]]
1216
1217But when you assign a value, it shows up in multiple places:
1218
Senthil Kumaran77493202016-06-04 20:07:34 -07001219.. testsetup::
1220
1221 A = [[None] * 2] * 3
1222
1223.. doctest::
1224
1225 >>> A[0][0] = 5
1226 >>> A
1227 [[5, None], [5, None], [5, None]]
Georg Brandld7413152009-10-11 21:25:26 +00001228
1229The reason is that replicating a list with ``*`` doesn't create copies, it only
1230creates references to the existing objects. The ``*3`` creates a list
1231containing 3 references to the same list of length two. Changes to one row will
1232show in all rows, which is almost certainly not what you want.
1233
1234The suggested approach is to create a list of the desired length first and then
1235fill in each element with a newly created list::
1236
1237 A = [None] * 3
1238 for i in range(3):
1239 A[i] = [None] * 2
1240
1241This generates a list containing 3 different lists of length two. You can also
1242use a list comprehension::
1243
1244 w, h = 2, 3
1245 A = [[None] * w for i in range(h)]
1246
Benjamin Peterson6d3ad2f2016-05-26 22:51:32 -07001247Or, you can use an extension that provides a matrix datatype; `NumPy
Ezio Melottic1f58392013-06-09 01:04:21 +03001248<http://www.numpy.org/>`_ is the best known.
Georg Brandld7413152009-10-11 21:25:26 +00001249
1250
1251How do I apply a method to a sequence of objects?
1252-------------------------------------------------
1253
1254Use a list comprehension::
1255
Georg Brandl62eaaf62009-12-19 17:51:41 +00001256 result = [obj.method() for obj in mylist]
Georg Brandld7413152009-10-11 21:25:26 +00001257
Larry Hastings3732ed22014-03-15 21:13:56 -07001258.. _faq-augmented-assignment-tuple-error:
Georg Brandld7413152009-10-11 21:25:26 +00001259
R David Murraybcf06d32013-05-20 10:32:46 -04001260Why does a_tuple[i] += ['item'] raise an exception when the addition works?
1261---------------------------------------------------------------------------
1262
1263This is because of a combination of the fact that augmented assignment
1264operators are *assignment* operators, and the difference between mutable and
1265immutable objects in Python.
1266
1267This discussion applies in general when augmented assignment operators are
1268applied to elements of a tuple that point to mutable objects, but we'll use
1269a ``list`` and ``+=`` as our exemplar.
1270
1271If you wrote::
1272
1273 >>> a_tuple = (1, 2)
1274 >>> a_tuple[0] += 1
1275 Traceback (most recent call last):
1276 ...
1277 TypeError: 'tuple' object does not support item assignment
1278
1279The reason for the exception should be immediately clear: ``1`` is added to the
1280object ``a_tuple[0]`` points to (``1``), producing the result object, ``2``,
1281but when we attempt to assign the result of the computation, ``2``, to element
1282``0`` of the tuple, we get an error because we can't change what an element of
1283a tuple points to.
1284
1285Under the covers, what this augmented assignment statement is doing is
1286approximately this::
1287
R David Murray95ae9922013-05-21 11:44:41 -04001288 >>> result = a_tuple[0] + 1
R David Murraybcf06d32013-05-20 10:32:46 -04001289 >>> a_tuple[0] = result
1290 Traceback (most recent call last):
1291 ...
1292 TypeError: 'tuple' object does not support item assignment
1293
1294It is the assignment part of the operation that produces the error, since a
1295tuple is immutable.
1296
1297When you write something like::
1298
1299 >>> a_tuple = (['foo'], 'bar')
1300 >>> a_tuple[0] += ['item']
1301 Traceback (most recent call last):
1302 ...
1303 TypeError: 'tuple' object does not support item assignment
1304
1305The exception is a bit more surprising, and even more surprising is the fact
1306that even though there was an error, the append worked::
1307
1308 >>> a_tuple[0]
1309 ['foo', 'item']
1310
R David Murray95ae9922013-05-21 11:44:41 -04001311To see why this happens, you need to know that (a) if an object implements an
1312``__iadd__`` magic method, it gets called when the ``+=`` augmented assignment
1313is executed, and its return value is what gets used in the assignment statement;
1314and (b) for lists, ``__iadd__`` is equivalent to calling ``extend`` on the list
1315and returning the list. That's why we say that for lists, ``+=`` is a
1316"shorthand" for ``list.extend``::
R David Murraybcf06d32013-05-20 10:32:46 -04001317
1318 >>> a_list = []
1319 >>> a_list += [1]
1320 >>> a_list
1321 [1]
1322
R David Murray95ae9922013-05-21 11:44:41 -04001323This is equivalent to::
R David Murraybcf06d32013-05-20 10:32:46 -04001324
1325 >>> result = a_list.__iadd__([1])
1326 >>> a_list = result
1327
1328The object pointed to by a_list has been mutated, and the pointer to the
1329mutated object is assigned back to ``a_list``. The end result of the
1330assignment is a no-op, since it is a pointer to the same object that ``a_list``
1331was previously pointing to, but the assignment still happens.
1332
1333Thus, in our tuple example what is happening is equivalent to::
1334
1335 >>> result = a_tuple[0].__iadd__(['item'])
1336 >>> a_tuple[0] = result
1337 Traceback (most recent call last):
1338 ...
1339 TypeError: 'tuple' object does not support item assignment
1340
1341The ``__iadd__`` succeeds, and thus the list is extended, but even though
1342``result`` points to the same object that ``a_tuple[0]`` already points to,
1343that final assignment still results in an error, because tuples are immutable.
1344
1345
Georg Brandld7413152009-10-11 21:25:26 +00001346I want to do a complicated sort: can you do a Schwartzian Transform in Python?
1347------------------------------------------------------------------------------
1348
1349The technique, attributed to Randal Schwartz of the Perl community, sorts the
1350elements of a list by a metric which maps each element to its "sort value". In
Berker Peksag5b6a14d2016-06-01 13:54:33 -07001351Python, use the ``key`` argument for the :meth:`list.sort` method::
Georg Brandld7413152009-10-11 21:25:26 +00001352
1353 Isorted = L[:]
1354 Isorted.sort(key=lambda s: int(s[10:15]))
1355
Georg Brandld7413152009-10-11 21:25:26 +00001356
1357How can I sort one list by values from another list?
1358----------------------------------------------------
1359
Georg Brandl62eaaf62009-12-19 17:51:41 +00001360Merge them into an iterator of tuples, sort the resulting list, and then pick
Georg Brandld7413152009-10-11 21:25:26 +00001361out the element you want. ::
1362
1363 >>> list1 = ["what", "I'm", "sorting", "by"]
1364 >>> list2 = ["something", "else", "to", "sort"]
1365 >>> pairs = zip(list1, list2)
Georg Brandl62eaaf62009-12-19 17:51:41 +00001366 >>> pairs = sorted(pairs)
Georg Brandld7413152009-10-11 21:25:26 +00001367 >>> pairs
Georg Brandl62eaaf62009-12-19 17:51:41 +00001368 [("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]
1369 >>> result = [x[1] for x in pairs]
Georg Brandld7413152009-10-11 21:25:26 +00001370 >>> result
1371 ['else', 'sort', 'to', 'something']
1372
Georg Brandl62eaaf62009-12-19 17:51:41 +00001373
Georg Brandld7413152009-10-11 21:25:26 +00001374Objects
1375=======
1376
1377What is a class?
1378----------------
1379
1380A class is the particular object type created by executing a class statement.
1381Class objects are used as templates to create instance objects, which embody
1382both the data (attributes) and code (methods) specific to a datatype.
1383
1384A class can be based on one or more other classes, called its base class(es). It
1385then inherits the attributes and methods of its base classes. This allows an
1386object model to be successively refined by inheritance. You might have a
1387generic ``Mailbox`` class that provides basic accessor methods for a mailbox,
1388and subclasses such as ``MboxMailbox``, ``MaildirMailbox``, ``OutlookMailbox``
1389that handle various specific mailbox formats.
1390
1391
1392What is a method?
1393-----------------
1394
1395A method is a function on some object ``x`` that you normally call as
1396``x.name(arguments...)``. Methods are defined as functions inside the class
1397definition::
1398
1399 class C:
Serhiy Storchakadba90392016-05-10 12:01:23 +03001400 def meth(self, arg):
Georg Brandld7413152009-10-11 21:25:26 +00001401 return arg * 2 + self.attribute
1402
1403
1404What is self?
1405-------------
1406
1407Self is merely a conventional name for the first argument of a method. A method
1408defined as ``meth(self, a, b, c)`` should be called as ``x.meth(a, b, c)`` for
1409some instance ``x`` of the class in which the definition occurs; the called
1410method will think it is called as ``meth(x, a, b, c)``.
1411
1412See also :ref:`why-self`.
1413
1414
1415How do I check if an object is an instance of a given class or of a subclass of it?
1416-----------------------------------------------------------------------------------
1417
1418Use the built-in function ``isinstance(obj, cls)``. You can check if an object
1419is an instance of any of a number of classes by providing a tuple instead of a
1420single class, e.g. ``isinstance(obj, (class1, class2, ...))``, and can also
1421check whether an object is one of Python's built-in types, e.g.
Georg Brandl62eaaf62009-12-19 17:51:41 +00001422``isinstance(obj, str)`` or ``isinstance(obj, (int, float, complex))``.
Georg Brandld7413152009-10-11 21:25:26 +00001423
Raymond Hettinger7bc25ec2021-04-05 12:48:24 -07001424Note that :func:`isinstance` also checks for virtual inheritance from an
1425:term:`abstract base class`. So, the test will return ``True`` for a
1426registered class even if hasn't directly or indirectly inherited from it. To
1427test for "true inheritance", scan the :term:`MRO` of the class:
1428
1429.. testcode::
1430
1431 from collections.abc import Mapping
1432
1433 class P:
1434 pass
1435
1436 class C(P):
1437 pass
1438
1439 Mapping.register(P)
1440
1441.. doctest::
1442
1443 >>> c = C()
1444 >>> isinstance(c, C) # direct
1445 True
1446 >>> isinstance(c, P) # indirect
1447 True
1448 >>> isinstance(c, Mapping) # virtual
1449 True
1450
1451 # Actual inheritance chain
1452 >>> type(c).__mro__
1453 (<class 'C'>, <class 'P'>, <class 'object'>)
1454
1455 # Test for "true inheritance"
1456 >>> Mapping in type(c).__mro__
1457 False
1458
Georg Brandld7413152009-10-11 21:25:26 +00001459Note that most programs do not use :func:`isinstance` on user-defined classes
1460very often. If you are developing the classes yourself, a more proper
1461object-oriented style is to define methods on the classes that encapsulate a
1462particular behaviour, instead of checking the object's class and doing a
1463different thing based on what class it is. For example, if you have a function
1464that does something::
1465
Georg Brandl62eaaf62009-12-19 17:51:41 +00001466 def search(obj):
Georg Brandld7413152009-10-11 21:25:26 +00001467 if isinstance(obj, Mailbox):
Serhiy Storchakadba90392016-05-10 12:01:23 +03001468 ... # code to search a mailbox
Georg Brandld7413152009-10-11 21:25:26 +00001469 elif isinstance(obj, Document):
Serhiy Storchakadba90392016-05-10 12:01:23 +03001470 ... # code to search a document
Georg Brandld7413152009-10-11 21:25:26 +00001471 elif ...
1472
1473A better approach is to define a ``search()`` method on all the classes and just
1474call it::
1475
1476 class Mailbox:
1477 def search(self):
Serhiy Storchakadba90392016-05-10 12:01:23 +03001478 ... # code to search a mailbox
Georg Brandld7413152009-10-11 21:25:26 +00001479
1480 class Document:
1481 def search(self):
Serhiy Storchakadba90392016-05-10 12:01:23 +03001482 ... # code to search a document
Georg Brandld7413152009-10-11 21:25:26 +00001483
1484 obj.search()
1485
1486
1487What is delegation?
1488-------------------
1489
1490Delegation is an object oriented technique (also called a design pattern).
1491Let's say you have an object ``x`` and want to change the behaviour of just one
1492of its methods. You can create a new class that provides a new implementation
1493of the method you're interested in changing and delegates all other methods to
1494the corresponding method of ``x``.
1495
1496Python programmers can easily implement delegation. For example, the following
1497class implements a class that behaves like a file but converts all written data
1498to uppercase::
1499
1500 class UpperOut:
1501
1502 def __init__(self, outfile):
1503 self._outfile = outfile
1504
1505 def write(self, s):
1506 self._outfile.write(s.upper())
1507
1508 def __getattr__(self, name):
1509 return getattr(self._outfile, name)
1510
1511Here the ``UpperOut`` class redefines the ``write()`` method to convert the
1512argument string to uppercase before calling the underlying
Zackery Spytzcaf1aad2020-04-26 21:23:52 -06001513``self._outfile.write()`` method. All other methods are delegated to the
1514underlying ``self._outfile`` object. The delegation is accomplished via the
Georg Brandld7413152009-10-11 21:25:26 +00001515``__getattr__`` method; consult :ref:`the language reference <attribute-access>`
1516for more information about controlling attribute access.
1517
1518Note that for more general cases delegation can get trickier. When attributes
1519must be set as well as retrieved, the class must define a :meth:`__setattr__`
1520method too, and it must do so carefully. The basic implementation of
1521:meth:`__setattr__` is roughly equivalent to the following::
1522
1523 class X:
1524 ...
1525 def __setattr__(self, name, value):
1526 self.__dict__[name] = value
1527 ...
1528
1529Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store
1530local state for self without causing an infinite recursion.
1531
1532
Andre Delfino778ad922020-09-20 14:09:50 -03001533How do I call a method defined in a base class from a derived class that extends it?
1534------------------------------------------------------------------------------------
Georg Brandld7413152009-10-11 21:25:26 +00001535
Georg Brandl62eaaf62009-12-19 17:51:41 +00001536Use the built-in :func:`super` function::
Georg Brandld7413152009-10-11 21:25:26 +00001537
1538 class Derived(Base):
Serhiy Storchakadba90392016-05-10 12:01:23 +03001539 def meth(self):
Andre Delfino778ad922020-09-20 14:09:50 -03001540 super().meth() # calls Base.meth
Georg Brandld7413152009-10-11 21:25:26 +00001541
Andre Delfino778ad922020-09-20 14:09:50 -03001542In the example, :func:`super` will automatically determine the instance from
1543which it was called (the ``self`` value), look up the :term:`method resolution
1544order` (MRO) with ``type(self).__mro__``, and return the next in line after
1545``Derived`` in the MRO: ``Base``.
Georg Brandld7413152009-10-11 21:25:26 +00001546
1547
1548How can I organize my code to make it easier to change the base class?
1549----------------------------------------------------------------------
1550
Andre Delfino4642ccd2020-10-21 02:25:05 -03001551You could assign the base class to an alias and derive from the alias. Then all
Georg Brandld7413152009-10-11 21:25:26 +00001552you have to change is the value assigned to the alias. Incidentally, this trick
1553is also handy if you want to decide dynamically (e.g. depending on availability
1554of resources) which base class to use. Example::
1555
Andre Delfino4642ccd2020-10-21 02:25:05 -03001556 class Base:
1557 ...
1558
1559 BaseAlias = Base
Georg Brandld7413152009-10-11 21:25:26 +00001560
1561 class Derived(BaseAlias):
Andre Delfino4642ccd2020-10-21 02:25:05 -03001562 ...
Georg Brandld7413152009-10-11 21:25:26 +00001563
1564
1565How do I create static class data and static class methods?
1566-----------------------------------------------------------
1567
Georg Brandl62eaaf62009-12-19 17:51:41 +00001568Both static data and static methods (in the sense of C++ or Java) are supported
1569in Python.
Georg Brandld7413152009-10-11 21:25:26 +00001570
1571For static data, simply define a class attribute. To assign a new value to the
1572attribute, you have to explicitly use the class name in the assignment::
1573
1574 class C:
1575 count = 0 # number of times C.__init__ called
1576
1577 def __init__(self):
1578 C.count = C.count + 1
1579
1580 def getcount(self):
1581 return C.count # or return self.count
1582
1583``c.count`` also refers to ``C.count`` for any ``c`` such that ``isinstance(c,
1584C)`` holds, unless overridden by ``c`` itself or by some class on the base-class
1585search path from ``c.__class__`` back to ``C``.
1586
1587Caution: within a method of C, an assignment like ``self.count = 42`` creates a
Georg Brandl62eaaf62009-12-19 17:51:41 +00001588new and unrelated instance named "count" in ``self``'s own dict. Rebinding of a
1589class-static data name must always specify the class whether inside a method or
1590not::
Georg Brandld7413152009-10-11 21:25:26 +00001591
1592 C.count = 314
1593
Antoine Pitrouf3520402011-12-03 22:19:55 +01001594Static methods are possible::
Georg Brandld7413152009-10-11 21:25:26 +00001595
1596 class C:
1597 @staticmethod
1598 def static(arg1, arg2, arg3):
1599 # No 'self' parameter!
1600 ...
1601
1602However, a far more straightforward way to get the effect of a static method is
1603via a simple module-level function::
1604
1605 def getcount():
1606 return C.count
1607
1608If your code is structured so as to define one class (or tightly related class
1609hierarchy) per module, this supplies the desired encapsulation.
1610
1611
1612How can I overload constructors (or methods) in Python?
1613-------------------------------------------------------
1614
1615This answer actually applies to all methods, but the question usually comes up
1616first in the context of constructors.
1617
1618In C++ you'd write
1619
1620.. code-block:: c
1621
1622 class C {
1623 C() { cout << "No arguments\n"; }
1624 C(int i) { cout << "Argument is " << i << "\n"; }
1625 }
1626
1627In Python you have to write a single constructor that catches all cases using
1628default arguments. For example::
1629
1630 class C:
1631 def __init__(self, i=None):
1632 if i is None:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001633 print("No arguments")
Georg Brandld7413152009-10-11 21:25:26 +00001634 else:
Georg Brandl62eaaf62009-12-19 17:51:41 +00001635 print("Argument is", i)
Georg Brandld7413152009-10-11 21:25:26 +00001636
1637This is not entirely equivalent, but close enough in practice.
1638
1639You could also try a variable-length argument list, e.g. ::
1640
1641 def __init__(self, *args):
1642 ...
1643
1644The same approach works for all method definitions.
1645
1646
1647I try to use __spam and I get an error about _SomeClassName__spam.
1648------------------------------------------------------------------
1649
1650Variable names with double leading underscores are "mangled" to provide a simple
1651but effective way to define class private variables. Any identifier of the form
1652``__spam`` (at least two leading underscores, at most one trailing underscore)
1653is textually replaced with ``_classname__spam``, where ``classname`` is the
1654current class name with any leading underscores stripped.
1655
1656This doesn't guarantee privacy: an outside user can still deliberately access
1657the "_classname__spam" attribute, and private values are visible in the object's
1658``__dict__``. Many Python programmers never bother to use private variable
1659names at all.
1660
1661
1662My class defines __del__ but it is not called when I delete the object.
1663-----------------------------------------------------------------------
1664
1665There are several possible reasons for this.
1666
1667The del statement does not necessarily call :meth:`__del__` -- it simply
1668decrements the object's reference count, and if this reaches zero
1669:meth:`__del__` is called.
1670
1671If your data structures contain circular links (e.g. a tree where each child has
1672a parent reference and each parent has a list of children) the reference counts
1673will never go back to zero. Once in a while Python runs an algorithm to detect
1674such cycles, but the garbage collector might run some time after the last
1675reference to your data structure vanishes, so your :meth:`__del__` method may be
1676called at an inconvenient and random time. This is inconvenient if you're trying
1677to reproduce a problem. Worse, the order in which object's :meth:`__del__`
1678methods are executed is arbitrary. You can run :func:`gc.collect` to force a
1679collection, but there *are* pathological cases where objects will never be
1680collected.
1681
1682Despite the cycle collector, it's still a good idea to define an explicit
1683``close()`` method on objects to be called whenever you're done with them. The
Gregory P. Smithe9d978f2017-08-28 13:43:26 -07001684``close()`` method can then remove attributes that refer to subobjects. Don't
Georg Brandld7413152009-10-11 21:25:26 +00001685call :meth:`__del__` directly -- :meth:`__del__` should call ``close()`` and
1686``close()`` should make sure that it can be called more than once for the same
1687object.
1688
1689Another way to avoid cyclical references is to use the :mod:`weakref` module,
1690which allows you to point to objects without incrementing their reference count.
1691Tree data structures, for instance, should use weak references for their parent
1692and sibling references (if they need them!).
1693
Georg Brandl62eaaf62009-12-19 17:51:41 +00001694.. XXX relevant for Python 3?
1695
1696 If the object has ever been a local variable in a function that caught an
1697 expression in an except clause, chances are that a reference to the object
1698 still exists in that function's stack frame as contained in the stack trace.
1699 Normally, calling :func:`sys.exc_clear` will take care of this by clearing
1700 the last recorded exception.
Georg Brandld7413152009-10-11 21:25:26 +00001701
1702Finally, if your :meth:`__del__` method raises an exception, a warning message
1703is printed to :data:`sys.stderr`.
1704
1705
1706How do I get a list of all instances of a given class?
1707------------------------------------------------------
1708
1709Python does not keep track of all instances of a class (or of a built-in type).
1710You can program the class's constructor to keep track of all instances by
1711keeping a list of weak references to each instance.
1712
1713
Georg Brandld8ede4f2013-10-12 18:14:25 +02001714Why does the result of ``id()`` appear to be not unique?
1715--------------------------------------------------------
1716
1717The :func:`id` builtin returns an integer that is guaranteed to be unique during
1718the lifetime of the object. Since in CPython, this is the object's memory
1719address, it happens frequently that after an object is deleted from memory, the
1720next freshly created object is allocated at the same position in memory. This
1721is illustrated by this example:
1722
Senthil Kumaran77493202016-06-04 20:07:34 -07001723>>> id(1000) # doctest: +SKIP
Georg Brandld8ede4f2013-10-12 18:14:25 +0200172413901272
Senthil Kumaran77493202016-06-04 20:07:34 -07001725>>> id(2000) # doctest: +SKIP
Georg Brandld8ede4f2013-10-12 18:14:25 +0200172613901272
1727
1728The two ids belong to different integer objects that are created before, and
1729deleted immediately after execution of the ``id()`` call. To be sure that
1730objects whose id you want to examine are still alive, create another reference
1731to the object:
1732
1733>>> a = 1000; b = 2000
Senthil Kumaran77493202016-06-04 20:07:34 -07001734>>> id(a) # doctest: +SKIP
Georg Brandld8ede4f2013-10-12 18:14:25 +0200173513901272
Senthil Kumaran77493202016-06-04 20:07:34 -07001736>>> id(b) # doctest: +SKIP
Georg Brandld8ede4f2013-10-12 18:14:25 +0200173713891296
1738
1739
Raymond Hettingerf8775e42021-04-03 19:54:49 -07001740When can I rely on identity tests with the *is* operator?
1741---------------------------------------------------------
1742
1743The ``is`` operator tests for object identity. The test ``a is b`` is
1744equivalent to ``id(a) == id(b)``.
1745
1746The most important property of an identity test is that an object is always
1747identical to itself, ``a is a`` always returns ``True``. Identity tests are
1748usually faster than equality tests. And unlike equality tests, identity tests
1749are guaranteed to return a boolean ``True`` or ``False``.
1750
1751However, identity tests can *only* be substituted for equality tests when
1752object identity is assured. Generally, there are three circumstances where
1753identity is guaranteed:
1754
17551) Assignments create new names but do not change object identity. After the
1756assignment ``new = old``, it is guaranteed that ``new is old``.
1757
17582) Putting an object in a container that stores object references does not
1759change object identity. After the list assignment ``s[0] = x``, it is
1760guaranteed that ``s[0] is x``.
1761
17623) If an object is a singleton, it means that only one instance of that object
1763can exist. After the assignments ``a = None`` and ``b = None``, it is
1764guaranteed that ``a is b`` because ``None`` is a singleton.
1765
1766In most other circumstances, identity tests are inadvisable and equality tests
1767are preferred. In particular, identity tests should not be used to check
1768constants such as :class:`int` and :class:`str` which aren't guaranteed to be
1769singletons::
1770
1771 >>> a = 1000
1772 >>> b = 500
1773 >>> c = b + 500
1774 >>> a is c
1775 False
1776
1777 >>> a = 'Python'
1778 >>> b = 'Py'
1779 >>> c = b + 'thon'
1780 >>> a is c
1781 False
1782
1783Likewise, new instances of mutable containers are never identical::
1784
1785 >>> a = []
1786 >>> b = []
1787 >>> a is b
1788 False
1789
1790In the standard library code, you will see several common patterns for
1791correctly using identity tests:
1792
17931) As recommended by :pep:`8`, an identity test is the preferred way to check
1794for ``None``. This reads like plain English in code and avoids confusion with
1795other objects that may have boolean values that evaluate to false.
1796
17972) Detecting optional arguments can be tricky when ``None`` is a valid input
1798value. In those situations, you can create an singleton sentinel object
1799guaranteed to be distinct from other objects. For example, here is how
1800to implement a method that behaves like :meth:`dict.pop`::
1801
1802 _sentinel = object()
1803
1804 def pop(self, key, default=_sentinel):
1805 if key in self:
1806 value = self[key]
1807 del self[key]
1808 return value
1809 if default is _sentinel:
1810 raise KeyError(key)
1811 return default
1812
18133) Container implementations sometimes need to augment equality tests with
1814identity tests. This prevents the code from being confused by objects such as
1815``float('NaN')`` that are not equal to themselves.
1816
1817For example, here is the implementation of
1818:meth:`collections.abc.Sequence.__contains__`::
1819
1820 def __contains__(self, value):
1821 for v in self:
1822 if v is value or v == value:
1823 return True
1824 return False
1825
1826
Georg Brandld7413152009-10-11 21:25:26 +00001827Modules
1828=======
1829
1830How do I create a .pyc file?
1831----------------------------
1832
R David Murrayd913d9d2013-12-13 12:29:29 -05001833When a module is imported for the first time (or when the source file has
1834changed since the current compiled file was created) a ``.pyc`` file containing
1835the compiled code should be created in a ``__pycache__`` subdirectory of the
1836directory containing the ``.py`` file. The ``.pyc`` file will have a
1837filename that starts with the same name as the ``.py`` file, and ends with
1838``.pyc``, with a middle component that depends on the particular ``python``
1839binary that created it. (See :pep:`3147` for details.)
Georg Brandld7413152009-10-11 21:25:26 +00001840
R David Murrayd913d9d2013-12-13 12:29:29 -05001841One reason that a ``.pyc`` file may not be created is a permissions problem
1842with the directory containing the source file, meaning that the ``__pycache__``
1843subdirectory cannot be created. This can happen, for example, if you develop as
1844one user but run as another, such as if you are testing with a web server.
1845
1846Unless the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable is set,
1847creation of a .pyc file is automatic if you're importing a module and Python
1848has the ability (permissions, free space, etc...) to create a ``__pycache__``
1849subdirectory and write the compiled module to that subdirectory.
Georg Brandld7413152009-10-11 21:25:26 +00001850
R David Murrayfdf95032013-06-19 16:58:26 -04001851Running Python on a top level script is not considered an import and no
1852``.pyc`` will be created. For example, if you have a top-level module
R David Murrayd913d9d2013-12-13 12:29:29 -05001853``foo.py`` that imports another module ``xyz.py``, when you run ``foo`` (by
1854typing ``python foo.py`` as a shell command), a ``.pyc`` will be created for
1855``xyz`` because ``xyz`` is imported, but no ``.pyc`` file will be created for
1856``foo`` since ``foo.py`` isn't being imported.
Georg Brandld7413152009-10-11 21:25:26 +00001857
R David Murrayd913d9d2013-12-13 12:29:29 -05001858If you need to create a ``.pyc`` file for ``foo`` -- that is, to create a
1859``.pyc`` file for a module that is not imported -- you can, using the
1860:mod:`py_compile` and :mod:`compileall` modules.
Georg Brandld7413152009-10-11 21:25:26 +00001861
1862The :mod:`py_compile` module can manually compile any module. One way is to use
1863the ``compile()`` function in that module interactively::
1864
1865 >>> import py_compile
R David Murrayfdf95032013-06-19 16:58:26 -04001866 >>> py_compile.compile('foo.py') # doctest: +SKIP
Georg Brandld7413152009-10-11 21:25:26 +00001867
R David Murrayd913d9d2013-12-13 12:29:29 -05001868This will write the ``.pyc`` to a ``__pycache__`` subdirectory in the same
1869location as ``foo.py`` (or you can override that with the optional parameter
1870``cfile``).
Georg Brandld7413152009-10-11 21:25:26 +00001871
1872You can also automatically compile all files in a directory or directories using
1873the :mod:`compileall` module. You can do it from the shell prompt by running
1874``compileall.py`` and providing the path of a directory containing Python files
1875to compile::
1876
1877 python -m compileall .
1878
1879
1880How do I find the current module name?
1881--------------------------------------
1882
1883A module can find out its own module name by looking at the predefined global
1884variable ``__name__``. If this has the value ``'__main__'``, the program is
1885running as a script. Many modules that are usually used by importing them also
1886provide a command-line interface or a self-test, and only execute this code
1887after checking ``__name__``::
1888
1889 def main():
Georg Brandl62eaaf62009-12-19 17:51:41 +00001890 print('Running test...')
Georg Brandld7413152009-10-11 21:25:26 +00001891 ...
1892
1893 if __name__ == '__main__':
1894 main()
1895
1896
1897How can I have modules that mutually import each other?
1898-------------------------------------------------------
1899
1900Suppose you have the following modules:
1901
Julien Palardfd79af72021-04-13 18:03:22 +02001902:file:`foo.py`::
Georg Brandld7413152009-10-11 21:25:26 +00001903
1904 from bar import bar_var
1905 foo_var = 1
1906
Julien Palardfd79af72021-04-13 18:03:22 +02001907:file:`bar.py`::
Georg Brandld7413152009-10-11 21:25:26 +00001908
1909 from foo import foo_var
1910 bar_var = 2
1911
1912The problem is that the interpreter will perform the following steps:
1913
Julien Palardfd79af72021-04-13 18:03:22 +02001914* main imports ``foo``
1915* Empty globals for ``foo`` are created
1916* ``foo`` is compiled and starts executing
1917* ``foo`` imports ``bar``
1918* Empty globals for ``bar`` are created
1919* ``bar`` is compiled and starts executing
1920* ``bar`` imports ``foo`` (which is a no-op since there already is a module named ``foo``)
1921* The import mechanism tries to read ``foo_var`` from ``foo`` globals, to set ``bar.foo_var = foo.foo_var``
Georg Brandld7413152009-10-11 21:25:26 +00001922
1923The last step fails, because Python isn't done with interpreting ``foo`` yet and
1924the global symbol dictionary for ``foo`` is still empty.
1925
1926The same thing happens when you use ``import foo``, and then try to access
1927``foo.foo_var`` in global code.
1928
1929There are (at least) three possible workarounds for this problem.
1930
1931Guido van Rossum recommends avoiding all uses of ``from <module> import ...``,
1932and placing all code inside functions. Initializations of global variables and
1933class variables should use constants or built-in functions only. This means
1934everything from an imported module is referenced as ``<module>.<name>``.
1935
1936Jim Roskind suggests performing steps in the following order in each module:
1937
1938* exports (globals, functions, and classes that don't need imported base
1939 classes)
1940* ``import`` statements
1941* active code (including globals that are initialized from imported values).
1942
1943van Rossum doesn't like this approach much because the imports appear in a
1944strange place, but it does work.
1945
1946Matthias Urlichs recommends restructuring your code so that the recursive import
1947is not necessary in the first place.
1948
1949These solutions are not mutually exclusive.
1950
1951
1952__import__('x.y.z') returns <module 'x'>; how do I get z?
1953---------------------------------------------------------
1954
Ezio Melottie4aad5a2014-08-04 19:34:29 +03001955Consider using the convenience function :func:`~importlib.import_module` from
1956:mod:`importlib` instead::
Georg Brandld7413152009-10-11 21:25:26 +00001957
Ezio Melottie4aad5a2014-08-04 19:34:29 +03001958 z = importlib.import_module('x.y.z')
Georg Brandld7413152009-10-11 21:25:26 +00001959
1960
1961When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
1962-------------------------------------------------------------------------------------------------
1963
1964For reasons of efficiency as well as consistency, Python only reads the module
1965file on the first time a module is imported. If it didn't, in a program
1966consisting of many modules where each one imports the same basic module, the
Brett Cannon4f422e32013-06-14 22:49:00 -04001967basic module would be parsed and re-parsed many times. To force re-reading of a
Georg Brandld7413152009-10-11 21:25:26 +00001968changed module, do this::
1969
Brett Cannon4f422e32013-06-14 22:49:00 -04001970 import importlib
Georg Brandld7413152009-10-11 21:25:26 +00001971 import modname
Brett Cannon4f422e32013-06-14 22:49:00 -04001972 importlib.reload(modname)
Georg Brandld7413152009-10-11 21:25:26 +00001973
1974Warning: this technique is not 100% fool-proof. In particular, modules
1975containing statements like ::
1976
1977 from modname import some_objects
1978
1979will continue to work with the old version of the imported objects. If the
1980module contains class definitions, existing class instances will *not* be
1981updated to use the new class definition. This can result in the following
Marco Buttu909a6f62017-03-18 17:59:33 +01001982paradoxical behaviour::
Georg Brandld7413152009-10-11 21:25:26 +00001983
Brett Cannon4f422e32013-06-14 22:49:00 -04001984 >>> import importlib
Georg Brandld7413152009-10-11 21:25:26 +00001985 >>> import cls
1986 >>> c = cls.C() # Create an instance of C
Brett Cannon4f422e32013-06-14 22:49:00 -04001987 >>> importlib.reload(cls)
Georg Brandl62eaaf62009-12-19 17:51:41 +00001988 <module 'cls' from 'cls.py'>
Georg Brandld7413152009-10-11 21:25:26 +00001989 >>> isinstance(c, cls.C) # isinstance is false?!?
1990 False
1991
Georg Brandl62eaaf62009-12-19 17:51:41 +00001992The nature of the problem is made clear if you print out the "identity" of the
Marco Buttu909a6f62017-03-18 17:59:33 +01001993class objects::
Georg Brandld7413152009-10-11 21:25:26 +00001994
Georg Brandl62eaaf62009-12-19 17:51:41 +00001995 >>> hex(id(c.__class__))
1996 '0x7352a0'
1997 >>> hex(id(cls.C))
1998 '0x4198d0'