“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

A, sorting,

1. List provides its own sort function: sort. A) CMP is a comparison function that takes two elements and returns -1, 0, 1. B) key is a comparison function that takes an element and returns the key of that element. C) Reverse is a flag bit, indicating ascending or descending order. The default is False in ascending order and True in descending order. 3. The performance of using key and reverse is better than that of CMP functions. The time is half of the CMP function.

The use of iteration

1. Iteration is more efficient than direct list traversal. Such as the list returned by dictionary keys and iterators returned by iterkeys. 2. The reversed() built-in function will return an iterator with reversed access. The argument must be a sequence. 3. Enumerate: Returns an iterator with an index value. 4, for eachLine in myFile replace for eachLine in myfile.readlines () : 6, you can define a class, can be used iteratively. But you need to define methods: iter, next. Filter (function, iterable) : Filter can be used for iteration.

Third, the use of generators

The yield keyword blocks the execution of a function and saves the current execution environment. The entire package is called a generator. Generators can be created by calling generator functions. A generator function is a function that contains the keyword yield. The generator can be executed with.next(). On each invocation, the code is executed until the yield keyword is stopped and the value of the expression following the yield keyword is returned. You can send a message to the generator by calling the send() function. A = yield L: The input parameter of send is assigned to A. Throw: Allows clients to pass in any exceptions to throw. 6. Same as throw, but with a specific exception: GeneratorExit. 7. Send accepts only one parameter, but multiple parameters can be passed as tuples. A class method can also return a generator, because it is essentially a function. 9. How do generators get their own send and NEx functions when they are used? Passing a second pass through SEND is somewhat risky, with a high probability of cross-referencing and memory leaks due to failure to garbage collect. 10. The first time, you must call next to start the generator.

Four, the use of decoration

1. Decorators are essentially functions (or callable objects) that accept function objects. Decorators are used only to decorate or decorate the wrapper of a function, return a modified function object, assign it the original identifier, and permanently lose access to the original function. 2. What is a decorator with parameters? It’s a function that returns a decorator and takes arguments. 3. The no-argument decorator returns a function that replaces the original identifier.

def decofun(fun): def _mydeco(*args, **kwargs): print('before fun! ') ret = fun(*args, **kwargs) print('after fun', ret) return ret return _mydeco Decofun def funtest(): decofun print('now in funtest! ') return 1 funtest()Copy the code

A) @decofun2 B) @decofun c) def funtest(): d) print(‘now in funtest! ‘) e) return 1 f) The principle is that funtest is first packaged by decoFun and then packaged by Decofun2. That is, the first part of the top decorator (decofun2) is called, then the first part of the decoFun function is called, and then funtest is called. After funtest returns, the last part of decoFun is called first, followed by the last part of decofun2. It’s like a stack structure. 5, do not abuse the decoration. If a decorator is used only once, consider its existence. Def decoarg(arg): a) def decofun3(fun): b) def _mydeco(*args, **kwargs): c) print(‘decoarg before fun! ‘, arg) d) ret = fun(*args, **kwargs) e) print(‘decoarg after fun’, One of the most important techniques used by decorators is closures. What the decorator function returns is essentially a closure. 9, decorators can also decorate __methods of a class:

  def __init__(self):
        self.i = 1
Copy the code


@decoarg(1)

@decofun2

@decofun

def call(self):

    print('i is %d' % self.i)
Copy the code

Note that a decorator decorates a class method that cannot be inherited by a subclass (or that a subclass’s method is not decorated). Because it’s essentially a function. A) class obj: b) def init(self, fun): c) self. Fun = fun d) e) def call(self, args, **kwargs): f) print(‘decofun before fun! ‘, args, kwargs) g) ret = self.fun(*args, **kwargs) h) print(‘decofun after fun’, Ret) I) return ret j) @objdeco k) def funtest(a, b=2): l) print(‘funtest1 a, b= ‘, a, b) a) print(‘funtest1 a, b= ‘, a, b) a)

Decorators can decorate classes. The decorator receives a class name and returns the same class name. It can add attributes or perform operations to the class.