Type hints

Type hints are an amazing feature once a codebase grows in size. In early development, not being locked into types and being able to write code more freely allows for faster experimentation. Once a codebase grows it might become confusing which types you can come across. Itโ€™s only when you run into those concrete cases that it starts becoming valuable to add the type hints.

What is the if __name__ == '__main__'-construct?

It allows you to execute code when the file runs as a script, but not when itโ€™s imported as a module.

Dependency Injection in Python

Dependency Injection is a Design Pattern to achieve loose coupling which simplifies testing. Example from ๐ŸŒ My Website Generator:

Instead of having a global configuration that can be accessed from anywhere, explicitly pass the configuration to each class or function that needs it.

Without Dependency Injection:

import config
 
class SourceFile:
    def write(self):
        destination_path = f'{config.WEBSITE_DESTINATION_FOLDER}/{self.get_relative_destination_path()}'

With Dependency Injection:

class SourceFile:
    def __init__(self, config):
        self.config = config
 
    def write(self):
        destination_path = f'{self.config.WEBSITE_DESTINATION_FOLDER}/{self.get_relative_destination_path()}'

Resources:

Personal projects using Python