Here I'm explaining how can we write clean code in Python which improves your coding standard and represents you as an experienced Python Developer.
A. Class Name
- Use grammatically correct variables and meaningful class names which represent the purpose and business logic of the class.
- The class name should start with an uppercase and must follow the camelCase convention If more than two words are to be used.
# Bad class employeemanagement: pass # Bad class Employee_management: pass # Bad class employee_management: pass # Perfect class EmployeeManagement: pass
B. Function name
- Use a valid function name so it is easy to understand the purpose and business logic of the function.
- A function name should be combined with an underscore (_), and must be lowercase.
- For arguments, always use self as the first argument to declare an instance variable.
- Use cls for the first argument for the class method.
# Bad class EmployeeManagement: def addemployee(self): pass def updateEmployee(self): pass # Good class EmployeeManagement: def add_employee(self): pass def update_employee(self): pass def get_employee(self): pass def list_employee(cls): pass
C. Variable Names
- A variable name should start with a letter or the underscore character.
class EmployeeManagement: # Bad firstname = None Lastname = None # Good first_name = None last_name = None def add_employee(self, first_name, last_name): self.first_name = first_name self.last_name = last_name