The conditional expression will return the default value if the variable stores Show Conditional expressions are very similar to an In the examples, we check if the variable stores You can use this approach to return a default value if An alternative approach is to use the boolean The expression However, this approach does not explicitly check for The boolean All values that are not truthy are considered falsy. The falsy values in Python are:
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:
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:
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 += 122: >>> 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 += 124, the compiler recognizes it as a local variable. Consequently when the earlier >>> x = 10 >>> def foo(): ... print(x) ... x += 125 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 += 127 was required for all global references, you’d be using >>> x = 10 >>> def foo(): ... print(x) ... x += 127 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 += 127 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 += 131. You might expect that, when called, they would return, respectively, >>> x = 10 >>> def foo(): ... print(x) ... x += 132, >>> x = 10 >>> def foo(): ... print(x) ... x += 133, >>> x = 10 >>> def foo(): ... print(x) ... x += 134, >>> x = 10 >>> def foo(): ... print(x) ... x += 135, and >>> x = 10 >>> def foo(): ... print(x) ... x += 136. However, when you actually try you will see that they all return >>> x = 10 >>> def foo(): ... print(x) ... x += 136: >>> squares[2]() 16 >>> squares[4]() 16 This happens because >>> x = 10 >>> def foo(): ... print(x) ... x += 124 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 += 124 is >>> x = 10 >>> def foo(): ... print(x) ... x += 134, so all the functions now return >>> x = 10 >>> def foo(): ... print(x) ... x += 141, i.e. >>> x = 10 >>> def foo(): ... print(x) ... x += 136. You can also verify this by changing the value of >>> x = 10 >>> def foo(): ... print(x) ... x += 124 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 += 124: >>> squares = [] >>> for x in range(5): ... squares.append(lambda n=x: n**2) Here, >>> x = 10 >>> def foo(): ... print(x) ... x += 145 creates a new variable >>> x = 10 >>> def foo(): ... print(x) ... x += 146 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 += 124 had at that point in the loop. This means that the value of >>> x = 10 >>> def foo(): ... print(x) ... x += 146 will be >>> x = 10 >>> def foo(): ... print(x) ... x += 132 in the first lambda, >>> x = 10 >>> def foo(): ... print(x) ... x += 133 in the second, >>> x = 10 >>> def foo(): ... print(x) ... x += 151 in the third, and so on. Therefore each lambda will now return the correct result: >>> x = 10 >>> def foo(): ... print(x) ... x += 10 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 += 11 mod.py: >>> x = 10 >>> def foo(): ... print(x) ... x += 12 main.py: >>> x = 10 >>> def foo(): ... print(x) ... x += 13 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 += 152. 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:
It is sometimes necessary to move imports to a function or class to avoid problems with circular imports. Gordon McMillan says:
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 += 14 The first time you call this function, >>> x = 10 >>> def foo(): ... print(x) ... x += 161 contains a single item. The second time, >>> x = 10 >>> def foo(): ... print(x) ... x += 161 contains two items because when >>> x = 10 >>> def foo(): ... print(x) ... x += 163 begins executing, >>> x = 10 >>> def foo(): ... print(x) ... x += 161 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 += 165, 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 += 165 as the default value and inside the function, check if the parameter is >>> x = 10 >>> def foo(): ... print(x) ... x += 165 and create a new list/dictionary/whatever if it is. For example, don’t write: >>> x = 10 >>> def foo(): ... print(x) ... x += 15 but: >>> x = 10 >>> def foo(): ... print(x) ... x += 16 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 += 17 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 += 168 and >>> x = 10 >>> def foo(): ... print(x) ... x += 169 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 += 168 and >>> x = 10 >>> def foo(): ... print(x) ... x += 169: >>> x = 10 >>> def foo(): ... print(x) ... x += 18 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 += 19 foo, bar and kwargs are parameters of >>> x = 10 >>> def foo(): ... print(x) ... x += 172. However, when calling >>> x = 10 >>> def foo(): ... print(x) ... x += 172, for example: >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment0 the values >>> x = 10 >>> def foo(): ... print(x) ... x += 174, >>> x = 10 >>> def foo(): ... print(x) ... x += 175, and >>> x = 10 >>> def foo(): ... print(x) ... x += 176 are arguments. If you wrote code like: >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment1 you might be wondering why appending an element to >>> x = 10 >>> def foo(): ... print(x) ... x += 177 changed >>> x = 10 >>> def foo(): ... print(x) ... x += 124 too. There are two factors that produce this result:
After the call to >>> x = 10 >>> def foo(): ... print(x) ... x += 184, the content of the mutable object has changed from >>> x = 10 >>> def foo(): ... print(x) ... x += 185 to >>> x = 10 >>> def foo(): ... print(x) ... x += 186. Since both the variables refer to the same object, using either name accesses the modified value >>> x = 10 >>> def foo(): ... print(x) ... x += 186. If we instead assign an immutable object to >>> x = 10 >>> def foo(): ... print(x) ... x += 124: >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment2 we can see that in this case >>> x = 10 >>> def foo(): ... print(x) ... x += 124 and >>> x = 10 >>> def foo(): ... print(x) ... x += 177 are not equal anymore. This is because integers are , and when we do >>> x = 10 >>> def foo(): ... print(x) ... x += 191 we are not mutating the int >>> x = 10 >>> def foo(): ... print(x) ... x += 192 by incrementing its value; instead, we are creating a new object (the int >>> x = 10 >>> def foo(): ... print(x) ... x += 193) and assigning it to >>> x = 10 >>> def foo(): ... print(x) ... x += 124 (that is, changing which object >>> x = 10 >>> def foo(): ... print(x) ... x += 124 refers to). After this assignment we have two objects (the ints >>> x = 10 >>> def foo(): ... print(x) ... x += 193 and >>> x = 10 >>> def foo(): ... print(x) ... x += 192) and two variables that refer to them ( >>> x = 10 >>> def foo(): ... print(x) ... x += 124 now refers to >>> x = 10 >>> def foo(): ... print(x) ... x += 193 but >>> x = 10 >>> def foo(): ... print(x) ... x += 177 still refers to >>> x = 10 >>> def foo(): ... print(x) ... x += 192). Some operations (for example >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment02 and >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment03) mutate the object, whereas superficially similar operations (for example >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment04 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 += 165 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 assignment03 thinking it will give you a sorted copy of >>> x = 10 >>> def foo(): ... print(x) ... x += 177, you’ll instead end up with >>> x = 10 >>> def foo(): ... print(x) ... x += 165, 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 assignment10 mutates lists but not tuples or ints ( >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment11 is equivalent to >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment12 and mutates >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment13, whereas >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment14 and >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment15 create new objects). In other words:
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.
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 assignment24 which returns a function >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment25 that computes the value >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment26. Using nested scopes: >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment7 Or using a callable object: >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment8 In both cases, >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment9 gives a callable object where >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment27. 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() 100 Object can encapsulate state for several methods: >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 101 Here >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment28, >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment29 and >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment30 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() 102 Sequences can be copied by slicing: >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 103 For an instance >>> x = 10 >>> def foo(): ... print(x) ... x += 124 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 assignment36 and >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment37 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() 104 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 assignment38 the created instance is still reported as an instance of class >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment39. However, it is impossible to say whether the instance’s name is >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment40 or >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment41, 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:
Comma is not an operator in Python. Consider this session: >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 105 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() 106 not: >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 107 The same is true of the various assignment operators ( >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment42, >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment10 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() 108 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() 109 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 assignment44 form. Yes. Usually this is done by nesting within >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment45. See the following three examples, slightly adapted from Ulf Bartelt: >>> print(x) 110 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) 111 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) 112 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) 113 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) 114 It’s primarily driven by the desire that >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment49 have the same sign as >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment50. If you want that, and also want: >>> print(x) 115 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 assignment51 need to make >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment49 have the same sign as >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment53. There are few real use cases for >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment49 when >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment50 is negative. When >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment50 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 assignment49 to be >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment58. 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 assignment59 is useful; >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment60 is a bug waiting to bite. Trying to lookup an >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment20 literal attribute in the normal manner gives a because the period is seen as a decimal point: >>> print(x) 116 The solution is to separate the literal from the period with either a space or parentheses. >>> print(x) 117 For integers, use the built-in type constructor, e.g. >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment64. Similarly, converts to floating-point, e.g. >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment66. By default, these interpret the number as decimal, so that >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment67 holds true, and >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment68 raises . >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment70 takes the base to convert from as a second optional argument, so >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment71. 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 assignment74 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 assignment76 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 assignment77 to the string >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment78, 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 assignment82 yields >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment83 and >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment84 yields >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment85. 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) 118 There are various techniques.
You can use >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment91 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 assignment92 without removing other trailing whitespace. If the string >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment92 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 113 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 assignment94 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 assignment95 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 assignment99 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 114 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 115 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 116 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 117 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 118 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:
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:
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 119 (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 assignment10 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() 1014 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() 1015 yields >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1016 and >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1017 yields >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1018. 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() 1020 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() 1021 yields >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1022 and >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1023 yields >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1024. If the argument is a list, it makes a copy just like >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1025 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() 1026 as the same as >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1027. Using negative indices can be very convenient. For example >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1028 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:
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 assignment87 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() 1031 and the analogue of cdr is >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1032. 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 += 168 doesn’t create copies, it only creates references to the existing objects. The >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1034 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]() 160 This generates a list containing 3 different lists of length two. You can also use a list comprehension: >>> squares[2]() 16 >>> squares[4]() 161 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]() 162 To just run the method or function without saving the return values, a plain loop will suffice: >>> squares[2]() 16 >>> squares[4]() 163 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 assignment16 and >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment10 as our exemplar. If you wrote: >>> squares[2]() 16 >>> squares[4]() 164 The reason for the exception should be immediately clear: >>> x = 10 >>> def foo(): ... print(x) ... x += 133 is added to the object >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1039 points to ( >>> x = 10 >>> def foo(): ... print(x) ... x += 133), producing the result object, >>> x = 10 >>> def foo(): ... print(x) ... x += 151, but when we attempt to assign the result of the computation, >>> x = 10 >>> def foo(): ... print(x) ... x += 151, to element >>> x = 10 >>> def foo(): ... print(x) ... x += 132 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]() 165 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]() 166 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]() 167 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 assignment10 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() 1044 is equivalent to calling >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1047 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 assignment10 is a “shorthand” for >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1049: >>> squares[2]() 16 >>> squares[4]() 168 This is equivalent to: >>> squares[2]() 16 >>> squares[4]() 169 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 assignment13. 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 assignment13 was previously pointing to, but the assignment still happens. Thus, in our tuple example what is happening is equivalent to: >>> x = 8 >>> squares[2]() 640 The >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1044 succeeds, and thus the list is extended, but even though >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1053 points to the same object that >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1039 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() 1055 argument for the method: >>> x = 8 >>> squares[2]() 641 Merge them into an iterator of tuples, sort the resulting list, and then pick out the element you want. >>> x = 8 >>> squares[2]() 642 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() 1057 class that provides basic accessor methods for a mailbox, and subclasses such as >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1058, >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1059, >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1060 that handle various specific mailbox formats. A method is a function on some object >>> x = 10 >>> def foo(): ... print(x) ... x += 124 that you normally call as >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1062. Methods are defined as functions inside the class definition: >>> x = 8 >>> squares[2]() 643 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() 1063 should be called as >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1064 for some instance >>> x = 10 >>> def foo(): ... print(x) ... x += 124 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() 1066. 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() 1068, 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() 1069 or >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1070. 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() 1072 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]() 644 >>> x = 8 >>> squares[2]() 645 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]() 646 A better approach is to define a >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1074 method on all the classes and just call it: >>> x = 8 >>> squares[2]() 647 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 += 124 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 += 124. 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]() 648 Here the >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1077 class redefines the >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1078 method to convert the argument string to uppercase before calling the underlying >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1079 method. All other methods are delegated to the underlying >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1080 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() 1082 is roughly equivalent to the following: >>> x = 8 >>> squares[2]() 649 Most >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1082 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() 1088 value), look up the (MRO) with >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1089, and return the next in line after >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1090 in the MRO: >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1091. 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() 1092 also refers to >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1093 for any >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1094 such that >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1095 holds, unless overridden by >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1094 itself or by some class on the base-class search path from >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1097 back to >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1098. Caution: within a method of C, an assignment like >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1099 creates a new and unrelated instance named “count” in >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1088’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) 1101 (at least two leading underscores, at most one trailing underscore) is textually replaced with >>> print(x) 1102, where >>> print(x) 1103 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) 1104. 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) 1106 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) 1106 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) 1106 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) 1111 method on objects to be called whenever you’re done with them. The >>> print(x) 1111 method can then remove attributes that refer to subobjects. Don’t call >>> print(x) 1106 directly – >>> print(x) 1106 should call >>> print(x) 1111 and >>> print(x) 1111 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) 1106 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 += 119 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 += 100 The >>> foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment22 operator tests for object identity. The test >>> print(x) 1124 is equivalent to >>> print(x) 1125. The most important property of an identity test is that an object is always identical to itself, >>> print(x) 1126 always returns >>> x = 10 >>> def foobar(): ... global x ... print(x) ... x += 1 ... >>> foobar() 1072. 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() 1072 or >>> print(x) 1129. 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) 1130, it is guaranteed that >>> print(x) 1131. 2) Putting an object in a container that stores object references does not change object identity. After the list assignment >>> print(x) 1132, it is guaranteed that >>> print(x) 1133. 3) If an object is a singleton, it means that only one instance of that object can exist. After the assignments >>> print(x) 1134 and >>> print(x) 1135, it is guaranteed that >>> print(x) 1124 because >>> x = 10 >>> def foo(): ... print(x) ... x += 165 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 += 101 Likewise, new instances of mutable containers are never identical: >>> x = 10 >>> def foo(): ... print(x) ... x += 102 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 += 165. 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 += 165 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 += 103 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) 1143 that are not equal to themselves. For example, here is the implementation of >>> print(x) 1144: >>> x = 10 >>> def foo(): ... print(x) ... x += 104 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 += 105 The classes can be used like this: >>> x = 10 >>> def foo(): ... print(x) ... x += 106 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 += 107 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 += 108 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) 1151 file containing the compiled code should be created in a >>> print(x) 1152 subdirectory of the directory containing the >>> print(x) 1153 file. The >>> print(x) 1151 file will have a filename that starts with the same name as the >>> print(x) 1153 file, and ends with >>> print(x) 1151, with a middle component that depends on the particular >>> print(x) 1157 binary that created it. (See PEP 3147 for details.) One reason that a >>> print(x) 1151 file may not be created is a permissions problem with the directory containing the source file, meaning that the >>> print(x) 1152 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) 1152 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) 1151 will be created. For example, if you have a top-level module >>> print(x) 1163 that imports another module >>> print(x) 1164, when you run >>> print(x) 1165 (by typing >>> print(x) 1166 as a shell command), a >>> print(x) 1151 will be created for >>> print(x) 1168 because >>> print(x) 1168 is imported, but no >>> print(x) 1151 file will be created for >>> print(x) 1165 since >>> print(x) 1163 isn’t being imported. If you need to create a >>> print(x) 1151 file for >>> print(x) 1165 – that is, to create a >>> print(x) 1151 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) 1179 function in that module interactively: >>> x = 10 >>> def foo(): ... print(x) ... x += 109 This will write the >>> print(x) 1151 to a >>> print(x) 1152 subdirectory in the same location as >>> print(x) 1163 (or you can override that with the optional parameter >>> print(x) 1183). 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) 1185 and providing the path of a directory containing Python files to compile: >>> x = 10 >>> def foo(): ... print(x) ... x += 110 A module can find out its own module name by looking at the predefined global variable >>> print(x) 1186. If this has the value >>> print(x) 1187, 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) 1186: >>> x = 10 >>> def foo(): ... print(x) ... x += 111 Suppose you have the following modules: >>> print(x) 1163: >>> x = 10 >>> def foo(): ... print(x) ... x += 112 >>> print(x) 1190: >>> x = 10 >>> def foo(): ... print(x) ... x += 113 The problem is that the interpreter will perform the following steps:
The last step fails, because Python isn’t done with interpreting >>> print(x) 1165 yet and the global symbol dictionary for >>> print(x) 1165 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 1106, and then try to access >>> def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ... >>> foo() 10 1107 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 1108, 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 1109. Jim Roskind suggests performing steps in the following order in each module:
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 += 114 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 += 115 Warning: this technique is not 100% fool-proof. In particular, modules containing statements like >>> x = 10 >>> def foo(): ... print(x) ... x += 116 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.
|