Python x if not None else y

The conditional expression will return the default value if the variable stores None, otherwise the variable is returned.

Conditional expressions are very similar to an if/else statement.

In the examples, we check if the variable stores None, and if it does, we return the string default value, otherwise, the variable is returned.

You can use this approach to return a default value if None from a function, or to reassign a variable to a default value if it currently stores None.

An alternative approach is to use the boolean or operator.

The expression x or y returns the value to the left if it's truthy, otherwise the value to the right is returned.

However, this approach does not explicitly check for None.

The boolean or operator will return the value to the right if the value to the left is falsy.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and if/else1.
  • if/else2 (zero) of any numeric type
  • empty sequences and collections: if/else3 (empty string), if/else4 (empty tuple), if/else5 (empty list), if/else6 (empty dictionary), if/else7 (empty set), if/else8 (empty range).

So if the value to the left is any of the aforementioned falsy values, the value to the right is returned.

Several debuggers for Python are described below, and the built-in function allows you to drop into any of them.

The pdb module is a simple but adequate console-mode debugger for Python. It is part of the standard Python library, and is . You can also write your own debugger by using the code for pdb as an example.

The IDLE interactive development environment, which is part of the standard Python distribution (normally available as Tools/scripts/idle3), includes a graphical debugger.

PythonWin is a Python IDE that includes a GUI debugger based on pdb. The PythonWin debugger colors breakpoints and has quite a few cool features such as debugging non-PythonWin programs. PythonWin is available as part of pywin32 project and as a part of the ActivePython distribution.

Eric is an IDE built on PyQt and the Scintilla editing component.

trepan3k is a gdb-like debugger.

Visual Studio Code is an IDE with debugging tools that integrates with version-control software.

There are a number of commercial Python IDEs that include graphical debuggers. They include:

  • Wing IDE

  • Komodo IDE

  • PyCharm

Yes.

Pylint and Pyflakes do basic checking that will help you catch bugs sooner.

Static type checkers such as Mypy, Pyre, and Pytype can check type hints in Python source code.

You don’t need the ability to compile Python to C code if all you want is a stand-alone program that users can download and run without having to install the Python distribution first. There are a number of tools that determine the set of modules required by a program and bind these modules together with a Python binary to produce a single executable.

One is to use the freeze tool, which is included in the Python source tree as Tools/freeze. It converts Python byte code to C arrays; with a C compiler you can embed all your modules into a new program, which is then linked with the standard Python modules.

It works by scanning your source recursively for import statements (in both forms) and looking for the modules in the standard Python path as well as in the source directory (for built-in modules). It then turns the bytecode for modules written in Python into C code (array initializers that can be turned into code objects using the marshal module) and creates a custom-made config file that only contains those built-in modules which are actually used in the program. It then compiles the generated C code and links it with the rest of the Python interpreter to form a self-contained binary which acts exactly like your script.

The following packages can help with the creation of console and GUI executables:

  • Nuitka (Cross-platform)

  • PyInstaller (Cross-platform)

  • PyOxidizer (Cross-platform)

  • cx_Freeze (Cross-platform)

  • py2app (macOS only)

  • py2exe (Windows only)

Yes. The coding style required for standard library modules is documented as PEP 8.

It can be a surprise to get the in previously working code when it is modified by adding an assignment statement somewhere in the body of a function.

This code:

>>> x = 10
>>> def bar():
...     print(x)
...
>>> bar()
10

works, but this code:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1

results in an

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
22:

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment

This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope. Since the last statement in foo assigns a new value to

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24, the compiler recognizes it as a local variable. Consequently when the earlier
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
25 attempts to print the uninitialized local variable and an error results.

In the example above you can access the outer scope variable by declaring it global:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10

This explicit declaration is required in order to remind you that (unlike the superficially analogous situation with class and instance variables) you are actually modifying the value of the variable in the outer scope:

>>> print(x)
11

You can do a similar thing in a nested scope using the keyword:

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring for assigned variables provides a bar against unintended side-effects. On the other hand, if

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
27 was required for all global references, you’d be using
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
27 all the time. You’d have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of the
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
27 declaration for identifying side-effects.

Assume you use a for loop to define a few different lambdas (or even plain functions), e.g.:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)

This gives you a list that contains 5 lambdas that calculate

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
31. You might expect that, when called, they would return, respectively,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
32,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
33,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
34,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
35, and
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
36. However, when you actually try you will see that they all return
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
36:

>>> squares[2]()
16
>>> squares[4]()
16

This happens because

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 is not local to the lambdas, but is defined in the outer scope, and it is accessed when the lambda is called — not when it is defined. At the end of the loop, the value of
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 is
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
34, so all the functions now return
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
41, i.e.
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
36. You can also verify this by changing the value of
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 and see how the results of the lambdas change:

>>> x = 8
>>> squares[2]()
64

In order to avoid this, you need to save the values in variables local to the lambdas, so that they don’t rely on the value of the global

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)

Here,

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
45 creates a new variable
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
46 local to the lambda and computed when the lambda is defined so that it has the same value that
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 had at that point in the loop. This means that the value of
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
46 will be
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
32 in the first lambda,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
33 in the second,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
51 in the third, and so on. Therefore each lambda will now return the correct result:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
0

Note that this behaviour is not peculiar to lambdas, but applies to regular functions too.

The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere. For example:

config.py:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
1

