In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what are the skills for the use of Python", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the skills for the use of Python" this article.
1. Organize string input
The problem of sorting out user input is extremely common in the programming process. In general, converting characters to lowercase or uppercase is sufficient, and sometimes you can use the regular expression module "Regex" to do this. But if the problem is complex, there may be a better way to solve it:
User_input = "This
String has some whitespaces...
"
Character_map = {
Ord ():
Ord ():
Ord (): None
}
User_input.translate (character_map) # This string has some whitespaces...
In this example, you can see that the spaces "n" and "t" have been replaced with a single space, and "r" has been deleted. This is just a simple example, and we can go one step further and use the "unicodedata" package to generate a large remap table and use the "combining ()" in it for generation and mapping.
2. Control what can / cannot be imported
Some languages have a very obvious mechanism for exporting members (variables, methods, interfaces). For example, in Golang, only members that begin with uppercase letters are exported. However, in Python, all members are exported (unless we use "_ _ all__"):
Def foo ():
Pass
Def bar ():
Pass
_ _ all__ = ["bar"]
In the above code, we know that only the "bar" function has been exported. Similarly, we can leave "_ _ all__" empty so that nothing is exported, resulting in a "AttributeError" when imported from this module.
3. Save memory with "_ _ slots__"
If you have ever written a program that creates a large number of instances of some kind, you may have noticed that your program suddenly needs a lot of memory. That's because Python uses dictionaries to represent the properties of class instances, which makes it fast, but not very efficient in memory use. Usually, this is not a serious problem. However, if your program is seriously affected by this, try "_ _ slots__":
Class Person:
_ _ slots__ = ["first_name", "last_name", "phone"]
Def _ _ init__ (self, first_name, last_name, phone):
Self.first_name = first_name
Self.last_name = last_name
Self.phone = phone
When we define the "_ _ slots__" attribute, Python does not use a dictionary to represent the attribute, but uses a small fixed-size array, which greatly reduces the memory required for each instance. Using "_ _ slots__" also has some disadvantages: we cannot declare any new properties, we can only use existing properties on "_ _ slots__". Also, classes with "_ _ slots__" cannot use multiple inheritance.
4. Iterator slice (Slice)
If you slice the iterator, a "TypeError" is returned, indicating that the generator object does not have a subscript, but we can solve this problem with a simple solution:
Import itertools
S = itertools.islice (range (50), 10, 20) #
For val in s:
...
We can use "itertools.islice" to create a "islice" object, which is an iterator that produces the items we want. Note, however, that this operation uses all generator entries before slicing, as well as all items in the "islice" object.
5. A simple way to realize the comparison operator
It is tedious to implement all the comparison operators (such as _ _ lt__, _ _ le__, _ _ gt__, _ _ ge__) for a class. Is there an easier way to do this? At times like this, "functools.total_ordering" is a good helper:
From functools import total_ordering
@ total_ordering
Class Number:
Def _ _ init__ (self, value):
Self.value = value
Def _ _ lt__ (self, other):
Return self.value
< other.value def __eq__(self, other): return self.value == other.value print(Number(20) >Number (3))
Print (Number (1)
< Number(5)) print(Number(15) >= Number (15))
Print (Number (10)
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.