In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the ultra-practical functions and features of Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the ultra-practical functions and features of Python?"
First, take a look at an example that defines only optional parameters
Now, let's look at how to define a function that accepts arbitrary arguments. We use tuples to do this.
Use Glob () to find the file
Most Python functions have long and descriptive names. But you may not know what a function named glob () does unless you are already familiar with it from somewhere else.
It looks like a more powerful version of the listdir () function. It allows you to search for files by using pattern matching.
You can find multiple file types like this:
If you want to get the absolute path to each file, you can call the realpath () function on the return value:
Debug
The following example uses the inspect module. This module is very useful for debugging purposes and has far more functions than is described here.
This article will not cover every detail of this module, but will show you some use cases.
Generate a unique ID
In some cases you need to generate a unique string. I see a lot of people using the md5 () function to do this, but it's really not for that purpose.
There is actually a Python function called uuid () that is used for this purpose.
You may notice that even though strings are unique, the characters that follow them look similar. This is because the generated string is associated with the MAC address of the computer.
To reduce repetition, you can use these two functions.
Serialization
Have you ever needed to store a complex variable in a database or text file? You don't need to think of a strange way to convert an array or object lattice into a formatted string, because Python already provides this feature.
This is a native Python serialization method. However, JSON has become popular in recent years, and Python has added support for it. Now you can use JSON to encode and decode.
This is more compact and, most importantly, compatible with JavaScript and many other languages. However, for complex objects, some of the information may be lost.
Compressed character
When talking about compression, we usually think of files, such as ZIP structures. Long characters can be compressed in Python without involving any archive files.
Register the Shutdown function
There is a module called atexit, which allows you to execute some code immediately after the script has been run.
If you want to measure some benchmark data at the end of script execution, such as how long it takes to run:
It looks simple at first glance. You just need to add the code to the bottom of the script, and it will run before the script ends. But if there is a fatal error in the script or if the script is terminated by the user, it may not run.
When you use atexit.register (), your code will be executed, no matter what reason the script stops running.
At this point, I believe you have a deeper understanding of "what are the ultra-practical functions and features of Python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 300
*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.