mod.py:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
2

main.py:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
3

Note that using a module is also the basis for implementing the singleton design pattern, for the same reason.

In general, don’t use

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
52. Doing so clutters the importer’s namespace, and makes it much harder for linters to detect undefined names.

Import modules at the top of a file. Doing so makes it clear what other modules your code requires and avoids questions of whether the module name is in scope. Using one import per line makes it easy to add and delete module imports, but using multiple imports per line uses less screen space.

It’s good practice if you import modules in the following order:

  1. standard library modules – e.g. , , ,

  2. third-party library modules (anything installed in Python’s site-packages directory) – e.g.

    >>> x = 10
    >>> def foo():
    ...     print(x)
    ...     x += 1
    
    57,
    >>> x = 10
    >>> def foo():
    ...     print(x)
    ...     x += 1
    
    58,
    >>> x = 10
    >>> def foo():
    ...     print(x)
    ...     x += 1
    
    59

  3. locally developed modules

It is sometimes necessary to move imports to a function or class to avoid problems with circular imports. Gordon McMillan says:

Circular imports are fine where both modules use the “import <module>” form of import. They fail when the 2nd module wants to grab a name out of the first (“from module import name”) and the import is at the top level. That’s because names in the 1st are not yet available, because the first module is busy importing the 2nd.

In this case, if the second module is only used in one function, then the import can easily be moved into that function. By the time the import is called, the first module will have finished initializing, and the second module can do its import.

It may also be necessary to move imports out of the top level of code if some of the modules are platform-specific. In that case, it may not even be possible to import all of the modules at the top of the file. In this case, importing the correct modules in the corresponding platform-specific code is a good option.

Only move imports into a local scope, such as inside a function definition, if it’s necessary to solve a problem such as avoiding a circular import or are trying to reduce the initialization time of a module. This technique is especially helpful if many of the imports are unnecessary depending on how the program executes. You may also want to move imports into a function if the modules are only ever used in that function. Note that loading a module the first time may be expensive because of the one time initialization of the module, but loading a module multiple times is virtually free, costing only a couple of dictionary lookups. Even if the module name has gone out of scope, the module is probably available in .

This type of bug commonly bites neophyte programmers. Consider this function:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
4

The first time you call this function,

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
61 contains a single item. The second time,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
61 contains two items because when
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
63 begins executing,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
61 starts out with an item already in it.

It is often expected that a function call creates new objects for default values. This is not what happens. Default values are created exactly once, when the function is defined. If that object is changed, like the dictionary in this example, subsequent calls to the function will refer to this changed object.

By definition, immutable objects such as numbers, strings, tuples, and

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
65, are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion.

Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
65 as the default value and inside the function, check if the parameter is
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
65 and create a new list/dictionary/whatever if it is. For example, don’t write:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
5

but:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
6

This feature can be useful. When you have a function that’s time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function, and return the cached value if the same value is requested again. This is called “memoizing”, and can be implemented like this:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
7

You could use a global variable containing a dictionary instead of the default value; it’s a matter of taste.

Collect the arguments using the

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
68 and
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
69 specifiers in the function’s parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary. You can then pass these arguments when calling another function by using
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
68 and
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
69:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
8

are defined by the names that appear in a function definition, whereas are the values actually passed to a function when calling it. Parameters define what a function can accept. For example, given the function definition:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
9

foo, bar and kwargs are parameters of

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
72. However, when calling
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
72, for example:

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
0

the values

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
74,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
75, and
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
76 are arguments.

If you wrote code like:

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
1

you might be wondering why appending an element to

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
77 changed
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 too.

There are two factors that produce this result:

  1. Variables are simply names that refer to objects. Doing

    >>> x = 10
    >>> def foo():
    ...     print(x)
    ...     x += 1
    
    79 doesn’t create a copy of the list – it creates a new variable
    >>> x = 10
    >>> def foo():
    ...     print(x)
    ...     x += 1
    
    77 that refers to the same object
    >>> x = 10
    >>> def foo():
    ...     print(x)
    ...     x += 1
    
    24 refers to. This means that there is only one object (the list), and both
    >>> x = 10
    >>> def foo():
    ...     print(x)
    ...     x += 1
    
    24 and
    >>> x = 10
    >>> def foo():
    ...     print(x)
    ...     x += 1
    
    77 refer to it.

  2. Lists are , which means that you can change their content.

After the call to

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
84, the content of the mutable object has changed from
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
85 to
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
86. Since both the variables refer to the same object, using either name accesses the modified value
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
86.

If we instead assign an immutable object to

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24:

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
2

we can see that in this case

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 and
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
77 are not equal anymore. This is because integers are , and when we do
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
91 we are not mutating the int
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
92 by incrementing its value; instead, we are creating a new object (the int
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
93) and assigning it to
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 (that is, changing which object
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 refers to). After this assignment we have two objects (the ints
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
93 and
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
92) and two variables that refer to them (
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 now refers to
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
93 but
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
77 still refers to
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
92).

Some operations (for example

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
02 and
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
03) mutate the object, whereas superficially similar operations (for example
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
04 and ) create a new object. In general in Python (and in all cases in the standard library) a method that mutates an object will return
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
65 to help avoid getting the two types of operations confused. So if you mistakenly write
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
03 thinking it will give you a sorted copy of
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
77, you’ll instead end up with
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
65, which will likely cause your program to generate an easily diagnosed error.

