Skip to content

Instantly share code, notes, and snippets.

@Julynx
Created July 8, 2025 18:15
Show Gist options
  • Save Julynx/7a2c37d1d31391fe088421f84ec5884c to your computer and use it in GitHub Desktop.
Save Julynx/7a2c37d1d31391fe088421f84ec5884c to your computer and use it in GitHub Desktop.
The last Python style guide you'll ever need

When writing Python code, make sure you follow these guidelines strictly to ensure mantainability and production-readiness:

  1. Use descriptive variable names. Choose full english words over abbreviations or generic terms.

  2. Write function and module docstrings. After every set of changes, use a formatter like black, then a linter like pylint to check for errors and warnings. Correct all of them without bypassing anything until you achieve a perfect 10/10 score.

  3. Include extensive error handling. Use exceptions for unexpected outcomes and return values for expected outcomes. Use a proper logging module to log any errors and their tracebacks to a log file, not just the console.

  4. A function should do one thing and one thing only. Functions should be semantically organized into modules, neatly grouped inside descriptively named directories, and not just sit in the root directory.

  5. Write tests whenever you add a new feature or fix a bug, and run those tests after every set of changes to ensure the code works as expected. If tests fail, rewrite and run again until all pass.

  6. Follow the Pythonic way of doing things. Use built-in functions over for loops, Path objects over raw strings, context managers over open/close statements, exceptions over error values, etc.

  7. Do not reinvent the wheel. Use well-established libraries and modules for common or complex tasks instead of writing your own implementations.

  8. Keep data, configuration, access keys and other resources separate from program logic. Create independent files with the appropriate file extensions: .env for environment variables, .toml for configuration, .csv or .json for data, etc. These files should exist inside descriptively named directories and be loaded by the script using the appropriate library.

  9. Remove unused code regularly, making sure it is truly unused. If you notice the code getting too long or the logic too convoluted, consider a full refactor before proceeding with the next feature.

  10. Break tasks into subtasks before approaching them. Track progress using a ROADMAP.md file with checkboxes and update it after every set of changes.

@Julynx
Copy link
Author

Julynx commented Jul 8, 2025

NOTE: This file is brief and generic on purpose to be used as a prompt or as a rules file for AI code generation, to instantly improve the quality of generated code. It can also be easily integrated into most Python projects as a style guide. This is not meant to be used in place of a CONTRIBUTING.md file or similar, though.

This gist was written entirely by a human. Remember to star it if you found it useful for your use case!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment