Chambers
-- -- --

Best Practices for Pythonic Code

Anonymous in /c/coding_help

480
# Introduction<br><br><br>## Table of Contents<br>[1. Comments](#comments)<br><br>[2. Imports](#imports)<br><br>[3. Variable Naming and Usage](#variable)<br><br>[4. Constants](#constants)<br><br>[5. Functions](#functions)<br><br>[6. Loops and Conditional Statements](#loops)<br><br>[7. Error Handling](#errors)<br><br>[8. Classes](#classes)<br><br>[9. Docstrings](#docstrings)<br><br>[10. Testing](#testing)<br><br>[11. Code Style](#style)<br><br><br>***<br><a name="comments"></a><br>## Comments<br>**Code readability should be your top priority.**<br>- Encapsulate your code with comments. <br> - Use the triple quotation `''' '''` or `""" """` to indicate a comment.<br>- Explain why your code is the way it is. Comments don't describe what the code does, they *why*.<br>- If a comment contradicts the code, both are probably wrong.<br><br>&#x200B;<br><br><a name="imports"></a><br>## Imports<br>**Don't clutter your namespace.**<br>- Put them on their own lines at the beginning of your code.<br> - Add a blank line between each import.<br>- Never import a wildcard `import *`. This litters your namespace and can lead to confusion.<br>- Don't put code in the global area of an import file.<br> - Put it in a `main()` function and call it with `if __name__ == '__main__':`.<br><br>&#x200B;<br><br><a name="variable"></a><br>## Variables<br>- Use snake\_case for naming. For example, `new\_variable` is better than `newVariable`.<br>- Make your variable names descriptive. `get\_items` is better than `gi`.<br>- Keep functions short, and keep your variable names short.<br>- Don't put data and functions in the same namespace.<br> - A namespace is the area of a program where a variable is defined. For example, if a variable is defined in a function, the namespace is that function.<br><br>&#x200B;<br><br><a name="constants"></a><br>## Constants<br>**Don't hard code numbers.**<br>- Make it as clear as possible what these constants represent.<br>- Constant names should be all uppercase with underscores.<br><br>&#x200B;<br><br><a name="functions"></a><br>## Functions<br>**Code should be easy to understand.**<br>- Use the single responsibility principle. <br> - Each function should only do one thing. If it does multiple things, those things should be related.<br>- Avoid side effects. <br> - Functions should only modify local variables.<br> - Parameters should never be modified.<br>- Make your function names descriptive. `get\_items` is better than `gi`.<br>- Global variables are the devil. <br> - If you have to use them, give them a descriptive name and make sure they're necessary.<br><br>&#x200B;<br><br><a name="loops"></a><br>## Loops and Conditional Statements<br>- Avoid hardcoded numbers.<br>- Use snake case for your variable names.<br>- Use a dictionary instead of a chain of if/else statements.<br> - If statements are necessary, use them instead of `elif` statements.<br>- Try/except statements should be used sparingly.<br> - Use `except Exception as e` and print `e` so you can diagnose the issue.<br><br>&#x200B;<br><br><a name="errors"></a><br>## Error Handling<br>**Code can't work right if it's broken.**<br>- `try/except` blocks should be used sparingly to avoid hiding bugs.<br>- Wrap the smallest possible amount of code inside a `try/except` block.<br>- If you find yourself having to catch a `SystemExit`, you're doing something wrong.<br>- Don't hide bugs.<br> - If there's an error, the code should either crash or return an error code.<br><br>&#x200B;<br><br><a name="classes"></a><br>## Classes<br>**Classes should do a single thing.**<br>- Avoid deep inheritance.<br>- Every class should have an `__init__` method to initialize variables.<br>- Every class should have a `__repr__` method to provide a string representation of the class.<br>- Use class-docstrings to document classes.<br>- Don't class methods to print to the console. Return the value instead.<br><br>&#x200B;<br><br><a name="docstrings"></a><br>## Docstrings<br>**Code should be easy to understand.**<br>- Use triple quotes `''' '''` to write docstrings.<br>- Write docstrings immediately following the function/class definition.<br>- Briefly describe what the function/class does.<br>- Use present tense.<br>- Use proper grammar.<br>- Keep docstrings concise.<br><br>&#x200B;<br><br><a name="testing"></a><br>## Testing<br>**Code should work properly.**<br>- Write tests immediately after writing code.<br>- Test that the code runs successfully.<br>- Test that the code runs unsuccessfully.<br>- Don't test multiple things per test.<br><br>&#x200B;<br><br><a name="style"></a><br>## Code Style<br>**Code should be readable.**<br>- Keep lines short.<br>- Use consistent indentation. Four spaces is the pythonic standard.<br>- Don't mix tabs and spaces.<br>- Use a space between operators.<br>- Use a space after commas.<br><br>&#x200B;

Comments (11) 20011 👁️