Python 3 has been around for a while, and quite a few developers, especially those just starting out in Python, are already using this version of the language. While many of the new features are widely used, it looks like a few are left behind. In this article, I'll cover three of the least known but useful features. I know about them from other languages ββand they make Python 3 cool.
This article is a translation of 3 Neglected Features in Python 3 That Everyone Should Be Using .
Enumerations
I have used enums a lot in Java and Swift. I continue to use them now in Python.
Declaring an enumeration in Python is very easy to do, and it was possible before version 3 (albeit with limitations):
from enum import Enum
class State(Enum):
AIR = 0
LAND = 1
SEA = 2
myState = State.AIR
# 0
print(myState.value)
# AIR
print(myState.name)
In the code above, the enumeration is introduced by declaring a class that inherits from
Enum
. And then all the necessary states are simply described. In my case: AIR
, LAND
and SEA
.
A functionality that was added in Python 3 is the ability to use
.value
and .name
. They allow you to get the number and string corresponding to the enumeration.
For example, the value output
State.LAND.name
will be LAND
.
Enums are useful in code when you want some textual identifiers for constants. For example, instead of comparing a state with 0 or 1, it is much more revealing to compare with
State.MOVING
or State.STATIONARY
. Constants can change and if someone looks at the code later, then the wordMOVING
will give much more understanding than 0. As a result, the readability of the code is greatly increased.
More information can be found in the official Python 3 documentation on Enum .
Formatting
Added in version 3.6, it
fstrings
is a powerful text formatting tool. They allow for much more readable and error-free code (which I enjoy after switching from Java). This is better than the format
one used previously in Python. Here's a usage example format
:
name = ''
blog_title = 'codeatcpp.com'
# , codeatcpp.com.
a = ", {} {}.".format(name, blog_title)
It is easy to notice the empty curly braces inside the string and after the list with the names of the variables in a specific order.
Now let's look at the same code, but using it
fstring
is more readable and very similar to the Swift way of formatting.
name = ''
blog_title = 'codeatcpp.com'
# , codeatcpp.com.
a = f", {name} {blog_title}."
To get such a neat string, you just need to put a letter
f
in front of the quotes, and then instead of empty brackets, you can immediately write the names of variables or data right in the string. Since variables are written directly in a line, there is no need to count the number of elements and keep track of the order in which the variables are placed at the end. They are just right where their values ββare needed.
Using
fstring
it gives more readable and easier to maintain code than using the classical approaches.
Data classes
Data classes can be a more confusing topic than the previous ones, so it will require a little more explanation. Data classes are something I really liked about Kotlin, so I love using them in Python as well.
A data class is a class whose sole purpose is to store data. The class contains variables that can be read and written, but has no additional logic.
Imagine you have a program where you need to pass a string and an array of numbers between different classes. You can have methods like
pass(str, arr)
, but it's much more convenient to make a class that contains a string and an array as the only members of the class.
Using a data class better shows what you are trying to do and also makes it easier to create unit tests.
The example below shows a simple data class that is a 3D vector, but it can be easily extended to represent any combination of different data:
from dataclasses import dataclass
#
@dataclass
class Vector3D:
x: int
y: int
z: int
#
u = Vector3D(1,1,-1)
# : Vector3D(x=1, y=1, z=-1)
print(u)
It is easy to see here that the definition of a data class is very similar to the definition of a regular class, except that a decorator is used
@dataclass
and then each field is defined as :
.
Although the functionality of the created one is
Vector3D
very limited, the point of using the data class is to increase efficiency and reduce the number of errors in the code. It is much better to pass it as a parameter Vector3D
than a set of type variables int
.
More information about the decorator
@dataclass
can be found in the official Python 3 documentation .
Conclusion
Let me know in the comments how you like these possibilities. It will be interesting to hear about new scenarios for their use. Happy coding!