Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the three important functions in Python3

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of what the three important functions of Python3 are, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading the three important functions of Python3. Let's take a look at it.

Enumerate

Enumeration is a commonly used feature in Java and Swift, and I extended it to Python. Creating enumerations in Python is simple and can be used in versions prior to Python 3 (although the functionality is more limited):

From enum importEnum classState (Enum): AIR=0 LAND=1 SEA=2 myState = State.AIR # Prints 0 print (myState.value) # Prints AIR print (myState.name)

In the above code, you can see that enumerations can be easily constructed by constructing a class and making it a subclass of the enumeration. Here, you only need to define each state in the following line. As far as I'm concerned, I have AIR, LAND, SEA.

The new feature of Python 3 is to run .value and .name. This gets the integer value associated with the state or the string associated with it.

In the above code, the input State.LAND.name returns LAND, so the function is more than just an integer enumeration.

Enumerated types in your code are useful when descriptive constants are needed. For example, instead of checking whether the status is 0 or 1, check to see if it is State.MOVING or State.STATIONARY. Constants may change, and if someone is looking at your code, MOVING is more meaningful than zero, and the readability of the code will be greatly improved.

Format

The addition of fstring in Python 3.6is a great way to format text. They are highly readable and are not error-prone. Fstring is easier to read than the format previously used by Python. The following is an example of using a format:

Name = Brett blog_title = Medium # Hi, my name isBrett and I am writing on my Medium blog. A = "Hi, myname is {} and I am writing on my {} blog." .format (name,blog_title)

As shown above, enter parentheses in the string and list the names of each variable in order. There are many tasks of the same code, but fstring greatly increases the readability of the code, especially similar to formatting strings with Swift.

Name = Brett blog_title = Medium # Hi, my name isBrett and I am writing on my Medium blog. A = f "Hi, myname is {name} and I am writing on my {blog_title} blog."

To complete this more concise string, simply precede the quotation marks with the letter f, and then put the variable or data directly in parentheses instead of using empty parentheses. Because the variable itself is written in parentheses, it is not necessary to calculate the number of items written in the format to determine the location of the variable, the variable is where it should be.

Fstring can generate more readable and reliable code than string concatenation or formatting strings.

Data class

The data class may be more obscure than the above, so I'll explain it briefly. I've come to like data classes in Kotlin, so I'd love to use them in Python.

A data class is actually a class whose sole purpose is to hold the data. The class will have variables that can be accessed and written, but there is no additional logic above.

Suppose you have a program that passes a string and an array of numbers between different classes. It is also possible to use an approach such as pass (str,arr). It is best to create a data class that contains only strings as fields and arrays. By creating a data class, what you do will be clearer and unit testing will be easier.

The following example shows how to create a simple data class that represents three-dimensional vectors, but this can easily be extended to represent any combination of different data:

From dataclasses import dataclass # Definedataclass @ dataclass classVector3D: X: int y: int z: int # Create a vector u = Vector3D # Outputs: Vector3D

Here, you can see that the definition of a data class is very similar to declaring a normal class, except that we use @ dataclass first, and then the name of each field is name:type.

Although the functionality of the Vector3D we created is limited, the purpose of the data class is to improve efficiency and reduce errors in the code, and passing Vector3D is much better than passing int variables.

This is the end of the article on "what are the three important functions of Python3". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the three important functions of Python3". If you want to learn more knowledge, you are welcome to 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: 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report