Section

Putting It All Together: A Simple OOP Example in Python

Part of The Prince Academy's AI & DX engineering stack.

Follow The Prince Academy Inc.

Now that we've introduced the core concepts of Object-Oriented Programming (OOP) – classes and objects – let's bring them to life with a practical and simple example. We'll create a Dog class, which is a blueprint for representing different dog objects. Each dog will have its own characteristics (attributes) and behaviors (methods).

class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed
        self.is_hungry = True

    def bark(self):
        return "Woof! Woof!"

    def eat(self):
        if self.is_hungry:
            self.is_hungry = False
            return f"{self.name} is eating."
        else:
            return f"{self.name} is not hungry right now."

In this Dog class:

  • class Dog:: This line defines our blueprint for creating Dog objects.
  • def __init__(self, name, breed):: This is the constructor method. It's called automatically when you create a new Dog object. self refers to the instance of the class being created. name and breed are parameters we pass in to customize each dog. Inside __init__, we set up the initial state of the dog object by assigning values to its attributes: self.name, self.breed, and self.is_hungry (which defaults to True for a new dog).
  • def bark(self):: This is a method that defines a behavior for our Dog objects. When called, it returns the string "Woof! Woof!".
  • def eat(self):: This method also defines a behavior. It checks if the dog is_hungry. If it is, it sets is_hungry to False and returns a message indicating the dog is eating. Otherwise, it informs that the dog isn't hungry.

Now, let's create some actual Dog objects (instances) from our Dog class and see them in action:

my_dog = Dog("Buddy", "Golden Retriever")
your_dog = Dog("Lucy", "Poodle")

print(f"My dog's name is {my_dog.name} and he is a {my_dog.breed}.")
print(f"Your dog's name is {your_dog.name} and she is a {your_dog.breed}.")

print(my_dog.bark())
print(your_dog.eat())
print(your_dog.eat()) # Trying to eat again

In this code:

  • my_dog = Dog("Buddy", "Golden Retriever"): We're creating an object named my_dog from the Dog class. We pass "Buddy" and "Golden Retriever" to the __init__ method, which sets up the name and breed for this specific Dog object.
  • your_dog = Dog("Lucy", "Poodle"): Similarly, we create another Dog object named your_dog.
  • print(f"My dog's name is {my_dog.name}..."): We access the attributes of our objects using dot notation (e.g., my_dog.name).
  • print(my_dog.bark()): We call the bark method on the my_dog object.
  • print(your_dog.eat()): We call the eat method. The first time, Lucy is hungry, so she eats.
  • print(your_dog.eat()): The second time we call eat on your_dog, she's no longer hungry, and the method reflects that.

This example demonstrates how a class serves as a blueprint to create multiple objects, each with its own unique set of data (attributes) and the ability to perform actions (methods). This is the essence of putting OOP concepts into practice.

classDiagram
    class Dog {
        -name: str
        -breed: str
        -is_hungry: bool
        +__init__(name: str, breed: str)
        +bark()
        +eat()
    }
    Dog --|> object