Python Clean Code Tip:
Use how and when to use decorators
š
TestDriven.io
24.3K posts
Teaching practical programming through real-world applications. Tweets by @MikeHerman and @JanGiacomelli.
- Python clean code tip: Use enums to group related constants š
- Python Clean Code Tip: Check the quality of your code inside your CI pipeline Use: - black - code formatting - isort - optimize imports - bandit - check for security vulnerabilities - safety - check for security vulnerabilities of dependencies Github Actions Example š
- Python Clean Code Tip: Avoid using too many attributes on a single object. Try to cluster them to improve cohesion, reduce coupling, and improve readability š
- Python Clean Code Tip: Use dataclasses when only storing attributes inside your class instances to reduce the amount of boilerplate code š
- Python tip: To generate a random password you should use the secrets module For example, you can generate a random password of length 21 containing letters, number, and special charactersš
- Python Clean Code Tip: Don't use flags in functions Flags are variables passed to functions, which the function uses to determine its behavior. They are considered bad because functions should only perform one task. -> split your function into smaller functions.
- Python Clean Code Tip: Check the quality of your code inside your CI pipeline. - flake8 - style guide enforcer - black - code formatting - isort - optimize imports - bandit - check for security vulnerabilities - safety - check for security vulnerabilities of dependencies š
- Python Clean Code Tip: Use a list comprehension to filter lists instead of for loop š
- Python Clean Code Tip: Avoid using too many attributes on a single object. Try to cluster them to improve cohesion, reduce coupling, and improve readability š
- Python Clean Code Tip: Keep your arguments at a minimum Ideally, your functions should only have one to two arguments. If you need to provide more arguments to the function, you can create a config object which you pass to the function or split it into multiple functions.
- Python clean code tip: Use a dictionary to remove duplicates from a list and maintain order š
- Python Clean Code Tip: Avoid setting attributes of your objects outside of the constructor. Instead, implement methods that map to real-world concepts. Why? To ensure attributes exist and are easily discoverable. š
- Python clean code tip: Use dependency injection to simplify testing š















