Several interesting and worth learning python codes, share with everyone to learn together

First select a few python codes that I think are worth pondering, I hope it will help you who are learning python.

1. The class has two methods, one is __new__, the other is __init__ , what is the difference, which one will be executed first?

 
 

The results are as follows:

Let's look at another example

The results are as follows:

 
 

Here is the official explanation:

The function of __init__ is to initialize the class instance. The first parameter is self, which represents the object itself and can have no return value. __new__ returns an instance of a new class. The first parameter is cls, which represents the class itself, and must have a return value.

Obviously, the class can be instantiated before the object can be produced. Obviously, __new__ is executed first, and then __init__. In fact, as long as __new__ returns an instance of the class itself, it will automatically call __init__ for initialization.

But there is an exception, if __new__ returns an instance of another class, it will not call the current class's __init__.

Below we output the types of object a and object b respectively:

 
 

It can be seen that a is an object of test class, and b is an object of object.

2. The object returned by the map function

The first parameter of the map() function is fun, the second parameter is generally a list, and the third parameter can be written with a list or not. The function is to sequentially call the function fun for each element of the list in the list.

 
 

Did you find that when the element in b was output for the second time, it was found to be empty. The reason is that the map() function returns an iterator and uses yield on the returned result to save memory.
for example:

If yield is not used here, when the elements in the list are very large, they will all be loaded into memory, which is a waste of memory and reduces efficiency.

3. Is compile unnecessary in regular expressions?

For example, there is a demand now. For the text <div class="nam">China</div>, use regular matching to find "China" in the tag, where the class name of the class is uncertain. There are two methods, the code is as follows:

Why use compile to write two more lines of code here? The reason is that compile compiles the regular expression into an object, speeding it up, and reusing it.

4. [[1,2],[3,4],[5,6]] One line of code to expand the list, get [1,2,3,4,5,6]

 
 

5. One line of code inserts the string "->" into the middle of each character in "abcdefg"

 
 

It is also recommended to use os.path.join() to join the file paths of the operating system.

6. zip function

The zip() function takes one or more sequences (iterables) as arguments and returns a list of tuples. Simultaneously pair the side-by-side elements of these sequences. The zip() parameter can accept any type of sequence, and can also have more than two parameters; when the lengths of the incoming parameters are different, zip can automatically intercept the shortest sequence length to obtain a tuple.

 
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324357405&siteId=291194637