In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of fuzzy semantics in Python syntax. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!
1. Slices do not perform out-of-bounds checks and report errors
What will be the output of the following code?
list = ['a', 'b', 'c', 'd', 'e']print list[10:]
The following code will output an empty list [] without generating an IndexError error. As expected, try to get members of a list with an index that exceeds the number of members.
For example, trying to get members of list[10] and beyond results in IndexError .
However, trying to get a slice of a list that starts with index exceeding the number of members does not generate an IndexError, but simply returns an empty list.
This becomes a particularly nasty bug because it runs without errors, making it hard to track bugs.
2. 1ist = [[ ]] * 5list # output? list[0].append(10)list # output? list[1].append(20)list # output? list.append (30)list # output?
What will lines 2, 4, 6 and 8 output? Try explaining.
The output is as follows:
[[],[],[],[],[]][[10],[10],[10],[10],[10]][[10,20],[10,20],[10,20]][[10,20],[10,20],[10,20],[10,20],[10,20],30]
The output of the first line is intuitively easy to understand, e.g. list = [ [ ] ] * 5 simply creates five empty lists. However, the key to understanding the expression list=[ [ ] ] * 5 is that it does not create a list of five separate lists, but rather it creates a list of five references to the same list. Only by understanding this can we better understand the output results that follow.
3. Delayed binding of closures
What will be the output of the following code? Please explain.
You have a problem with your studies and no one answers it? Xiaobian created a Python learning exchange group: 531509025 looking for like-minded friends, mutual help, there are good video learning tutorials and PDF e-books in the group! '''def multipliers(): return [lambda x : i*x for i in range(4)]print [m(2) for m in multipliers()]
How do you modify the definition of multipliers above to produce the desired result?
The above code outputs [6, 6, 6, 6] instead of [0, 2, 4, 6] as we would like.
The reason for the above problem is Python's delayed binding of closures. This means that when the inner function is called, the value of the parameter is looked up inside the closure. Therefore, when any function returned by multipliers() is called, the value of i will look up in the vicinity. At that point, regardless of whether the returned function is called or not, the for loop has completed and i has been assigned the final value of 3.
Therefore, each time the returned function multiplies the value 3 passed in, since the value 2 passed in the previous code, they all end up returning 6(3*2). As it happens, The Hitchhiker's Guide to Python also points out that there is also a widely misunderstood point of knowledge associated with lambdas functions, but not in this case. There is nothing special about the function created by lambda expressions; it is essentially the same as the function created by def.
Here are some ways to solve this problem.
One solution is to use Python generators.
def multipliers(): for i in range(4): yield lambda x : i * x
Another solution is to create a closure that binds immediately using default functions.
def multipliers(): return [lambda x, i=i : i * x for i in range(4)]
An alternative is to use partial functions:
from functools import partialfrom operator import muldef multipliers(): return [partial(mul, i) for i in range(4)] The above is "Example analysis of fuzzy semantics in Python syntax". Thank you for reading all the contents of this article! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.