In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what are the five pits about Python. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Today, I'd like to discuss some common potholes in Python.
1 tuples containing a single element
Some functions in Python have tuples with 1 element in them, so it is wrong to create them:
C = (5) # NO!
It actually creates an integer element 5, which must be followed by a comma:
C = (5,) # YES!
2 default parameter is set to empty
A function with default parameters, if the type is container and set to empty:
Def f (a def b = []): # NO!
Print (b)
Return b
Ret = f (1)
Ret.append (1)
Ret.append (2)
# when f (1) is called again, it is expected to print as []
F (1)
# but it is [1pc2]
This is a pit of variable type default parameters. Be sure to set this default parameter to None:
Def f (a recorder none): # YES!
Pass
3 unbound potholes of shared variables
Sometimes you want multiple functions to share a global variable, but try to change it to a local variable within a function:
I = 1
Def f ():
ITunes 1 # NO!
Def g ():
Print (I)
You should display the declaration I as the global variable in the f function:
I = 1
Def f ():
Global i # YES!
ITunes 1
4 the pit of quick copy of list
In python, * and list operations are used to achieve fast element replication:
A = [1pyrm 3rem 5] * 3 # [1pyrm 3je 5jr 5jr 1pr 3jre 5pr 1jue 3pr 5]
A [0] = 10 [10, 2, 3, 1, 2, 3, 1, 2, 3]
If the list element is a compound type such as a list or dictionary:
A = [[1mage3, 5], [2, 4]] * 3 # [[1, 3, 5], [2, 4], [1, 3, 5], [2, 4]]
A [0] [0] = 10 #
The result may surprise you, other a [1 [0], etc., have also been modified to 10.
[[10, 3, 5], [2, 4], [10, 3, 5], [2, 4], [10, 3, 5], [2, 4]]
This is because * copied compound objects are shallow references, which means that id (a [0]) is equal to id (a [2]) house number. If you want to achieve a deep copy effect, do this:
A = [[] for _ in range (3)]
5 the pit of list deletion
Delete an element from a list that may be repeated multiple times in the list:
Def del_item (lst,e):
Return [lst.remove (I) for i in e if iTunes] # NO!
Consider deleting element 3 in this sequence [1pc3rec 3je 3je 5], and it turns out that only two of them are deleted:
Del_item ([1pyrm 3rem 3jr 3je 5], 3) # results: [1pr 3je 5]
The right thing to do:
Def del_item (lst,e):
D = dict (zip (range (len (lst)), lst)) # YES! Construct a dictionary
Return [v for kpene v in d.items () if vandalism] about what the five pits of Python are, so much for sharing here. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.