However, there is one class of operations where the same operation sometimes has different behaviors with different types: the augmented assignment operators. For example,

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
10 mutates lists but not tuples or ints (
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
11 is equivalent to
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
12 and mutates
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
13, whereas
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
14 and
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
15 create new objects).

In other words:

  • If we have a mutable object (, , , etc.), we can use some specific operations to mutate it and all the variables that refer to it will see the change.

  • If we have an immutable object (, , , etc.), all the variables that refer to it will always see the same value, but operations that transform that value into a new value always return a new object.

If you want to know if two variables refer to the same object or not, you can use the operator, or the built-in function .

Remember that arguments are passed by assignment in Python. Since assignment just creates references to objects, there’s no alias between an argument name in the caller and callee, and so no call-by-reference per se. You can achieve the desired effect in a number of ways.

  1. By returning a tuple of the results:

    >>> foo()
    Traceback (most recent call last):
      ...
    UnboundLocalError: local variable 'x' referenced before assignment
    
    3

    This is almost always the clearest solution.

  2. By using global variables. This isn’t thread-safe, and is not recommended.

  3. By passing a mutable (changeable in-place) object:

    >>> foo()
    Traceback (most recent call last):
      ...
    UnboundLocalError: local variable 'x' referenced before assignment
    
    4

  4. By passing in a dictionary that gets mutated:

    >>> foo()
    Traceback (most recent call last):
      ...
    UnboundLocalError: local variable 'x' referenced before assignment
    
    5

  5. Or bundle up values in a class instance:

    >>> foo()
    Traceback (most recent call last):
      ...
    UnboundLocalError: local variable 'x' referenced before assignment
    
    6

    There’s almost never a good reason to get this complicated.

Your best choice is to return a tuple containing the multiple results.

You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
24 which returns a function
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
25 that computes the value
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
26. Using nested scopes:

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
7

Or using a callable object:

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
8

In both cases,

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
9

gives a callable object where

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
27.

The callable object approach has the disadvantage that it is a bit slower and results in slightly longer code. However, note that a collection of callables can share their signature via inheritance:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
0

Object can encapsulate state for several methods:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
1

Here

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
28,
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
29 and
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
30 act like functions which share the same counting variable.

In general, try or for the general case. Not all objects can be copied, but most can.

Some objects can be copied more easily. Dictionaries have a method:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
2

Sequences can be copied by slicing:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
3

For an instance

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 of a user-defined class, returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class.

Generally speaking, it can’t, because objects don’t really have names. Essentially, assignment always binds a name to a value; the same is true of

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
36 and
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
37 statements, but in that case the value is a callable. Consider the following code:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
4

Arguably the class has a name: even though it is bound to two names and invoked through the name

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
38 the created instance is still reported as an instance of class
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
39. However, it is impossible to say whether the instance’s name is
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
40 or
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
41, since both names are bound to the same value.

Generally speaking it should not be necessary for your code to “know the names” of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial.

In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to this question:

The same way as you get the name of that cat you found on your porch: the cat (object) itself cannot tell you its name, and it doesn’t really care – so the only way to find out what it’s called is to ask all your neighbours (namespaces) if it’s their cat (object)…

….and don’t be surprised if you’ll find that it’s known by many names, or no name at all!

Comma is not an operator in Python. Consider this session:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
5

Since the comma is not an operator, but a separator between expressions the above is evaluated as if you had entered:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
6

not:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
7

The same is true of the various assignment operators (

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
42,
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
10 etc). They are not truly operators but syntactic delimiters in assignment statements.

Yes, there is. The syntax is as follows:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
8

Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators:

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
9

However, this idiom is unsafe, as it can give wrong results when on_true has a false boolean value. Therefore, it is always better to use the

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
44 form.

Yes. Usually this is done by nesting within

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
45. See the following three examples, slightly adapted from Ulf Bartelt:

>>> print(x)
11
0

Don’t try this at home, kids!

A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Positional-only parameters are the ones without an externally usable name. Upon calling a function that accepts positional-only parameters, arguments are mapped to parameters based solely on their position. For example, is a function that accepts positional-only parameters. Its documentation looks like this:

>>> print(x)
11
1

The slash at the end of the parameter list means that both parameters are positional-only. Thus, calling with keyword arguments would lead to an error:

>>> print(x)
11
2

To specify an octal digit, precede the octal value with a zero, and then a lower or uppercase “o”. For example, to set the variable “a” to the octal value “10” (8 in decimal), type:

>>> print(x)
11
3

Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero, and then a lower or uppercase “x”. Hexadecimal digits can be specified in lower or uppercase. For example, in the Python interpreter:

>>> print(x)
11
4

It’s primarily driven by the desire that

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
49 have the same sign as
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
50. If you want that, and also want:

>>> print(x)
11
5

then integer division has to return the floor. C also requires that identity to hold, and then compilers that truncate

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
51 need to make
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
49 have the same sign as
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
53.

There are few real use cases for

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
49 when
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
50 is negative. When
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
50 is positive, there are many, and in virtually all of them it’s more useful for
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
49 to be
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
58. If the clock says 10 now, what did it say 200 hours ago?
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
59 is useful;
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
60 is a bug waiting to bite.

Trying to lookup an

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
20 literal attribute in the normal manner gives a because the period is seen as a decimal point:

>>> print(x)
11
6

