Python features and features that you need to know and are useful to you

Source: Internet
Author: User
Tags unique id

After years of using Python, I stumbled upon features and features that we didn't know in the past. Some can be said to be very useful, but they are not fully utilized. With that in mind, I've edited some of the features of Python that you should know about.

Functions with any number of parameters

You may already know that Python allows you to define optional parameters. But there is also a way to define any number of arguments to the function.

First, look at the following is an example that defines only optional parameters

Now, let's see how to define a function that can accept arbitrary arguments. We use tuples to achieve.

Find files using Glob ()

Most python functions have long, descriptive names. But a function named you may not know what it is unless you are familiar with it from somewhere else. It's like a more powerful version of a function. It allows you to search for files by using pattern matching.

You can find multiple file types as follows:

If you want to get the absolute path to each file, you can call the function on the return value:

Debugging

The following example uses a module. This module is useful for debugging purposes and is much more powerful than what 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've seen a lot of people use the MD5 () function to do this, but it's really not for this purpose. In fact, there is a Python function named UUID () for this purpose.

You might notice that even though the strings are unique, the few characters behind them look similar. This is because the generated string is associated with the MAC address of the computer.

To reduce the duplication, you can use these two functions.

Serialization of

Have you ever needed to store a complex variable in a database or text file? You don't have to think of a fancy way to convert an array or an object into a typed string, because Python already provides this functionality.

This is a native Python serialization method. In recent years, however, JSON has become popular, 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 characters

When we talk about compression, we usually think of files, such as the ZIP structure. Long characters can be compressed in Python, and no files are involved.

Registering the Shutdown function

There is a module called, which allows you to execute some code immediately after the script has finished running.

If you want to measure some benchmark data at the end of the script execution, such as how long it takes to run:

Drilling looks very simple. Just add the code to the bottom of the script and it will run before the end of the script. However, if there is a fatal error in the script or the script is terminated by the user, it may not run.

When you use Atexit.register (), your code executes regardless of the reason the script stops running.

Conclusion

Are you aware that the Python features are not widely known to be useful? Please share it with us at the comment office. Thank you for reading!

Python features and features that you need to know and are useful to you

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.