Skip to content

Instantly share code, notes, and snippets.

@thamognya
Created January 23, 2023 04:20
Show Gist options
  • Save thamognya/1b1fcbb8d992da74bc12382cc68d3455 to your computer and use it in GitHub Desktop.
Save thamognya/1b1fcbb8d992da74bc12382cc68d3455 to your computer and use it in GitHub Desktop.
Python Good Practices

Python Good Practices

Here are a few good practices for writing Python code:

Follow the PEP 8 style guide for Python code. This guide provides guidelines for formatting and structuring code in a consistent and readable manner.

Use meaningful and descriptive variable names. Variable names should be clear and indicate the purpose of the variable. Avoid using single-letter variable names, unless they are used as temporary loop variables or for similar purposes.

Use snake_case for variable and function names, and use PascalCase for class names.

Use meaningful and descriptive function and method names. Function and method names should indicate the purpose of the function or method.

Use the "import as" syntax to give clear and meaningful names to imported modules.

Use docstrings to provide documentation for functions, methods, and classes. Docstrings should provide a brief description of what the function, method, or class does, and any input and output parameters.

Use comments to explain complex or non-obvious code. Comments should be used sparingly and should explain why the code is written in a certain way, rather than what the code is doing.

Use exception handling to handle errors and unexpected situations.

Avoid using global variables if possible. Use function arguments and return values instead.

Keep your functions and methods small and focused. Functions and methods that do too much are harder to understand, test and debug.

By following these practices, you can make your Python code more readable, maintainable, and less prone to errors.

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