I heard that this is a question that 70% of people who learn Python will get it wrong, see if you have mastered it

rookie monologue

 

 

Although the Python language is beautiful, concise and powerful, it also has many pits, which will fall into it if you are not careful. I also encountered it when I was learning Python. I will summarize it today, and I hope it can be beneficial to everyone!

1. Pitfall 1: Mutable default parameters

Variable parameters in Python are very flexible, but there are also pitfalls. If you don't believe me, take a look at this small example:

 

 

We want this function to be called every time, because the second parameter is the default parameter. If the second parameter is not provided, a new list will be created, and then the list will be returned. Guess what the result should be:

Should it be [1][2][3].

But actually the result is:

[1, 2, 3]

[1, 2, 3]

[1, 2, 3]

That is to say, the variable addresses l1 and l2 are polluted by l3. Why:

When the function is called for the first time, the default res list is created, and the address space for this list is created! The point is, Python's default parameters are created once, not every time the function is called. That is to say, changes to this list in the following code will also modify the previous content!

If you don't believe me, you can print the address of res to see:

 

 

Are the addresses the same.

Correct practice: It is a good practice to create a new object by assigning the default parameter to None each time the function is called.

 

 

Advanced and advanced: the use of crawler recursion

Some students said that this is indeed a trap, but it doesn't seem to have any practical use. Let's look at an example of a crawler. A few weeks ago, I just engaged in a crawler combat activity in the small dense circle, and one of them needs to be crawled in a loop. Scenario of recursive call!

I parse the pages of a web page and get the results of the parsing. If the current number of pages exceeds the maximum number of pages, stop, otherwise continue to recursively parse the next page. I recommend it to everyone to learn and communicate, 719+139+688, like Python's high together.

Here res=None is used, and recursion is quite useful.

2. Pitfall 2: Lazy Binding in Closures

If the above trap 1 is easier to understand, then the following trap is even more confusing! Most people will fall into it, this is how Python binds variables in closure scope~~

 

 

We have a function called squares (a function used to create squares), which has 3 anonymous functions, each of which returns the square of i. It seems to be quite simple, just return the square

Then we use a for loop to get each square function, and then input a 2 to the square. That is to say, x is 2. The result we expect is: 0,1,2

0*2=0

1*2=2

2*2=4

But the actual result is:

4

4

4

Why is this happening? Closures are a relatively difficult concept to understand in Python. They are often combined with function decorators, plus an anonymous function, which makes them dizzy! Let's change the way of ordinary functions to understand:

 

 

Python's closure is delayed binding , which means that the value of the variable used in the closure is queried when the internal function is called.

The res contains the closure function make_square . This closure is quite powerful. It will extend to the outside of the scope of the function, that is, it contains the binding to the variable i, and the value of i in the last query is 2.

Correct way:

 

 

i Because the closure delay binding will cause the above problems, the solution is to bind it immediately, and it will be done.

There are many interesting and interesting scenery on the way of learning Python, and there are many pits . You are welcome to discuss together. If you have any questions, you are also welcome to come!

The article is sourced from the Internet and organized by yourself. If there is any infringement, please contact to delete it within 24 hours.

Guess you like

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