May 31, 2021 Article blog
The article was reproduced from the public number: Core Reading
Python 3 has been online for some time, and most developers, especially those who are programming for the first time, are already using it.
But are you sure you've done a thorough study of Python 3? I n fact, many of these new features are still unknown to most people. This article discusses three little-known but very useful features in Python 3 that I came into contact with and fell in love with in other languages, and their addition makes the Python 3 experience better.
Enumeration is a feature commonly used in Java and Swift, and I extended it to Python. Creating enumerations in Python is simple and can be used in previous versions of 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 easily construct an enumeration by constructing a class and making it a subclass of enumeration. H ere you simply 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 code above, input
State.LAND.name
return
LAND
so the functionality is more than just an integer enumeration.
Enumeration types in code are useful when descriptive constants are required. F
or example, instead of checking whether the status is 0 or 1, check whether it is
State.MOVING
or
State.STATIONARY
Constants can change, and if someone is looking at your code,
MOVING
makes more sense than 0, and the readability of the code is greatly improved.
fstring
added in
Python 3.6
is a great way to format text. T
hey are readable and error-prone.
fstring
is easier to read than the format
Python
used previously.
Here's 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, brackets are entered in the string, and the names of each variable are listed in order.
There are many tasks for the same code, but
fstring
greatly increases the readability of the code, especially if it is 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 simpler string, simply put the letter f before the quotation marks and place the variable or data directly in parentheses instead of empty brackets. Because the variable itself is written in parentheses, you do not have to calculate the number of items written in the format to determine the position of the variable, which is where it should be.
fstring
can generate more readable and reliable code than string connections or formatted strings.
The data class may be more obscure than what was said above, so I'll explain briefly.
I've come to love 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 to, but there is no additional logic on it.
Suppose you have a program that passes a string and an array of numbers between different classes. I
t is also possible to use methods such as
pass
(str, arr), preferably 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 a 3D vector, 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(1,1,-1)
# Outputs: Vector3D(x=1,y=1, z=-1)
print(u)
Here, you can see that the definition of a data class is very similar to declaring a normal class, except that we first use
@dataclass
and then the name of each field is
name:type
Although the
Vector3D
we created has limited functionality, the purpose of the data class is simply to increase efficiency and reduce errors in the code, and it is much better to pass
Vector3D
than to pass
int
variable.
These are
W3Cschool编程狮
introductions to
the three important features that Python 3 has overlooked,
and I hope this will help you.