The solution is to separate the literal from the period with either a space or parentheses.

>>> print(x)
11
7

For integers, use the built-in type constructor, e.g.

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
64. Similarly, converts to floating-point, e.g.
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
66.

By default, these interpret the number as decimal, so that

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
67 holds true, and
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
68 raises .
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
70 takes the base to convert from as a second optional argument, so
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
71. If the base is specified as 0, the number is interpreted using Python’s rules: a leading ‘0o’ indicates octal, and ‘0x’ indicates a hex number.

Do not use the built-in function if all you need is to convert strings to numbers. will be significantly slower and it presents a security risk: someone could pass you a Python expression that might have unwanted side effects. For example, someone could pass

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
74 which would erase your home directory.

also has the effect of interpreting numbers as Python expressions, so that e.g.

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
76 gives a syntax error because Python does not allow leading ‘0’ in a decimal number (except ‘0’).

To convert, e.g., the number

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
77 to the string
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
78, use the built-in type constructor . If you want a hexadecimal or octal representation, use the built-in functions or . For fancy formatting, see the and sections, e.g.
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
82 yields
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
83 and
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
84 yields
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
85.

You can’t, because strings are immutable. In most situations, you should simply construct a new string from the various parts you want to assemble it from. However, if you need an object with the ability to modify in-place unicode data, try using an object or the module:

>>> print(x)
11
8

There are various techniques.

  • The best is to use a dictionary that maps strings to functions. The primary advantage of this technique is that the strings do not need to match the names of the functions. This is also the primary technique used to emulate a case construct:

    >>> print(x)
    11
    
    9

  • Use the built-in function :

    >>> def foo():
    ...    x = 10
    ...    def bar():
    ...        nonlocal x
    ...        print(x)
    ...        x += 1
    ...    bar()
    ...    print(x)
    ...
    >>> foo()
    10
    11
    
    0

    Note that works on any object, including classes, class instances, modules, and so on.

    This is used in several places in the standard library, like this:

    >>> def foo():
    ...    x = 10
    ...    def bar():
    ...        nonlocal x
    ...        print(x)
    ...        x += 1
    ...    bar()
    ...    print(x)
    ...
    >>> foo()
    10
    11
    
    1

  • Use to resolve the function name:

    >>> def foo():
    ...    x = 10
    ...    def bar():
    ...        nonlocal x
    ...        print(x)
    ...        x += 1
    ...    bar()
    ...    print(x)
    ...
    >>> foo()
    10
    11
    
    2

You can use

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
91 to remove all occurrences of any line terminator from the end of the string
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
92 without removing other trailing whitespace. If the string
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
92 represents more than one line, with several empty lines at the end, the line terminators for all the blank lines will be removed:

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
3

Since this is typically only desired when reading text one line at a time, using

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
94 this way works well.

Not as such.

For simple input parsing, the easiest approach is usually to split the line into whitespace-delimited words using the method of string objects and then convert decimal strings to numeric values using or .

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
95 supports an optional “sep” parameter which is useful if the line uses something other than whitespace as a separator.

For more complicated input parsing, regular expressions are more powerful than C’s

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
99 and better suited for the task.

See the .

A raw string ending with an odd number of backslashes will escape the string’s quote:

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
4

There are several workarounds for this. One is to use regular strings and double the backslashes:

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
5

Another is to concatenate a regular string containing an escaped backslash to the raw string:

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
6

It is also possible to use to append a backslash on Windows:

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
7

Note that while a backslash will “escape” a quote for the purposes of determining where the raw string ends, no escaping occurs when interpreting the value of the raw string. That is, the backslash remains present in the value of the raw string:

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
8

Also see the specification in the .

That’s a tough one, in general. First, here are a list of things to remember before diving further:

  • Performance characteristics vary across Python implementations. This FAQ focuses on .

  • Behaviour can vary across operating systems, especially when talking about I/O or multi-threading.

  • You should always find the hot spots in your program before attempting to optimize any code (see the module).

  • Writing benchmark scripts will allow you to iterate quickly when searching for improvements (see the module).

  • It is highly recommended to have good code coverage (through unit testing or any other technique) before potentially introducing regressions hidden in sophisticated optimizations.

That being said, there are many tricks to speed up Python code. Here are some general principles which go a long way towards reaching acceptable performance levels:

  • Making your algorithms faster (or changing to faster ones) can yield much larger benefits than trying to sprinkle micro-optimization tricks all over your code.

  • Use the right data structures. Study documentation for the and the module.

  • When the standard library provides a primitive for doing something, it is likely (although not guaranteed) to be faster than any alternative you may come up with. This is doubly true for primitives written in C, such as builtins and some extension types. For example, be sure to use either the built-in method or the related function to do sorting (and see the for examples of moderately advanced usage).

  • Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower. You should avoid excessive abstraction, especially under the form of tiny functions or methods (which are also often detrimental to readability).

If you have reached the limit of what pure Python can allow, there are tools to take you further away. For example, Cython can compile a slightly modified version of Python code into a C extension, and can be used on many different platforms. Cython can take advantage of compilation (and optional type annotations) to make your code significantly faster than when interpreted. If you are confident in your C programming skills, you can also yourself.

See also

The wiki page devoted to performance tips.

and objects are immutable, therefore concatenating many strings together is inefficient as each concatenation creates a new object. In the general case, the total runtime cost is quadratic in the total string length.

