# Naming Rules for Python Files
[TOC]
This is how you should naming your Homeworks (Python file).
> Answer credited from Chatgpt 4-o
When naming Python files, it's important to follow a set of conventions that promote readability, compatibility, and adherence to best practices. Here are some common naming rules for Python files:
## Common Python File Naming Rules
1. **Use Lowercase Letters**:
- File names should generally be in all lowercase letters. This is a widely accepted convention in Python to ensure consistency and avoid any issues with case-sensitive file systems.
- Example: `my_script.py`, `data_processor.py`
2. **Use Underscores for Separation**:
- Use underscores (`_`) to separate words in the file name instead of spaces. Spaces in file names can lead to errors or unexpected behavior in some systems.
- Example: `data_analysis.py`, `file_reader.py`
3. **Avoid Special Characters**:
- Do not use special characters such as `!`, `@`, `#`, `$`, `%`, `^`, `&`, `*`, `(`, `)`, `=`, `+`, `{`, `}`, `[`, `]`, `;`, `:`, `'`, `"`, `<`, `>`, `,`, `?`, or `/`. These can cause errors or unexpected behavior in different environments.
- Stick to letters, numbers, and underscores.
4. **Do Not Use Spaces**:
- Avoid using spaces in file names, as they can cause problems in command-line interfaces and scripts.
- Instead, use underscores or hyphens (`-`), although underscores are more common in Python.
5. **Avoid Using Python Keywords**:
- Do not use Python reserved keywords (like `class`, `def`, `import`, `lambda`, etc.) as file names to prevent conflicts and unexpected errors.
- Example: Avoid naming a file `class.py` or `import.py`.
6. **Use English Language**:
- Use English words for file names to ensure broader compatibility and readability by an international audience.
7. **Keep It Descriptive but Concise**:
- File names should be descriptive enough to indicate their purpose or content but should also be concise to avoid unnecessarily long names.
- Example: `data_loader.py` is better than `this_is_a_script_to_load_data.py`.
8. **Do Not Start with Numbers**:
- Avoid starting a file name with a number. While technically allowed, it can be confusing and might conflict with certain environments or naming conventions.
- Example: Avoid `1st_script.py`; instead, use `first_script.py`.
9. **Use `.py` Extension**:
- All Python files should end with the `.py` extension. This ensures they are recognized as Python scripts by editors and tools.
- Example: `my_module.py`
10. **Avoid Names with Leading Underscores**:
- Leading underscores in file names are typically reserved for special cases (like indicating private modules in packages). Unless necessary, avoid them.
- Example: Use `helper_functions.py` instead of `_helper_functions.py`.
### Not Strict but Recommended
- **Consistency Across the Project**:
- Consistency in naming conventions across your entire project is essential for maintainability. Choose a style and stick to it throughout your project.
- **Avoid File Names Matching Python Standard Libraries**:
- Avoid naming your file after standard Python libraries (like `math.py`, `sys.py`, `json.py`). This can cause namespace conflicts.
## Summary
While some rules are strict (like no spaces or special characters), others are more about following best practices to make your codebase easy to read and maintain. Adhering to these conventions ensures your Python files remain portable, compatible, and understandable across different platforms and by different developers.