Junior — Middle
What is the difference between a class and a module in programming?
sobes.tech AI
Answer from AI
In Python, a class is a template for creating objects that encapsulate data (attributes) and behavior (methods). A class defines the structure and functionality of objects.
A module is a file containing Python code, which can include definitions of functions, classes, and variables. Modules are used for organizing and reusing code.
Example:
- A class describes how an object is structured (for example, the
Carclass). - A module is a file
car.py, which may contain this class and other functions related to cars.
Thus, a class is a construct within a module, and a module is a unit of code organization.