Python Classes


Classes in Python

A class in Python is a blueprint for creating objects. It encapsulates data for the object and provides methods to manipulate that data. Classes allow you to structure your code in a way that is more manageable, modular, and reusable.

Key Components of a Class

  1. Class Definition: Created using the class keyword.
  2. Attributes: Variables that hold data specific to an object.
  3. Methods: Functions defined inside the class that operate on the object's attributes.
  4. Constructor (__init__ method): A special method that initializes an object's attributes when the object is created.

Example of a Class

Let’s define a simple class called Car to illustrate these concepts.

Step 1: Define the Class

class Car: def __init__(self, make, model, year): self.make = make # Instance variable self.model = model # Instance variable self.year = year # Instance variable def display_info(self): # Instance method return f"{self.year} {self.make} {self.model}" def start_engine(self): # Another method return f"The engine of the {self.make} {self.model} is now running."

Step 2: Create Objects

You create objects of the class by calling the class name with the appropriate arguments for the constructor.

# Creating objects (instances) of the Car class car1 = Car("Toyota", "Camry", 2021) car2 = Car("Honda", "Civic", 2020)

Step 3: Access Attributes and Methods

You can access the attributes and methods of an object using the dot (.) operator.

# Accessing attributes print(car1.make) # Output: Toyota print(car2.model) # Output: Civic print(car1.year) # Output: 2021 # Calling methods print(car1.display_info()) # Output: 2021 Toyota Camry print(car2.start_engine()) # Output: The engine of the Honda Civic is now running.

Complete Example

Here’s a complete example that combines all the elements discussed:

class Car: def __init__(self, make, model, year): self.make = make # Instance variable self.model = model # Instance variable self.year = year # Instance variable def display_info(self): # Instance method return f"{self.year} {self.make} {self.model}" def start_engine(self): # Another method return f"The engine of the {self.make} {self.model} is now running." # Creating objects car1 = Car("Toyota", "Camry", 2021) car2 = Car("Honda", "Civic", 2020) # Accessing attributes print(f"Car 1: {car1.display_info()}") # Output: Car 1: 2021 Toyota Camry print(f"Car 2: {car2.display_info()}") # Output: Car 2: 2020 Honda Civic # Calling methods print(car1.start_engine()) # Output: The engine of the Toyota Camry is now running. print(car2.start_engine()) # Output: The engine of the Honda Civic is now running.

Summary

  • Class Definition: The Car class is defined with an __init__ constructor to initialize its attributes.
  • Creating Objects: Instances of the Car class are created (car1 and car2).
  • Accessing Data: You can access the attributes and methods of the objects using the dot notation.
  • Encapsulation: The class encapsulates data and functions related to the Car objects, making the code modular and easier to manage.

Classes are a powerful feature of Python that allow you to create structured and reusable code!