To accumulate many objects, the recommended idiom is to place them into a list and call at the end:

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
9

(another reasonably efficient idiom is to use )

To accumulate many objects, the recommended idiom is to extend a object using in-place concatenation (the

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
10 operator):

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
0

The type constructor

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
14 converts any sequence (actually, any iterable) into a tuple with the same items in the same order.

For example,

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
15 yields
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
16 and
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
17 yields
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
18. If the argument is a tuple, it does not make a copy but returns the same object, so it is cheap to call when you aren’t sure that an object is already a tuple.

The type constructor

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
20 converts any sequence or iterable into a list with the same items in the same order. For example,
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
21 yields
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
22 and
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
23 yields
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
24. If the argument is a list, it makes a copy just like
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
25 would.

Python sequences are indexed with positive numbers and negative numbers. For positive numbers 0 is the first index 1 is the second index and so forth. For negative indices -1 is the last index and -2 is the penultimate (next to last) index and so forth. Think of

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
26 as the same as
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
27.

Using negative indices can be very convenient. For example

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
28 is all of the string except for its last character, which is useful for removing the trailing newline from a string.

Use the built-in function:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
1

This won’t touch your original sequence, but build a new copy with reversed order to iterate over.

See the Python Cookbook for a long discussion of many ways to do this:

https://code.activestate.com/recipes/52560/

If you don’t mind reordering the list, sort it and then scan from the end of the list, deleting duplicates as you go:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
2

If all elements of the list may be used as set keys (i.e. they are all ) this is often faster

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
3

This converts the list into a set, thereby removing duplicates, and then back into a list.

As with removing duplicates, explicitly iterating in reverse with a delete condition is one possibility. However, it is easier and faster to use slice replacement with an implicit or explicit forward iteration. Here are three variations.:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
4

The list comprehension may be fastest.

Use a list:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
5

Lists are equivalent to C or Pascal arrays in their time complexity; the primary difference is that a Python list can contain objects of many different types.

The

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
87 module also provides methods for creating arrays of fixed types with compact representations, but they are slower to index than lists. Also note that NumPy and other third party packages define array-like structures with various characteristics as well.

To get Lisp-style linked lists, you can emulate cons cells using tuples:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
6

If mutability is desired, you could use lists instead of tuples. Here the analogue of a Lisp car is

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
31 and the analogue of cdr is
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
32. Only do this if you’re sure you really need to, because it’s usually a lot slower than using Python lists.

You probably tried to make a multidimensional array like this:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
7

This looks correct if you print it:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
8

But when you assign a value, it shows up in multiple places:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)
9

The reason is that replicating a list with

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
68 doesn’t create copies, it only creates references to the existing objects. The
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
34 creates a list containing 3 references to the same list of length two. Changes to one row will show in all rows, which is almost certainly not what you want.

The suggested approach is to create a list of the desired length first and then fill in each element with a newly created list:

>>> squares[2]()
16
>>> squares[4]()
16
0

This generates a list containing 3 different lists of length two. You can also use a list comprehension:

>>> squares[2]()
16
>>> squares[4]()
16
1

Or, you can use an extension that provides a matrix datatype; NumPy is the best known.

To call a method or function and accumulate the return values is a list, a is an elegant solution:

>>> squares[2]()
16
>>> squares[4]()
16
2

To just run the method or function without saving the return values, a plain loop will suffice:

>>> squares[2]()
16
>>> squares[4]()
16
3

This is because of a combination of the fact that augmented assignment operators are assignment operators, and the difference between mutable and immutable objects in Python.

This discussion applies in general when augmented assignment operators are applied to elements of a tuple that point to mutable objects, but we’ll use a

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
16 and
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
10 as our exemplar.

If you wrote:

>>> squares[2]()
16
>>> squares[4]()
16
4

The reason for the exception should be immediately clear:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
33 is added to the object
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
39 points to (
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
33), producing the result object,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
51, but when we attempt to assign the result of the computation,
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
51, to element
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
32 of the tuple, we get an error because we can’t change what an element of a tuple points to.

Under the covers, what this augmented assignment statement is doing is approximately this:

>>> squares[2]()
16
>>> squares[4]()
16
5

It is the assignment part of the operation that produces the error, since a tuple is immutable.

When you write something like:

>>> squares[2]()
16
>>> squares[4]()
16
6

The exception is a bit more surprising, and even more surprising is the fact that even though there was an error, the append worked:

>>> squares[2]()
16
>>> squares[4]()
16
7

To see why this happens, you need to know that (a) if an object implements an magic method, it gets called when the

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
10 augmented assignment is executed, and its return value is what gets used in the assignment statement; and (b) for lists,
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
44 is equivalent to calling
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
47 on the list and returning the list. That’s why we say that for lists,
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
10 is a “shorthand” for
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
49:

>>> squares[2]()
16
>>> squares[4]()
16
8

This is equivalent to:

>>> squares[2]()
16
>>> squares[4]()
16
9

The object pointed to by a_list has been mutated, and the pointer to the mutated object is assigned back to

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
13. The end result of the assignment is a no-op, since it is a pointer to the same object that
>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
13 was previously pointing to, but the assignment still happens.

Thus, in our tuple example what is happening is equivalent to:

>>> x = 8
>>> squares[2]()
64
0

The

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
44 succeeds, and thus the list is extended, but even though
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
53 points to the same object that
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
39 already points to, that final assignment still results in an error, because tuples are immutable.

