Automatic equal distance median line method là gì năm 2024
Several debuggers for Python are described below, and the built-in function allows you to drop into any of them. Show 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 asPEP 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 = 10def bar(): ... print(x) ...bar() 10 works, but this code: > x = 10def foo(): ... print(x) ... x += 1 results in an > x = 10def foo(): ... print(x) ... x += 1 11: > 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 = 10def foo(): ... print(x) ... x += 1 13, the compiler recognizes it as a local variable. Consequently when the earlier > x = 10def foo(): ... print(x) ... x += 1 14 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 = 10def 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: 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 = 10def foo(): ... print(x) ... x += 1 16 was required for all global references, you’d be using > x = 10def foo(): ... print(x) ... x += 1 16 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 = 10def foo(): ... print(x) ... x += 1 16 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 = 10def foo(): ... print(x) ... x += 1 20. You might expect that, when called, they would return, respectively, > x = 10def foo(): ... print(x) ... x += 1 21, > x = 10def foo(): ... print(x) ... x += 1 22, > x = 10def foo(): ... print(x) ... x += 1 23, > x = 10def foo(): ... print(x) ... x += 1 24, and > x = 10def foo(): ... print(x) ... x += 1 25. However, when you actually try you will see that they all return > x = 10def foo(): ... print(x) ... x += 1 25: > squaresundefined 16squaresundefined 16 This happens because > x = 10def foo(): ... print(x) ... x += 1 13 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 = 10def foo(): ... print(x) ... x += 1 13 is > x = 10def foo(): ... print(x) ... x += 1 23, so all the functions now return > x = 10def foo(): ... print(x) ... x += 1 30, i.e. > x = 10def foo(): ... print(x) ... x += 1 25. You can also verify this by changing the value of > x = 10def foo(): ... print(x) ... x += 1 13 and see how the results of the lambdas change: > x = 8squaresundefined 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 = 10def foo(): ... print(x) ... x += 1 13: > squares = []for x in range(5): ... squares.append(lambda n=x: n**2) Here, > x = 10def foo(): ... print(x) ... x += 1 34 creates a new variable > x = 10def foo(): ... print(x) ... x += 1 35 local to the lambda and computed when the lambda is defined so that it has the same value that > x = 10def foo(): ... print(x) ... x += 1 13 had at that point in the loop. This means that the value of > x = 10def foo(): ... print(x) ... x += 1 35 will be > x = 10def foo(): ... print(x) ... x += 1 21 in the first lambda, > x = 10def foo(): ... print(x) ... x += 1 22 in the second, > x = 10def foo(): ... print(x) ... x += 1 40 in the third, and so on. Therefore each lambda will now return the correct result: > squaresundefined 4squaresundefined 16 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 = 10def foo(): ... print(x) ... x += 1 0 mod.py: > x = 10def foo(): ... print(x) ... x += 1 1 main.py: > x = 10def foo(): ... print(x) ... x += 1 2 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 = 10def foo(): ... print(x) ... x += 1 41. 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: Circular imports are fine where both modules use the “import 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 = 10def foo(): ... print(x) ... x += 1 3 The first time you call this function, > x = 10def foo(): ... print(x) ... x += 1 50 contains a single item. The second time, > x = 10def foo(): ... print(x) ... x += 1 50 contains two items because when > x = 10def foo(): ... print(x) ... x += 1 52 begins executing, > x = 10def foo(): ... print(x) ... x += 1 50 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 = 10def foo(): ... print(x) ... x += 1 54, 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 = 10def foo(): ... print(x) ... x += 1 54 as the default value and inside the function, check if the parameter is > x = 10def foo(): ... print(x) ... x += 1 54 and create a new list/dictionary/whatever if it is. For example, don’t write: but: > x = 10def foo(): ... print(x) ... x += 1 4 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 = 10def foo(): ... print(x) ... x += 1 5 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 = 10def foo(): ... print(x) ... x += 1 57 and > x = 10def foo(): ... print(x) ... x += 1 58 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 = 10def foo(): ... print(x) ... x += 1 57 and > x = 10def foo(): ... print(x) ... x += 1 58: > x = 10def foo(): ... print(x) ... x += 1 6 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 = 10def foo(): ... print(x) ... x += 1 7 foo, bar and kwargs are parameters of > x = 10def foo(): ... print(x) ... x += 1 61. However, when calling > x = 10def foo(): ... print(x) ... x += 1 61, for example: > x = 10def foo(): ... print(x) ... x += 1 8 the values > x = 10def foo(): ... print(x) ... x += 1 63, > x = 10def foo(): ... print(x) ... x += 1 64, and > x = 10def foo(): ... print(x) ... x += 1 65 are arguments. If you wrote code like: > x = 10def foo(): ... print(x) ... x += 1 9 you might be wondering why appending an element to > x = 10def foo(): ... print(x) ... x += 1 66 changed > x = 10def foo(): ... print(x) ... x += 1 13 too. There are two factors that produce this result:
After the call to > x = 10def foo(): ... print(x) ... x += 1 73, the content of the mutable object has changed from > x = 10def foo(): ... print(x) ... x += 1 74 to > x = 10def foo(): ... print(x) ... x += 1 75. Since both the variables refer to the same object, using either name accesses the modified value > x = 10def foo(): ... print(x) ... x += 1 75. If we instead assign an immutable object to > x = 10def foo(): ... print(x) ... x += 1 13: > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 0 we can see that in this case > x = 10def foo(): ... print(x) ... x += 1 13 and > x = 10def foo(): ... print(x) ... x += 1 66 are not equal anymore. This is because integers are , and when we do > x = 10def foo(): ... print(x) ... x += 1 80 we are not mutating the int > x = 10def foo(): ... print(x) ... x += 1 81 by incrementing its value; instead, we are creating a new object (the int > x = 10def foo(): ... print(x) ... x += 1
13 (that is, changing which object > x = 10def foo(): ... print(x) ... x += 1 13 refers to). After this assignment we have two objects (the ints > x = 10def foo(): ... print(x) ... x += 1 82 and > x = 10def foo(): ... print(x) ... x += 1
13 now refers to > x = 10def foo(): ... print(x) ... x += 1 82 but > x = 10def foo(): ... print(x) ... x += 1 66 still refers to > x = 10def foo(): ... print(x) ... x += 1 81). Some operations (for example > x = 10def foo(): ... print(x) ... x += 1 91 and > x = 10def foo(): ... print(x) ... x += 1
93 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 = 10def foo(): ... print(x) ... x += 1 54 to help avoid getting the two types of operations confused. So if you mistakenly write > x = 10def foo(): ... print(x) ... x += 1 92 thinking it will give you a sorted copy of > x = 10def foo(): ... print(x) ... x += 1 66, you’ll instead end up with > x = 10def foo(): ... print(x) ... x += 1 54, 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, > x = 10def foo(): ... print(x) ... x += 1 99 mutates lists but not tuples or ints ( > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 00 is equivalent to > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 01 and mutates > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 02, whereas > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 03 and > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 04 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 assignment 13 which returns a function > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 14 that computes the value > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 15. Using nested scopes: > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 5 Or using a callable object: > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 6 In both cases, gives a callable object where > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 16. 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: > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 7 Object can encapsulate state for several methods: > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 8 Here > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 17, > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 18 and > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 19 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: Sequences can be copied by slicing: For an instance > x = 10def foo(): ... print(x) ... x += 1 13 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 25 and > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 26 statements, but in that case the value is a callable. Consider the following code: > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 9 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 27 the created instance is still reported as an instance of class > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 28. 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 29 or > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 30, 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 = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 0 Since the comma is not an operator, but a separator between expressions the above is evaluated as if you had entered: not: The same is true of the various assignment operators ( > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 31, > x = 10def foo(): ... print(x) ... x += 1 99 etc). They are not truly operators but syntactic delimiters in assignment statements. Yes, there is. The syntax is as follows: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 1 Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 2 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 33 form. Yes. Usually this is done by nesting within > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 34. See the following three examples, slightly adapted from Ulf Bartelt: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 3 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: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 4 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: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 5 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: 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: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 6 It’s primarily driven by the desire that > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 38 have the same sign as > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 39. If you want that, and also want: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 7 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 40 need to make > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 38 have the same sign as > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 42. There are few real use cases for > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 38 when > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 39 is negative. When > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 39 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 38 to be > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 47. 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 48 is useful; > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 49 is a bug waiting to bite. Trying to lookup an > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 09 literal attribute in the normal manner gives a because the period is seen as a decimal point: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 8 The solution is to separate the literal from the period with either a space or parentheses. > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 9 For integers, use the built-in type constructor, e.g. > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 53. Similarly, converts to floating-point, e.g. > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 55. By default, these interpret the number as decimal, so that > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 56 holds true, and > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 57 raises . > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 59 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 60. 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 63 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 65 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 66 to the string > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 67, 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 71 yields > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 72 and > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 73 yields > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 74. 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: > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 0 There are various techniques.
You can use > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 80 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 81 without removing other trailing whitespace. If the string > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 81 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 5 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 83 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 84 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 88 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 6 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 7 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 8 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 9 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: > squares = []for x in range(5): ... squares.append(lambda: x**2) 0 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. 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: > squares = []for x in range(5): ... squares.append(lambda: x**2) 1 (another reasonably efficient idiom is to use ) To accumulate many objects, the recommended idiom is to extend a object using in-place concatenation (the > x = 10def foo(): ... print(x) ... x += 1 99 operator): > squares = []for x in range(5): ... squares.append(lambda: x**2) 2 The type constructor > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 03 converts any sequence (actually, any iterable) into a tuple with the same items in the same order. For example, > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 04 yields > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 05 and > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 06 yields > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 07. 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 = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 09 converts any sequence or iterable into a list with the same items in the same order. For example, > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 10 yields > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 11 and > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 12 yields > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 13. If the argument is a list, it makes a copy just like > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 14 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 = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 15 as the same as > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 16. Using negative indices can be very convenient. For example > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 17 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) 3 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) 4 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) 5 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) 6 The list comprehension may be fastest. Use a list: > squares = []for x in range(5): ... squares.append(lambda: x**2) 7 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 76 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) 8 If mutability is desired, you could use lists instead of tuples. Here the analogue of a Lisp car is > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 20 and the analogue of cdr is > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 21. 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: This looks correct if you print it: > squares = []for x in range(5): ... squares.append(lambda: x**2) 9 But when you assign a value, it shows up in multiple places: > squaresundefined 16squaresundefined 16 0 The reason is that replicating a list with > x = 10def foo(): ... print(x) ... x += 1 57 doesn’t create copies, it only creates references to the existing objects. The > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 23 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: > squaresundefined 16squaresundefined 16 1 This generates a list containing 3 different lists of length two. You can also use a list comprehension: > squaresundefined 16squaresundefined 16 2 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: > squaresundefined 16squaresundefined 16 3 To just run the method or function without saving the return values, a plain loop will suffice: > squaresundefined 16squaresundefined 16 4 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 05 and > x = 10def foo(): ... print(x) ... x += 1 99 as our exemplar. If you wrote: > squaresundefined 16squaresundefined 16 5 The reason for the exception should be immediately clear: > x = 10def foo(): ... print(x) ... x += 1 22 is added to the object > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 28 points to ( > x = 10def foo(): ... print(x) ... x += 1 22), producing the result object, > x = 10def foo(): ... print(x) ... x += 1 40, but when we attempt to assign the result of the computation, > x = 10def foo(): ... print(x) ... x += 1 40, to element > x = 10def foo(): ... print(x) ... x += 1 21 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: > squaresundefined 16squaresundefined 16 6 It is the assignment part of the operation that produces the error, since a tuple is immutable. When you write something like: > squaresundefined 16squaresundefined 16 7 The exception is a bit more surprising, and even more surprising is the fact that even though there was an error, the append worked: > squaresundefined 16squaresundefined 16 8 To see why this happens, you need to know that (a) if an object implements an magic method, it gets called when the > x = 10def foo(): ... print(x) ... x += 1 99 augmented assignment is executed, and its return value is what gets used in the assignment statement; and (b) for lists, > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 33 is equivalent to calling > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 36 on the list and returning the list. That’s why we say that for lists, > x = 10def foo(): ... print(x) ... x += 1 99 is a “shorthand” for > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 38: > squaresundefined 16squaresundefined 16 9 This is equivalent to: > x = 8squaresundefined 64 0 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 02. 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 02 was previously pointing to, but the assignment still happens. Thus, in our tuple example what is happening is equivalent to: > x = 8squaresundefined 64 1 The > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 33 succeeds, and thus the list is extended, but even though > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 42 points to the same object that > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 28 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 = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 44 argument for the method: > x = 8squaresundefined 64 2 Merge them into an iterator of tuples, sort the resulting list, and then pick out the element you want. > x = 8squaresundefined 64 3 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 = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 46 class that provides basic accessor methods for a mailbox, and subclasses such as > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 47, > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 48, > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 49 that handle various specific mailbox formats. A method is a function on some object > x = 10def foo(): ... print(x) ... x += 1 13 that you normally call as > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 51. Methods are defined as functions inside the class definition: > x = 8squaresundefined 64 4 Self is merely a conventional name for the first argument of a method. A method defined as > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 52 should be called as > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 53 for some instance > x = 10def foo(): ... print(x) ... x += 1 13 of the class in which the definition occurs; the called method will think it is called as > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 55. 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 = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 57, and can also check whether an object is one of Python’s built-in types, e.g. > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 58 or > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 59. Note that also checks for virtual inheritance from an . So, the test will return > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 61 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 = 8squaresundefined 64 5 > x = 8squaresundefined 64 6 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 = 8squaresundefined 64 7 A better approach is to define a > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 63 method on all the classes and just call it: > x = 8squaresundefined 64 8 Delegation is an object oriented technique (also called a design pattern). Let’s say you have an object > x = 10def foo(): ... print(x) ... x += 1 13 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 = 10def foo(): ... print(x) ... x += 1 13. 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 = 8squaresundefined 64 9 Here the > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 66 class redefines the > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 67 method to convert the argument string to uppercase before calling the underlying > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 68 method. All other methods are delegated to the underlying > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 69 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 = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 71 is roughly equivalent to the following: > squares = []for x in range(5): ... squares.append(lambda n=x: n**2) 0 Most > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 71 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) 1 In the example, will automatically determine the instance from which it was called (the > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 77 value), look up the (MRO) with > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 78, and return the next in line after > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 79 in the MRO: > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 80. 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) 2 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) 3 > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 81 also refers to > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 82 for any > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 83 such that > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 84 holds, unless overridden by > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 83 itself or by some class on the base-class search path from > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 86 back to > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 87. Caution: within a method of C, an assignment like > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 88 creates a new and unrelated instance named “count” in > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 77’s own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not: 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 > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 90 (at least two leading underscores, at most one trailing underscore) is textually replaced with > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 91, where > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 92 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 > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 93. 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 > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 95 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 > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 95 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 > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 95 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 > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 00 method on objects to be called whenever you’re done with them. The > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 00 method can then remove attributes that refer to subobjects. Don’t call > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 95 directly – > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 95 should call > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 00 and > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 00 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 > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 95 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 = 10def foo(): ... print(x) ... x += 1 08 call. To be sure that objects whose id you want to examine are still alive, create another reference to the object: > squaresundefined 4squaresundefined 16 0 The > foo() Traceback (most recent call last): ... UnboundLocalError: local variable 'x' referenced before assignment 11 operator tests for object identity. The test > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 13 is equivalent to > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 14. The most important property of an identity test is that an object is always identical to itself, > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 15 always returns > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 61. Identity tests are usually faster than equality tests. And unlike equality tests, identity tests are guaranteed to return a boolean > x = 10def foobar(): ... global x ... print(x) ... x += 1 ...foobar() 10 61 or > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 18. However, identity tests can only be substituted for equality tests when object identity is assured. Generally, there are three circumstances where identity is guaranteed:
19, it is guaranteed that > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 20.
21, it is guaranteed that > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 22.
23 and > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 24, it is guaranteed that > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 13 because > x = 10def foo(): ... print(x) ... x += 1 54 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: > squaresundefined 4squaresundefined 16 1 Likewise, new instances of mutable containers are never identical: > squaresundefined 4squaresundefined 16 2 In the standard library code, you will see several common patterns for correctly using identity tests:
54. This reads like plain English in code and avoids confusion with other objects that may have boolean values that evaluate to false.
54 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 : > squaresundefined 4squaresundefined 16 3
32 that are not equal to themselves. For example, here is the implementation of > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 33: > squaresundefined 4squaresundefined 16 4 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: > squaresundefined 4squaresundefined 16 5 The classes can be used like this: > squaresundefined 4squaresundefined 16 6 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 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: > squaresundefined 4squaresundefined 16 7 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: > squaresundefined 4squaresundefined 16 8 When a module is imported for the first time (or when the source file has changed since the current compiled file was created) a > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 file containing the compiled code should be created in a > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 41 subdirectory of the directory containing the > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 42 file. The > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 file will have a filename that starts with the same name as the > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 42 file, and ends with > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40, with a middle component that depends on the particular > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 46 binary that created it. (See PEP 3147 for details.) One reason that a > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 file may not be created is a permissions problem with the directory containing the source file, meaning that the > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 41 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 > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 41 subdirectory and write the compiled module to that subdirectory. Running Python on a top level script is not considered an import and no > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 will be created. For example, if you have a top-level module > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 52 that imports another module > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 53, when you run > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 54 (by typing > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 55 as a shell command), a > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 will be created for > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 57 because > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 57 is imported, but no > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 file will be created for > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 54 since > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 52 isn’t being imported. If you need to create a > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 file for > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 54 – that is, to create a > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 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 > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 68 function in that module interactively: > squaresundefined 4squaresundefined 16 9 This will write the > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 40 to a > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 41 subdirectory in the same location as > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 52 (or you can override that with the optional parameter > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 72). 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 > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 74 and providing the path of a directory containing Python files to compile: A module can find out its own module name by looking at the predefined global variable > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 75. If this has the value > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 76, 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 > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 75: > x = 10def foo(): ... print(x) ... x += 1 00 Suppose you have the following modules: > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 52: > x = 10def foo(): ... print(x) ... x += 1 01 > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 79: > x = 10def foo(): ... print(x) ... x += 1 02 The problem is that the interpreter will perform the following steps:
The last step fails, because Python isn’t done with interpreting > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 54 yet and the global symbol dictionary for > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 54 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 95, and then try to access > def foo(): ... x = 10 ... def bar(): ... nonlocal x ... print(x) ... x += 1 ... bar() ... print(x) ...foo() 10 11 96 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 97, 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 98. 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 = 10def foo(): ... print(x) ... x += 1 03 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 = 10def foo(): ... print(x) ... x += 1 04 Warning: this technique is not 100% fool-proof. In particular, modules containing statements like > x = 10def foo(): ... print(x) ... x += 1 05 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: |