& python closure copy & shades with garbage collection & statements

1.1 Closures

  1, the concept of closure

      1. Define a function outside an inner function, the function in the use of temporary variables outside the function and the return value is outside the reference function thus constitutes a closure

      2. Under normal circumstances, we recognize them, if a function is over, everything will be relieved of internal functions, returned to memory, local variables will disappear.

      3. However, the closure is a special case, if the external function found at the end of their temporary variables inside the function will be used in the future, put the temporary variable is bound to an internal function, and then ended his own again.

   2, closure features    

      1. There must be an inline function

      2. The embedded function must reference an external variable in the function

      3. The external function return values ​​must be built-in functions

# Closure function instance 
DEF Outer (A): 
    B = 10
     DEF Inner ():           # internal function uses temporary variables outside the function 
        Print (A + B)         # return a value outside the reference function is a function of the 
    return Inner 

IF  the __name__ == ' __main__ ' : 
    Demo = outer (5 ) 
    Demo ()     # 15 

# here we call external functions pass parameters 5 
# at this time the outer two functions is a temporary variable B is 5 10, and create a within a function, then the function returns a reference within the deposit to the Demo 
# when found outside a function of the end of the function will use its own internal temporary variables, these two variables will not be temporary release, we will be bound to the internal function 
# we call the internal function, take a look at temporary variable inside the function is not able to use external functions 
# demo stored outside the function return values, i.e. referenced inner function, where the function corresponding to the implementation of inner
Examples closure

  3, the outer closure function modification function local variables 

    1, in which the basic syntax of python, a function can be read without global data, but to modify global data when there are two methods:
        1) Global global variable declarations
        2) the global variable is a variable type of data can be modified when
    2 using the following two methods in the case of closure modification
        1) in python3 can declare a variable with the nonlocal keyword, this variable is not a variable representing the local variable space, one needs to variable space looking up the variable.
        2) In python2, no nonlocal keyword, we can change the variable type closure variable data modifications, such as a list.

# Modified example of closure variable 
DEF Outer (A): 
    b = 10             # A closure and b are variable 
    C = [A]            # where variables corresponding to the modified method of closure 2 
    DEF Inner ():
         # Method a: nonlocal keyword statement (to python3) 
        nonlocal B 
        B +. 1 = # method 2: the modified closure variable into a variable data type such as a list (python2) 
        C [0] + =. 1 Print (C [0])
         Print (B)
     return inner          # outside the function returns a reference to the function of the IF the __name__ == ' __main__ ' : 
    Demo = outer (. 5 )

        
        

 
    demo()             # 6  11
A function to modify the outer closure function the local variable

1.2 Python in the copy

   1, a prior knowledge of the variables and their storage --python

       All variables 1. python is an object, store variables, using a reference semantics, but the value of a variable memory address where the store, rather than the variable itself only

       2. No matter how complex data structures, shallow copy will only copy layer.

       Understanding : two public a table, as long as the table unchanged, food on the table has changed is the common feeling of two people.

Str1 = >>> ' the Hello ' 
>>> str2 = str1 

# 1, so that str1 and str2 variables are stored in the memory address 'hello' where 
>>> the above mentioned id (str1)
 22.74828 million 
>>> the above mentioned id (str1)
 22.74828 million 
>> > # 2, when the value becomes the value str1 str1 the 'new hello' to be reassigned 'new hello' memory address, the value is still str2 'hello' memory address 
>>> str1 = ' new new Hello ' 
>>> ID (str1)
 22.74832 million 
>>> ID (str2)
 22.74828 million # . 3, no matter how complex data structures, shallow copy will only one copy. 
SourceList = >>> [1,2, [3,4- ]]
 >>> newList = sourceList >>>






1, 2, [100, 4]]
>>> newList
[1, 2, [100, 4]]
No matter how complex data structures, shallow copy will only copy one

   2, with a shallow copy deepcopy

      1, a shallow copy : no matter how complex data structures, shallow copy will only copy one

      2, a DeepCopy  : deep copy completely original copy of all data relevant variables to generate a set of exactly the same content in memory, we have these two variables in any of the modifications will not affect other variables

import copy
sourceList = [1,2,3,[4,5,6]]
copyList = copy.copy(sourceList)
deepcopyList = copy.deepcopy(sourceList)

sourceList[3][0]=100