The technique, attributed to Randal Schwartz of the Perl community, sorts the elements of a list by a metric which maps each element to its “sort value”. In Python, use the

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
55 argument for the method:

>>> x = 8
>>> squares[2]()
64
1

Merge them into an iterator of tuples, sort the resulting list, and then pick out the element you want.

>>> x = 8
>>> squares[2]()
64
2

A class is the particular object type created by executing a class statement. Class objects are used as templates to create instance objects, which embody both the data (attributes) and code (methods) specific to a datatype.

A class can be based on one or more other classes, called its base class(es). It then inherits the attributes and methods of its base classes. This allows an object model to be successively refined by inheritance. You might have a generic

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
57 class that provides basic accessor methods for a mailbox, and subclasses such as
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
58,
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
59,
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
60 that handle various specific mailbox formats.

A method is a function on some object

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 that you normally call as
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
62. Methods are defined as functions inside the class definition:

>>> x = 8
>>> squares[2]()
64
3

Self is merely a conventional name for the first argument of a method. A method defined as

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
63 should be called as
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
64 for some instance
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 of the class in which the definition occurs; the called method will think it is called as
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
66.

See also .

Use the built-in function . You can check if an object is an instance of any of a number of classes by providing a tuple instead of a single class, e.g.

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
68, and can also check whether an object is one of Python’s built-in types, e.g.
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
69 or
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
70.

Note that also checks for virtual inheritance from an . So, the test will return

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
72 for a registered class even if hasn’t directly or indirectly inherited from it. To test for “true inheritance”, scan the of the class:

>>> x = 8
>>> squares[2]()
64
4

>>> x = 8
>>> squares[2]()
64
5

Note that most programs do not use on user-defined classes very often. If you are developing the classes yourself, a more proper object-oriented style is to define methods on the classes that encapsulate a particular behaviour, instead of checking the object’s class and doing a different thing based on what class it is. For example, if you have a function that does something:

>>> x = 8
>>> squares[2]()
64
6

A better approach is to define a

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
74 method on all the classes and just call it:

>>> x = 8
>>> squares[2]()
64
7

Delegation is an object oriented technique (also called a design pattern). Let’s say you have an object

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24 and want to change the behaviour of just one of its methods. You can create a new class that provides a new implementation of the method you’re interested in changing and delegates all other methods to the corresponding method of
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
24.

Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts all written data to uppercase:

>>> x = 8
>>> squares[2]()
64
8

Here the

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
77 class redefines the
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
78 method to convert the argument string to uppercase before calling the underlying
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
79 method. All other methods are delegated to the underlying
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
80 object. The delegation is accomplished via the method; consult for more information about controlling attribute access.

Note that for more general cases delegation can get trickier. When attributes must be set as well as retrieved, the class must define a method too, and it must do so carefully. The basic implementation of

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
82 is roughly equivalent to the following:

>>> x = 8
>>> squares[2]()
64
9

Most

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
82 implementations must modify to store local state for self without causing an infinite recursion.

Use the built-in function:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
0

In the example, will automatically determine the instance from which it was called (the

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
88 value), look up the (MRO) with
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
89, and return the next in line after
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
90 in the MRO:
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
91.

You could assign the base class to an alias and derive from the alias. Then all you have to change is the value assigned to the alias. Incidentally, this trick is also handy if you want to decide dynamically (e.g. depending on availability of resources) which base class to use. Example:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
1

Both static data and static methods (in the sense of C++ or Java) are supported in Python.

For static data, simply define a class attribute. To assign a new value to the attribute, you have to explicitly use the class name in the assignment:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
2

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
92 also refers to
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
93 for any
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
94 such that
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
95 holds, unless overridden by
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
94 itself or by some class on the base-class search path from
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
97 back to
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
98.

Caution: within a method of C, an assignment like

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
99 creates a new and unrelated instance named “count” in
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
88’s own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
3

Static methods are possible:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
4

However, a far more straightforward way to get the effect of a static method is via a simple module-level function:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
5

If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation.

This answer actually applies to all methods, but the question usually comes up first in the context of constructors.

In C++ you’d write

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
6

In Python you have to write a single constructor that catches all cases using default arguments. For example:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
7

This is not entirely equivalent, but close enough in practice.

You could also try a variable-length argument list, e.g.

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
8

The same approach works for all method definitions.

Variable names with double leading underscores are “mangled” to provide a simple but effective way to define class private variables. Any identifier of the form

>>> print(x)
11
01 (at least two leading underscores, at most one trailing underscore) is textually replaced with
>>> print(x)
11
02, where
>>> print(x)
11
03 is the current class name with any leading underscores stripped.

This doesn’t guarantee privacy: an outside user can still deliberately access the “_classname__spam” attribute, and private values are visible in the object’s

>>> print(x)
11
04. Many Python programmers never bother to use private variable names at all.

There are several possible reasons for this.

The statement does not necessarily call – it simply decrements the object’s reference count, and if this reaches zero

>>> print(x)
11
06 is called.

If your data structures contain circular links (e.g. a tree where each child has a parent reference and each parent has a list of children) the reference counts will never go back to zero. Once in a while Python runs an algorithm to detect such cycles, but the garbage collector might run some time after the last reference to your data structure vanishes, so your

