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, we'll cover three of the least known but useful features. We know about them from other programming languages and they make Python 3 a cool language.
3 Important Python 3 Features Rarely Used.
We've used enums mostly in Java and Swift. Now we continue to use them in Python.
Declaring an enumeration in Python is very easy to do, and it was possible before the third version (albeit with restrictions):
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 our case these are 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 output for State.LAND.name would be LAND.
Enums are useful in code when you want some textual identifiers for constants. For example, instead of comparing state to 0 or 1, it is much more revealing to compare it to State.MOVING or State.STATIONARY. Constants can change and if someone looks at the code later, the word MOVING will give much more understanding than 0. As a result, the readability of the code is greatly increased.
Added in version 3.6, fstrings is a powerful text formatting tool. They allow for much more readable and error-free code (which we enjoy after switching from Java). This is better than the format used previously in Python. Here's an example of using the format:
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 fstring, which is more readable and very similar to the Swift way of formatting.
To get such a neat string, you just need to put the letter f in front of the quotes, and then instead of empty brackets, you can immediately write the names of variables or data directly 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 results in a more readable and easier to maintain code than with the classical approaches.
Data classes can be a more confusing topic than the previous ones, so it will require a little more explanation. Data classes are something we really liked about Kotlin, so we 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 the 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:
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 the @dataclass decorator is used and then each field is defined as name: type.
Although the functionality of the created Vector3D is very limited, the point of using the data class is to improve efficiency and reduce the number of errors in the code. After all, it is much better to pass a Vector3D as a parameter than a set of int variables.
More information about the @dataclass decorator can be found in the official Python 3 documentation.
Let us know in the Contact Us Form if you have tried these new features. It will be interesting to hear about new scenarios for their use.