DEV Community

Cover image for Classes in Python
Heather Kruszewski
Heather Kruszewski

Posted on

Classes in Python

Python Classes: A Comprehensive Guide

Python, with its simplicity and flexibility, has become one of the most popular programming languages in the world. Among its many features, classes stand out as a powerful tool for organizing and structuring code. In this comprehensive guide, we'll dive into the world of Python classes, covering everything from basic creation and usage to more advanced concepts like inheritance and importing classes.

Creating and Using a Class

  • In Python, a class is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of the class will have. Let's start with a simple example of a Car class:
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def display_info(self):
        print(f"{self.year} {self.make} {self.model}")
Enter fullscreen mode Exit fullscreen mode
  • Here, we define a Car class with attributes make, model, and year, and a method display_info() to print out information about the car.

  • Now, let's create an instance of this class and use it:

my_car = Car("Toyota", "Camry", 2020)
my_car.display_info()  
Enter fullscreen mode Exit fullscreen mode
  • Returns: 2020 Toyota Camry

  • We instantiate the Car class with specific values for make, model, and year, and then call the display_info() method to print out the car's information.

Classes and Instances

  • In object-oriented programming, a class serves as a template for creating objects, while an instance is a specific realization of that template. Let's expand our Car class to include additional functionality:
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def display_info(self):
        print(f"{self.year} {self.make} {self.model}")

    def read_odometer(self):
        print(f"This car has {self.odometer_reading} miles on it.")
Enter fullscreen mode Exit fullscreen mode
  • In this updated version, we added an odometer_reading attribute and a method read_odometer() to display the car's mileage. Now, let's create an instance and access its attributes and methods:
my_car = Car("Toyota", "Camry", 2020)
my_car.read_odometer() 
Enter fullscreen mode Exit fullscreen mode
  • Returns: This car has 0 miles on it.

  • Here, my_car is an instance of the Car class, and we can access its attributes like make, model, and year, as well as call its methods like read_odometer().

Inheritance

  • Inheritance is a fundamental concept in object-oriented programming that allows a new class to inherit attributes and methods from an existing class. Let's create a ElectricCar class that inherits from our Car class:
class ElectricCar(Car):
    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.battery_size = 75

    def describe_battery(self):
        print(f"This car has a {self.battery_size}-kWh battery.")
Enter fullscreen mode Exit fullscreen mode
  • Here, ElectricCar inherits from Car, so it automatically has the attributes and methods of the Car class. Additionally, it defines its own attribute battery_size and method describe_battery().

  • Now, let's create an instance of ElectricCar and use its methods:

my_electric_car = ElectricCar("Tesla", "Model S", 2022)
my_electric_car.display_info()  
my_electric_car.describe_battery()  
Enter fullscreen mode Exit fullscreen mode
  • Returns: 2022 Tesla Model S
  • Returns: This car has a 75-kWh battery.

Importing Classes

In Python, classes can be organized into modules and imported into other Python scripts as needed. Let's create a separate file named electric_car.py containing the ElectricCar class:

# electric_car.py
class ElectricCar(Car):
    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.battery_size = 75

    def describe_battery(self):
        print(f"This car has a {self.battery_size}-kWh battery.")
Enter fullscreen mode Exit fullscreen mode
  • Now, we can import the ElectricCar class into our main script and use it:
from electric_car import ElectricCar

my_electric_car = ElectricCar("Tesla", "Model S", 2022)
my_electric_car.describe_battery()  # Output: This car has a 75-kWh battery.
Enter fullscreen mode Exit fullscreen mode
  • Returns: This car has a 75-kWh battery.

  • By organizing classes into separate modules, we can maintain a clean and modular codebase.

Handling Exceptions in Classes

  • In Python, exceptions are used to handle errors that occur during program execution. Let's incorporate exception handling into our Car class example:
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        try:
            if year < 1900 or year > 2024:
                raise ValueError("Invalid year")
            self.year = year
        except ValueError as e:
            print(f"Error: {e}")
            self.year = None

    def info(self):
        if self.year:
            return f"{self.year} {self.make} {self.model}"
        else:
            return "Information unavailable"
Enter fullscreen mode Exit fullscreen mode
  • In this modified Car class, we've added exception handling to the constructor __init__() method. If an invalid year is provided (outside the range 1900-2024), a ValueError is raised. We catch this exception and print an error message, setting self.year to None in case of an error.
my_car = Car("Toyota", "Camry", 2022)
print(my_car.info())  

invalid_car = Car("Ford", "Mustang", 1899)
print(invalid_car.info())  
Enter fullscreen mode Exit fullscreen mode
  • Result: 2022 Toyota Camry
  • Result: Information Unavailable
  • With exception handling, our Car class gracefully handles invalid input, preventing unexpected crashes and providing informative error messages to the user.

Conclusion

In this guide, we've explored the fundamentals of Python classes, including creating and using classes, working with instances, understanding inheritance, importing classes from other modules, handling exceptions. Classes are a powerful feature of Python that enable code organization, reusability, and abstraction. By mastering classes, you'll be better equipped to build complex and scalable Python applications.

Resources

  • Mathes, E. (2023). Python crash course. No Starch Press.
  • (2023). Real Python. Retrieved February 2024.

Top comments (0)