>>> print(x)
11
06 method may be called at an inconvenient and random time. This is inconvenient if you’re trying to reproduce a problem. Worse, the order in which object’s
>>> print(x)
11
06 methods are executed is arbitrary. You can run to force a collection, but there are pathological cases where objects will never be collected.

Despite the cycle collector, it’s still a good idea to define an explicit

>>> print(x)
11
11 method on objects to be called whenever you’re done with them. The
>>> print(x)
11
11 method can then remove attributes that refer to subobjects. Don’t call
>>> print(x)
11
06 directly –
>>> print(x)
11
06 should call
>>> print(x)
11
11 and
>>> print(x)
11
11 should make sure that it can be called more than once for the same object.

Another way to avoid cyclical references is to use the module, which allows you to point to objects without incrementing their reference count. Tree data structures, for instance, should use weak references for their parent and sibling references (if they need them!).

Finally, if your

>>> print(x)
11
06 method raises an exception, a warning message is printed to .

Python does not keep track of all instances of a class (or of a built-in type). You can program the class’s constructor to keep track of all instances by keeping a list of weak references to each instance.

The builtin returns an integer that is guaranteed to be unique during the lifetime of the object. Since in CPython, this is the object’s memory address, it happens frequently that after an object is deleted from memory, the next freshly created object is allocated at the same position in memory. This is illustrated by this example:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)
9

The two ids belong to different integer objects that are created before, and deleted immediately after execution of the

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
19 call. To be sure that objects whose id you want to examine are still alive, create another reference to the object:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
00

The

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment
22 operator tests for object identity. The test
>>> print(x)
11
24 is equivalent to
>>> print(x)
11
25.

The most important property of an identity test is that an object is always identical to itself,

>>> print(x)
11
26 always returns
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
72. Identity tests are usually faster than equality tests. And unlike equality tests, identity tests are guaranteed to return a boolean
>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
...
>>> foobar()
10
72 or
>>> print(x)
11
29.

However, identity tests can only be substituted for equality tests when object identity is assured. Generally, there are three circumstances where identity is guaranteed:

1) Assignments create new names but do not change object identity. After the assignment

>>> print(x)
11
30, it is guaranteed that
>>> print(x)
11
31.

2) Putting an object in a container that stores object references does not change object identity. After the list assignment

>>> print(x)
11
32, it is guaranteed that
>>> print(x)
11
33.

3) If an object is a singleton, it means that only one instance of that object can exist. After the assignments

>>> print(x)
11
34 and
>>> print(x)
11
35, it is guaranteed that
>>> print(x)
11
24 because
>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
65 is a singleton.

In most other circumstances, identity tests are inadvisable and equality tests are preferred. In particular, identity tests should not be used to check constants such as and which aren’t guaranteed to be singletons:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
01

Likewise, new instances of mutable containers are never identical:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
02

In the standard library code, you will see several common patterns for correctly using identity tests:

1) As recommended by PEP 8, an identity test is the preferred way to check for

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
65. This reads like plain English in code and avoids confusion with other objects that may have boolean values that evaluate to false.

2) Detecting optional arguments can be tricky when

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
65 is a valid input value. In those situations, you can create a singleton sentinel object guaranteed to be distinct from other objects. For example, here is how to implement a method that behaves like :

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
03

3) Container implementations sometimes need to augment equality tests with identity tests. This prevents the code from being confused by objects such as

>>> print(x)
11
43 that are not equal to themselves.

For example, here is the implementation of

>>> print(x)
11
44:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
04

When subclassing an immutable type, override the method instead of the method. The latter only runs after an instance is created, which is too late to alter data in an immutable instance.

All of these immutable classes have a different signature than their parent class:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
05

The classes can be used like this:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
06

The two principal tools for caching methods are and . The former stores results at the instance level and the latter at the class level.

The cached_property approach only works with methods that do not take any arguments. It does not create a reference to the instance. The cached method result will be kept only as long as the instance is alive.

The advantage is that when an instance is no longer used, the cached method result will be released right away. The disadvantage is that if instances accumulate, so too will the accumulated method results. They can grow without bound.

The lru_cache approach works with methods that have hashable arguments. It creates a reference to the instance unless special efforts are made to pass in weak references.

The advantage of the least recently used algorithm is that the cache is bounded by the specified maxsize. The disadvantage is that instances are kept alive until they age out of the cache or until the cache is cleared.

This example shows the various techniques:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
07

The above example assumes that the station_id never changes. If the relevant instance attributes are mutable, the cached_property approach can’t be made to work because it cannot detect changes to the attributes.

To make the lru_cache approach work when the station_id is mutable, the class needs to define the and methods so that the cache can detect relevant attribute updates:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
08

When a module is imported for the first time (or when the source file has changed since the current compiled file was created) a

>>> print(x)
11
51 file containing the compiled code should be created in a
>>> print(x)
11
52 subdirectory of the directory containing the
>>> print(x)
11
53 file. The
>>> print(x)
11
51 file will have a filename that starts with the same name as the
>>> print(x)
11
53 file, and ends with
>>> print(x)
11
51, with a middle component that depends on the particular
>>> print(x)
11
57 binary that created it. (See PEP 3147 for details.)

One reason that a

>>> print(x)
11
51 file may not be created is a permissions problem with the directory containing the source file, meaning that the
>>> print(x)
11
52 subdirectory cannot be created. This can happen, for example, if you develop as one user but run as another, such as if you are testing with a web server.

