In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge about "how to write Python code". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. Class has two methods, one is new, the other is init, what is the difference, which will be executed first? class test(object): def __init__(self): print("test -> __init__") def __new__(cls): print("test ->__new__") return super().__ new__(cls)a = test()
The results are as follows:
test ->__new__test -> __init__
Here's another example:
class test2(object): def __init__(self): print("test2 -> __init__") def __new__(cls): print("test2 ->__new__") return object() b = test2()
The results are as follows:
test2 ->__new__
Here is the official explanation: init is used to initialize the class instance. The first parameter is self, which represents the object itself. There can be no return value. new returns an instance of a new class. The first argument is cls, which represents the class itself and must have a return value. Obviously, the class instantiates before it can produce objects, obviously new executes first, then init, in fact, whenever new returns an instance of the class itself, it automatically calls init to initialize. There are exceptions, however, where new does not call init of the current class if it returns an instance of another class.
Below we output the types of object a and object b respectively:
print( type(a))# print( type(b))#
As you can see, a is an object of class test, and b is an object of class object.
2. Objects returned by map function
The first parameter of map () function is fun, the second parameter is usually list, and the third parameter can be written list or not. The function is to call fun on each element of list in sequence.
>>> b=map(lambda x:x*x,[1,2,3])>>> [i for i in b][1, 4, 9]>>> [i for i in b][]>>>
Did you find that the second time you output the element in b, you found that it became empty. The reason is that the map() function returns an iterator and uses yield on the return result to save memory.
For example:
#encoding:UTF-8 def yield_test(n): for i in range(n): yield call(i) Do something else def call(i): return i*2 #use for loop x = yield_test(5)print([i for i in x])print([i for i in x])
The implementation results are:
[0, 2, 4, 6, 8] []
If you don't use yield here, then when the elements in the list are very large, they will all be loaded into memory, which is a waste of memory and will reduce efficiency.
3. Is compile unnecessary in regular expressions?
For example, there's a requirement for text.
China, use regular matching to find "China" in the label, where the class name of the class is uncertain.
There are two ways, the code is as follows:
>>> import re>>> text = 'China'>>> #Method 1...>>> re.findall('(.*) ',text)[' China']>>> #Method II...>>> regex='(.*) '>>> pattern = re.compile(regex)>>> re.findall(pattern,text)['China']>>>
Why write two extra lines of code with compile? The reason is that compile compiles regular expressions into an object, speeds them up, and reuses them.
4.[[1,2],[3,4],[5,6]] A line of code expands the list to [1,2,3,4,5,6]>> [j for i in [1,2],[3,4],[5,6]] for j in i][1, 2, 3, 4, 5, 6]>>5. A line of code inserts the string "->" in the middle of each character in "abcdefg">> "->".join("abcdefg")'a->b->c->d->e->f'>>>>
It is also recommended to use os.path.join() to splice the file paths of the operating system.
6. Zip function
The zip() function takes one or more sequences (iterable objects) as arguments and returns a list of tuples. And pair the elements that are side-by-side in these sequences. The zip() parameter can accept any type of sequence, and can also have more than two parameters; when the length of the passed parameters is different, zip can automatically truncate the shortest sequence length to obtain a tuple.
>>> a=[1,2]>>> b=(3,4)>>> zip(a,b)>>> for i in zip(a,b):... print(i)... (1, 3"How to write Python code" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.