[파이썬] 변수, 함수, 클래스 네이밍 규칙
When coding in Python, it’s important to follow proper naming conventions for variables, functions, and classes. Consistent and descriptive naming not only makes your code easier to understand but also improves its readability and maintainability. In this blog post, we will discuss some best practices for naming in Python.
Variables
- Variable names should be descriptive and reflect the purpose of the variable.
- Use lowercase letters and separate words with underscores (
snake_case
). - Avoid using single characters or abbreviations that are difficult to understand.
- Choose meaningful variable names like
age
,name
,total_sales
, etc.
total_sales = 1000
Functions
- Function names should be descriptive and indicate the action or purpose of the function.
- Use lowercase letters and separate words with underscores (
snake_case
). - Start the function name with a verb to make it clear that it’s performing an action.
- Avoid using ambiguous or generic names like
func1
,do_stuff
, etc.
def calculate_area(length, width):
return length * width
Classes
- Class names should be written in CamelCase starting with an uppercase letter.
- Use nouns or noun phrases to indicate what the class represents.
- Avoid using names that conflict with built-in Python objects or modules.
- Make sure the class name is singular and clearly describes the purpose or nature of the class.
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}")
Constants
- Constant names should be written in UPPERCASE letters.
- Use underscores to separate words (
THIS_IS_CONSTANT
). - Constants are usually defined at the module level and should not be modified.
- It’s a good practice to group related constants together.
PI = 3.1416
MAX_ATTEMPTS = 3
Conclusion
By following these naming conventions, your Python code will be more organized, readable, and easier to understand. Consistency in naming helps maintain a clean and professional coding style. Remember, good naming habits contribute to the overall quality and maintainability of your codebase.