day16 stage summary

day16 stage summary

Course goal: summarize and test the knowledge points of the second module "function and module" stage, so that students can better master the relevant knowledge of this module.

Course overview:

  • Knowledge supplement
  • Stage summary (mind map)
  • Examination questions

1. Knowledge supplement

1.1 nolocal keyword

In the previous course, we learned the global keyword.

name = 'root'


def outer():
    name = "Xiao Ming"

    def inner():
        global name
        name = 123

    inner()
    print(name)


outer()
print(name)

In fact, there is also a nolocal keyword, which is rarely used. It can be understood here. (variables pointing to the parent scope)

name = 'root'


def outer():
    name = "Xiao Ming"

    def inner():
        nonlocal name
        name = 123

    inner()
    print(name)


outer()
print(name)
name = 'root'


def outer():
    name = 'alex'

    def func():
        name = "Xiao Ming"

        def inner():
            nonlocal name
            name = 123

        inner()
        print(name)

    func()
    print(name)


outer()
print(name)

name = 'root'


def outer():
    name = 'alex'

    def func():
        nonlocal name
        name = "Xiao Ming"

        def inner():
            nonlocal name
            name = 123

        inner()
        print(name)

    func()
    print(name)


outer()
print(name)

1.2 yield from

In the generator section, we learned about the yield keyword, which is used in Python 3 After 3, a yield from was introduced.

def foo():
    yield 2
    yield 2
    yield 2


def func():
    yield 1
    yield 1
    yield 1
    yield from foo()
    yield 1
    yield 1


for item in func():
    print(item)

1.3 deep and shallow copy

Deep and shallow copy generally refers to variable data types: set, list, dict (copy of immutable data types is meaningless, and memory will not be copied)

  • Shallow copy

    • Immutable type, no copy.

      import copy
      
      v1 = "Xiao Ming"
      print(id(v1)) # 140652260947312
      
      v2 = copy.copy(v1) 
      print(id(v2)) # 140652260947312
      

      It is reasonable to say that after copying v1, the memory address of v2 should be different, but due to the internal optimization mechanism of python, the memory address is the same, because for immutable types, if the value is modified later, a new copy of data will be created and the original data will not be affected. Therefore, it is OK not to copy.

    • Variable type, copy only the first layer. (only one layer of list is copied, and the values in it are not copied)

      import copy
      
      v1 = ["Xiao Ming", "root", [44, 55]]
      print(id(v1))  # 140405837216896
      print(id(v1[2]))  # 140405837214592
      
      v2 = copy.copy(v1)
      print(id(v2))  # 140405837214784
      print(id(v2[2]))  # 140405837214592
      

  • Deep copy

    • Immutable type, no copy

      import copy
      
      v1 = "Xiao Ming"
      print(id(v1))  # 140188538697072
      
      v2 = copy.deepcopy(v1)
      print(id(v2))  # 140188538697072
      

      Special tuples:

      • There is no mutable type in tuple element, so it is not copied

        import copy
        
        v1 = ("Xiao Ming", "root")
        print(id(v1))  # 140243298961984
        
        v2 = copy.deepcopy(v1)
        print(id(v2))  # 140243298961984
        
      • There are variable types in the element. Find all [variable types] or [tuples containing variable types] and copy them

        import copy
        
        v1 = ("Xiao Ming", "root", [11, [44, 55], (11, 22), (11, [], 22), 33])
        v2 = copy.deepcopy(v1)
        
        print(id(v1))  # 140391475456384
        print(id(v2))  # 140391475456640
        
        print(id(v1[2]))  # 140352552779008
        print(id(v2[2]))  # 140352552920448
        
        print(id(v1[2][1]))  # 140642999940480
        print(id(v2[2][1]))  # 140643000088832
        
        print(id(v1[2][2]))  # 140467039914560
        print(id(v2[2][2]))  # 140467039914560
        
        print(id(v1[2][3]))  # 140675479841152
        print(id(v2[2][3]))  # 140675480454784
        
    • Variable type: find a copy of variable type or tuple containing variable type at all levels

      import copy
      
      v1 = ["Xiao Ming", "root", [11, [44, 55], (11, 22), (11, [], 22), 33]]
      v2 = copy.deepcopy(v1)
      
      print(id(v1))  # 140391475456384
      print(id(v2))  # 140391475456640
      
      print(id(v1[2]))  # 140352552779008
      print(id(v2[2]))  # 140352552920448
      
      print(id(v1[2][1]))  # 140642999940480
      print(id(v2[2][1]))  # 140643000088832
      
      print(id(v1[2][2]))  # 140467039914560
      print(id(v2[2][2]))  # 140467039914560
      
      print(id(v1[2][3]))  # 140675479841152
      print(id(v2[2][3]))  # 140675480454784
      
      import copy
      
      v1 = ["Xiao Ming", "root", [44, 55]]
      v2 = copy.deepcopy(v1)
      
      print(id(v1))  # 140405837216896
      print(id(v2))  # 140405837214784
      
      
      print(id(v1[2]))  # 140563140392256
      print(id(v2[2]))  # 140563140535744
      

    Shallow copy, which copies only the variable data types of the first layer.

    Deep copy copies the variable types of all layers. If there are variable types in tuples, they will also be copied.

    Special: tuples, immutable data types, if encountered

    • Shallow copy: never copy
    • Deep copy: if the data types in tuples are immutable data types, they will never be copied; If there are variable types in a tuple, the tuple will be copied.

2. Stage summary

Keywords: Python

Added by moleculo on Thu, 20 Jan 2022 15:17:57 +0200