Classes and Objects

Author

Davide Vitiello, Mirai Solutions GmbH

Published

March 11, 2025

In object-oriented programming (OOP), classes and objects are fundamental concepts that enable developers to create programs that are modular, reusable, and easy to understand.

Other than refreshing the basics of classses in Python, we’ll be expanding on some key concepts in OOP, which will be covered in the following sections:

Defining a Class

A class in Python is a blueprint for creating objects. It defines a set of attributes and methods that the objects created from the class can have.

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

Creating an Object

An object is an instance of a class. Creating an object means creating a specific instance of a class with its own unique attributes.

my_dog = Dog("Rex", 5)
my_dog.name
'Rex'

Methods

Methods are functions defined within a class that define the behaviors of the objects of the class.

Constructor / Initializer

A constructor method is a special method that is automatically called when a new instance (object) of a class is created. It is defined using the __init__ method. The primary purpose of the constructor is to initialize the new object’s state, i.e., to assign values to the object’s properties or to perform any initial setup necessary for the object to be used.

A standard method, on the other hand, is a function that is defined inside a class and is used to define behaviors or actions that the objects of the class can perform. Unlike the constructor method, which is called automatically when an object is created, methods must be called explicitly using the object.

class Dog:
    def __init__(self, name, age): # initializer method
        self.name = name
        self.age = age

    def get_age(self): # another method
        return self.age

    def set_age(self, age): # another method
        self.age = age

Class Attributes

Class attributes are variables that are shared among all instances of a class. They are defined within the class construction but outside any of the instance methods. Class attributes are accessed using the class name or an instance of the class.

class Dog:
    species = "Canis familiaris"

    def __init__(self, name, age):
        self.name = name
        self.age = age

print(Dog.species)
my_dog = Dog("Buddy", 3)
print(my_dog.species)
Canis familiaris
Canis familiaris

Example of Class

Below is a more complete example of the Dog class, gathering together the previous bits, demonstrating how to define a class, create an object, and use its attributes and methods.

class Dog:
    species = "Canis familiaris"

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def description(self):
        return f"{self.name} is {self.age} years old"

    def speak(self, sound):
        return f"{self.name} says {sound}"

mikey = Dog("Mikey", 6)

print(f"{mikey.name} is {mikey.age} years old.")
Mikey is 6 years old.
if mikey.species == "Canis familiaris":
    print(f"{mikey.name} is a {mikey.species}.")

print(mikey.description())
print(mikey.speak("Woof Woof")) 
Mikey is a Canis familiaris.
Mikey is 6 years old
Mikey says Woof Woof
Back to top