In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what are the two traps of Python programming?", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what are the two traps of Python programming?"
Immutable object trap
Idea: generate 1 million random strings, merge strings using non-object or mutable objects, respectively, and compare the time spent.
The code that generates a random string:
Import random import time
Big = [chr (I) for i in range (65mem91)] small = [chr (I) for i in range (97123)]
Def randNStr (n):...: result = []...: for i in range (n):...: strlen = random.randint (3Magne9)...: result.append ('.join (random.sample (small+big,strlen)...: return result
Use the immutable object method: combine+s is then assigned to combine, this statement: because combine, s are immutable objects, the result is a new object is generated, objects continue to pile up.
Def immfn (n):...: combine=''...: res = randNStr (n): beg = time.time (): print ('beg time% s'%str (beg))...: for s in res:...: combine= combine + s...: # print (combine). ..: end = time.time ().: print ('end time% s'%str (end)).: print (' elispe time:% s'%str (end-beg))
In order to avoid repeatedly generating new objects, using mutable objects, the objects that Python solves are mutable and immutable. It is mentioned that generally iterated objects are mutable.
Def mutfn (n):...: combine=''...: res = randNStr (n): beg = time.time (): print ('beg time% s'%str (beg))...: combine='' .join (res).: # print (' print result:%s'%combine)..: End = time.time ().: print ('end time% s'%str (end)).: print (' elispe time:% s'%str (end-beg))
Str.join (iter) is used here to accept iterable objects and return only one new object. Compare the calculation time of the above two methods, from 100000, step 100000, termination to 1 million, and compare and analyze the time of merging strings.
We can see that the method in which variable objects generate only one new object has more obvious advantages than immutable objects in the case of larger amount of data, showing an expanded trumpet. When the data is 900000, the time cost savings are as high as (0.025) / 0.025 = 6.6times. When the amount of data increases to 10 million or more, the advantage is more obvious.
Mutable object trap
BAT interview question 26: a Python blank question
The title is as follows:
Def f (xPowerl = []):...: for i in range (x):...: l.append (iTuni)...: print (l)
F (2) f (3)
The result is:
In [2]: F (2) [0,1]
In [3]: F (3) [0,1,0,1,4]
For the reason, the variable object is used as the function parameter and is set to the default value, and when the function is called again, the original value of the function parameter will be indexed again.
To avoid this problem, the default value of a mutable object is generally constant: None
Def f (XMagneNone):...: if l is None:...: l = [].: for i in range (x):...: l.append (iTuni)...: print (l)...:
When called continuously in this way, the output will be as expected.
The above is the content of this article on "what are the two traps of Python programming". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow 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: 215
*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.