print(sourceList)           # [1, 2, 3, [100, 5, 6]]
print(copyList)             # [1, 2, 3, [100, 5, 6]]
print(deepcopyList)         # [1, 2, 3, [4, 5, 6]]
Shallow copy and deepcopy

1.3 Python garbage collection 

    Garbage collection: https://www.cnblogs.com/TM0831/p/10599716.html

  1, the reference count

    1. Principle

        1) When a reference is created or copied object, the object reference count plus 1; when a reference object is destroyed, the reference count of the object decreased by one.
        2) when the object's reference count is reduced to zero, it means the object has no longer been used, it can be freed memory.

    2. advantage

        The reference count has a great advantage that real-time, any memory, once there is no reference to it, will be recovered immediately, while others refuse collection techniques must reclaim memory to be invalid under certain special conditions.

    3. drawback

        1) maintain reference counting mechanism brought referenced memory allocation and release of additional operations count and Python run carried out, citations assignment is directly proportional
        2) which is obviously caused by those other than garbage collection techniques additional operating only with the amount of memory to be recovered related to less efficient.
        3) At the same time, because of mutual references between objects, each object references will not be zero, so these objects are always occupied by the memory will not be released.

  2, Mark - Clear

    1. Description  

        1) It is divided into two phases: the first phase is a marker, the GC will all active objects marked tag, the second stage of those objects not marked inactive objects recovered.

        2) between objects connected together by reference (pointer) to form a directed graph

        3) From the root object (root object) departure, there are marked along the traverse objects to the side, reachable (reachable) objects for the active object, inactive objects unreachable object is to be cleared.

              The root object is global variables, call stack, registers.

        Note: like PyIntObject, PyStringObject immutable objects which would not be possible circular reference, because they can not hold the internal references to other objects.

             

          1. In the figure, program variables can be accessed directly from block 1, and may be accessed indirectly blocks 2 and 3, can not access blocks 4 and 5
          2. The first step is marking blocks 1, 2 and 3 and remember to block for later processing.
          Step 3. The marker block 2, block 3, the third step will mark, but can not remember block 2, since it has been marked.
          4. The block scan phase 2 and 3 will be ignored, because they have been marked, but the recovery block 4 and 5.

    2, shortcomings

        1) Clear flag as an auxiliary algorithm of garbage collection techniques Python, are some of the main processing container objects, such as list, dict, tuple, etc.

             Because for strings, numbers, objects are unlikely to cause a circular reference problem.

        2) pre-purge inactive object it must scan the entire sequence of heap memory, even if only a small portion of active objects also scan all objects.

   3, recovered generational

      1. Upon generation of the recovery technique is based on the basis of clear marking, an operation space for time.

      2. Python memory is divided into three "generations", respectively, for the young generation (generation 0), in the year (1st generation), the old year (2nd generation)

      3. They correspond to the three lists, the object of increasing the frequency of collection of survival time decrease waste thereof.

      4. The newly created objects are allocated in the young generation, the total number of the young generation of the list reaches the upper limit, Python garbage collection mechanism will be triggered

      5. The objects that can be recycled out of the recovery, and those objects are not recovered will be moved in years to go, and so on

      6. The old era of objects is the longest survival time of the object, or even survive in the life cycle of the system.

1.4 Context Management (with)

  1. What is the with statement

      1. with a context management protocol object from the flowchart try, except keywords and resource allocation, and finally releasing all relevant code removed, try ... .except ... .finlally simplified process flow.

      2.  Therefore, with the use of the object of processing must enter () and exit () method of the two

        1) with enter through the initialization method (method enter into operation before the statement executor)

        2) Then do as well in the aftermath of the exit handle exceptions (exit () method runs after the statement body is finished exit)

  2, with the statement usage scenarios

      1. with the statement for the occasion access to resources, ensuring that regardless of whether an exception occurs in the course of the implementation of the necessary "clean-up" operation, the release of resources

      2. For example, the files automatically shut down, thread lock automatically acquire and release.

  3, examples of the processing file operations with

Open with ( ' / etc / passwd ' ) AS f:
     for Line in f:
         Print (Line)
   # action this code: open a file, if all goes well, the file object assigned to f, then traverse the file iterator in each row, and when complete, close the file; 

  # but anywhere in this code, if an exception occurs, the file will be closed.

 

 

Guess you like

Origin www.cnblogs.com/jiaxinzhu/p/12459825.html