A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
3.2 Assigning values to variablesIn the assignment operator Python language, the equal sign (=) is the primary assignment operator. Note that the assignment does not directly assign a value to a variable, although you might think that it should be the case with other language programming experience. In the Python language, objects are passed by reference. When assigning a value, whether the object is newly created or an existing one, the reference to the object (not the value) is assigned to the variable. Similarly, if you are more familiar with C, you will know that an assignment statement is actually treated as an expression (which can return a value). This is not suitable for Python, however, and Python's assignment statements do not return a value. Chained assignment
Increment assignment>>> y = x = x + 1>>> x, Y (2, 2)
>>> x = 1>>> x + = 1>>> X2
Multivariate assignment (when assigning a value in this way, the objects on both sides of the equal sign are tuples)
>>> x, y, z = 1, 2, ' A string ' >>> x1>>> y2>>> z ' A string ' Python's multivariate assignment method can be achieved without intermediate change The value of two variables for the volume exchange.
3.3 identifiersThe legal Python identifier for Python identifier string rules is similar to most other high-level languages written in C:?? The first character must be a letter or an underscore (_)
BuiltIn addition to the keywords, Python has a collection of "built-in" names that can be used at any level of code, which can be set or used by the interpreter. Although built-in is not a keyword, it should be treated as a "system reserved word" and not used by him. However, there are situations that require overrides (that is, redefine, replace) them. Python does not support overloaded identifiers, so there is only one name binding at any given time. We can also tell the advanced reader that built-in is a member of the __builtins__ module, which is automatically imported by the interpreter before your program starts or before the >>> prompt in the interactive interpreter. Think of them as global variables that apply to any level of Python code. Private underline identifiers Python uses underscores to specify special variables as variable prefixes and suffixes.
Core style: Avoid underlining as the start of the variable name Because the underscore has special meaning for the interpreter and is the symbol used by the built-in identifier, We recommend that programmers avoid using underscores as the start of variable names. in general, variable name _xxx is considered "private" and cannot be used outside of a module or class. It is good practice to use _xxx to represent variables when the variables are private. Because the variable name __xxx__ has a special meaning for Python, this naming style should be avoided for common variables. 3.4 Basic Style Guide comments, documents, indents, identifiers module structure and layout # (1) Start line (Unix) & nbsp # (2) Module documentation # (3) module import # (4) Variable definition # (5) class Semantic # (6) function definition # (7) Main program (1) Start line &nbs P Usually only use the starting line in a Unix-like environment, with the start line being able to enter only the script name to execute the script, no need to invoke the interpreter directly. (2) module documentation briefly introduces the function of the module and the meaning of the important global variables, which can be accessed via module.__doc__ outside the module. Some content. (3) module import Import all modules required for the current module's code;The module is imported only once (when the current module is loaded), and the module import code inside the function is not executed unless the function is executing. (4) Variable definition The variables defined here are global variables, and all functions in this module can be used directly. from a good programming style point of view, unless it is necessary to use local variables instead of global variables, if you persist in doing so, your code will not only be easy to maintain, but also improve performance and save memory. (5) class definition statements All classes need to be defined here. When the module is imported, the class statement is executed and the classes are defined. The document variable for the class is class.__doc__. (6) Function definition statement The function defined here can be accessed externally through module.function () , the DEF statement will be executed when the module is imported, and the function will be defined, and the function's document variable is function.__doc__. (7) Main program This part of the code will be executed regardless of whether the module is imported by another module or executed directly as a script. Usually there is not too much functional code here, but different functions are called according to the mode of execution. Figure 3–1 Typical Python file structureRecommended code style: Main program call Main () functionSummary: _xxx without ' from module import * ' Import?? __xxx__ System Definition Name
?? Private variable names in the __xxx class
Variable definition?? variable without prior declaration
?? Variable does not need to specify type
?? Programmers don't care about memory management
?? Variable names are "recycled"
?? Del statement can release resources directly
A reference count internal tracking variable, called a reference counter.When this object is no longer needed, that is, the reference count of this object becomes 0 o'clock, it is garbage collected. Increase reference count When an object is created and assigned to a variable, the reference count of the object is set to 1. When the same object (reference) is assigned to another variable, or passed as a parameter to a function, method or class instance when, or assigned to a window Port object, a new reference to the object, or an alias, is created (the object's reference count is automatically added to 1). Reference count of objects in ?? object is created x = 3.14 ?? Or another alias is created y = x ?? Or passed as a parameter to the function (new local reference) Foobar (x) ?? or become an element of the container object myList = [123, X, ' XYZ '] reduce the reference count When the object's reference is destroyed, the reference count is reduced. The most obvious example of is that when a reference leaves its scope, this happens most often at the end of the function, when all local variables are automatically destroyed and the reference count of the object is reduced. When a variable is assigned to another object, the reference count of the original object is automatically reduced 1: foo = ' xyz ' # ' xyz ' 1 &NB Sp bar = foo # ' XYZ ' 2 foo = 123 # ' xyz ' 1   ; Other ways to reduce the reference count of objects include using the DEL statement to delete a variable (see the next section), or when an objectWhen a Window object is moved out (or the reference count of the container object itself becomes 0 o'clock). The reference count for an object reduces: in the following cases: A local reference has left its scope. such as Foobar () (see previous example) at the end of the function. ?? The alias of the object is explicitly destroyed. del y # or del x ?? Remove y from the current namespaces X's reference count minus one ?? An alias of an object is assigned to another object x = 123 ?? object is removed from a Window object Mylist.remove (x) ?? The Window object itself is destroyed del myList # or goes out-of-scope garbage collection The memory that is no longer being used is a machine called garbage collection System release. Although the interpreter tracks the reference count of the object, the garbage collector is responsible for freeing the memory. The garbage collector is a separate piece of code that is used to look for objects with a reference count of 0. It is also responsible for checking those objects whose reference count is greater than 0 but should also be destroyed. Specific situations can cause circular references. A circular reference occurs when you have at least two objects referencing each other, which means that when all references are gone, theseReferences still exist, which means that reference counting alone is not enough. Python's garbage collector is actually a reference counter and a cyclic garbage collector. When an object's reference count becomes 0, the interpreter pauses, releasing the object and other objects that only the object can access (reachable). as a reference count supplement, the garbage collector also pays attention to objects that are allocated a large amount (and those that are not destroyed by reference counting). In this case, the interpreter pauses to attempt to clean up all unreferenced loops. Core tip: Replace module variables with local variables
?? Profilers:profile, hotshot, CProfile
Python basics-"python core programming"
Start building with 50+ products and up to 12 months usage for Elastic Compute Service