Unless the environment variable is set, creation of a .pyc file is automatic if you’re importing a module and Python has the ability (permissions, free space, etc…) to create a

>>> print(x)
11
52 subdirectory and write the compiled module to that subdirectory.

Running Python on a top level script is not considered an import and no

>>> print(x)
11
51 will be created. For example, if you have a top-level module
>>> print(x)
11
63 that imports another module
>>> print(x)
11
64, when you run
>>> print(x)
11
65 (by typing
>>> print(x)
11
66 as a shell command), a
>>> print(x)
11
51 will be created for
>>> print(x)
11
68 because
>>> print(x)
11
68 is imported, but no
>>> print(x)
11
51 file will be created for
>>> print(x)
11
65 since
>>> print(x)
11
63 isn’t being imported.

If you need to create a

>>> print(x)
11
51 file for
>>> print(x)
11
65 – that is, to create a
>>> print(x)
11
51 file for a module that is not imported – you can, using the and modules.

The module can manually compile any module. One way is to use the

>>> print(x)
11
79 function in that module interactively:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
09

This will write the

>>> print(x)
11
51 to a
>>> print(x)
11
52 subdirectory in the same location as
>>> print(x)
11
63 (or you can override that with the optional parameter
>>> print(x)
11
83).

You can also automatically compile all files in a directory or directories using the module. You can do it from the shell prompt by running

>>> print(x)
11
85 and providing the path of a directory containing Python files to compile:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
10

A module can find out its own module name by looking at the predefined global variable

>>> print(x)
11
86. If this has the value
>>> print(x)
11
87, the program is running as a script. Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking
>>> print(x)
11
86:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
11

Suppose you have the following modules:

>>> print(x)
11
63:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
12

>>> print(x)
11
90:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
13

The problem is that the interpreter will perform the following steps:

  • main imports

    >>> print(x)
    11
    
    65

  • Empty globals for

    >>> print(x)
    11
    
    65 are created

  • >>> print(x)
    11
    
    65 is compiled and starts executing

  • >>> print(x)
    11
    
    65 imports
    >>> print(x)
    11
    
    95

  • Empty globals for

    >>> print(x)
    11
    
    95 are created

  • >>> print(x)
    11
    
    95 is compiled and starts executing

  • >>> print(x)
    11
    
    95 imports
    >>> print(x)
    11
    
    65 (which is a no-op since there already is a module named
    >>> print(x)
    11
    
    65)

  • The import mechanism tries to read

    >>> def foo():
    ...    x = 10
    ...    def bar():
    ...        nonlocal x
    ...        print(x)
    ...        x += 1
    ...    bar()
    ...    print(x)
    ...
    >>> foo()
    10
    11
    
    01 from
    >>> print(x)
    11
    
    65 globals, to set
    >>> def foo():
    ...    x = 10
    ...    def bar():
    ...        nonlocal x
    ...        print(x)
    ...        x += 1
    ...    bar()
    ...    print(x)
    ...
    >>> foo()
    10
    11
    
    03

The last step fails, because Python isn’t done with interpreting

>>> print(x)
11
65 yet and the global symbol dictionary for
>>> print(x)
11
65 is still empty.

The same thing happens when you use

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
06, and then try to access
>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
07 in global code.

There are (at least) three possible workarounds for this problem.

Guido van Rossum recommends avoiding all uses of

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
08, and placing all code inside functions. Initializations of global variables and class variables should use constants or built-in functions only. This means everything from an imported module is referenced as
>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
...
>>> foo()
10
11
09.

Jim Roskind suggests performing steps in the following order in each module:

  • exports (globals, functions, and classes that don’t need imported base classes)

  • >>> def foo():
    ...    x = 10
    ...    def bar():
    ...        nonlocal x
    ...        print(x)
    ...        x += 1
    ...    bar()
    ...    print(x)
    ...
    >>> foo()
    10
    11
    
    10 statements

  • active code (including globals that are initialized from imported values).

Van Rossum doesn’t like this approach much because the imports appear in a strange place, but it does work.

Matthias Urlichs recommends restructuring your code so that the recursive import is not necessary in the first place.

These solutions are not mutually exclusive.

Consider using the convenience function from instead:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
14

For reasons of efficiency as well as consistency, Python only reads the module file on the first time a module is imported. If it didn’t, in a program consisting of many modules where each one imports the same basic module, the basic module would be parsed and re-parsed many times. To force re-reading of a changed module, do this:

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
15

Warning: this technique is not 100% fool-proof. In particular, modules containing statements like

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1
16

will continue to work with the old version of the imported objects. If the module contains class definitions, existing class instances will not be updated to use the new class definition. This can result in the following paradoxical behaviour:

How do you check if a value is not none in Python?

We can check whether a variable is None or not by using the == relational operator in Python. We can check the type of the None object by using the type() in Python. If we compare the None type with None using the equality operator, we will get the result as True. In other cases, we will get the result as False.

What if x is none in Python?

When you do if x is None , you call the operator is , which checks the identity of x . None is a singleton in Python and all None values are also the exact same instance.

What can I use instead of none in Python?

object is a reasonable sentinel if None isn't appropriate - note that you can use default=object (i.e. the class) rather than default=_DEFAULT (an instance) to save the extra assignment. ... .
Your _DEFAULT is a perfectly reasonable way to go..

Is None True or false in Python?

Definition and Usage. The None keyword is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.