Python advanced knowledge points

“A quitter never wins and a winner never quits” — Napoleon Hill

3.1 Garbage Collection##

Garbage collection is the process of finding data objects that will not be accessible in the future in a running program, and reclaiming the resources (especially memory) used by those objects. Languages for automatic garbage collection--Java, C#, Python and most scripting languages. C is a language without garbage collection-programmers need to know when to allocate and reclaim memory.

3.2 Deep copy and shallow copy##

Refer to the previous article: [Learning Python for a year, this time I finally understand shallow copy and deep copy] (https://links.jianshu.com/go?to=https%3A%2F%2Fyuzhoustayhungry.github.io%2Fpost%2F%25E6%25B7%25B1%25E6%258B%25B7%25E8%25B4%259D%25E5%2592%258C%25E6%25B5%2585%25E6%258B%25B7%25E8%25B4%259D%2F)

Q: What is the difference between shallow copy and deep copy?

answer:

3.3 Iterators and generators##

Q: What is the difference between an iterator and a generator?

**Iterator: ** An object that implements the __iter__() and __next__() methods. The first method returns the iterator object itself and is used in for and in statements. The first method returns the next value in the iteration. If there are no more elements, a StopIteration exception will be raised.

**Generator: ** An easy way to create iterators, use the keyword yield. Generators use the function call stack to implicitly store the state of the iterator-compared to writing the same iterator as an explicit class, it simplifies the writing of iterators. It also helps improve readability.

Every generator is an iterator, but the reverse is incorrect. In particular, iterators can be fully mature classes and can therefore provide other functions. For example, it is easy to add a method to the iterator class above to change the iteration limit, which is impossible for generators.

3.4 Decorator@decorator

@ Symbols are syntactic sugar for decorators. They are used when defining functions to avoid assignment operations again.

import time

def time_function(f):
 def wrapper(*args,**kwargs):
  begin = time.time()
  result =f(*args,**kwargs)
  end = time.time()print("Function call with argument {all_args} took ".format(
   all_args="\t".join((str(args),str(kwargs))))+str(end - begin)+" seconds to execute.")return result

 return wrapper

3.5 List vs. tuple

Lists[1, 4, 7, "apple", 4], Tuple(3.14, "PI", 2,43, "e")

**The same point: ** are containers, and they can store different types of data, and can be indexed for access a[i]

**Difference: ** The tuple is immutable, you cannot change the value of the index a[i], nor can you add/remove elements from the tuple; but the list can.

Benefits of immutability: performance improvement, container friendliness, thread safety. The ancestor can be placed in the set set and used as a key value, but a list cannot. The creation and access speed of tuples is slightly faster, and the memory footprint is small.

3.6 * args and *kwargs

Both are used to pass variable parameters in functions. *arg is used to pass variable-length parameter lists:

The second parameter **kwargs is used when passing a variable number of keyword arguments to the function.

3.7 Error handling, exception mechanism##

3.8 lambda

increment_by_i =[lambda x: x + i for i inrange(10)]print(increment_by_i[3](4))

The program will print 13 (= 9 + 4) instead of the expected 7 (= 3 + 4). This is because the functions created in the loop have the same scope. They use the same variable name, so they all refer to the same variable i, which is 10 at the end of the loop, so it is 13 (= 9 + 4).

There are many ways to get the desired behavior. The reasonable way is to return the lambda from the function to avoid naming conflicts.

3.9 Function parameters##

What are the positional parameters, keyword parameters and default parameters of a function.

Recommended Posts

Python advanced knowledge points
Python knowledge points
Python3 supplementary knowledge points
2020--Python grammar common knowledge points
Python advanced (1)
Python crawler basic knowledge points finishing
Summary of knowledge points about Python unpacking
Knowledge points of shell execution in python
Python advanced usage summary
Basic knowledge of Python (1)
Noteworthy update points in Python 3.9
​Full analysis of Python module knowledge