Instead of cluttering your code with constants, you can create an enumeration using the Enum class. An enumeration is a set of symbolic names bound to unique, constant values.
THis is my favourite! pathlib solves a pet peeve of mine. The pathlib module provides a way to interact with the file system in a much more convenient way than dealing with os.path or the glob module. The pathlib module makes everything simpler.
You can join paths by '/' operator:
data_folder = Path("my_project/data/") file_to_open = data_folder/"raw_data.txt"
Moreover, you can directly get a content of the file, for example
- Type hints
You can use type hints to indicate the type of a value in your code. For example, you can use it to annotate the arguments of a function and its return type. These hints make your code more readable, and help tools understand it better.
Instead of having to use the `.format()` method to print your strings, you can use f-strings for a much more convenient way to replace values in your strings. f-strings are much more readable, concise, and easier to maintain.
- Underscores in Numeric Literals
This one is a small, nice addition: you can use underscores in numeric literals for improved readability. This will shave off a few seconds every time you had to count how many digits a number had.