Define Classes and Objects in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn the fundamental concepts of Object-Oriented Programming (OOP) in Python. We will explore how to define classes, which serve as blueprints for creating objects, and understand the relationship between classes and objects.

You will gain practical experience by creating and using instances of your defined classes. The lab will guide you through initializing objects with the __init__ method to set their initial state and customizing their string representation using the __repr__ method for better debugging and readability.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a beginner level lab with a 94% completion rate. It has received a 100% positive review rate from learners.

Define a Class and Create an Instance

In this step, you will learn how to define a basic class and create an object, or instance, from it.

In Object-Oriented Programming, a class is a blueprint for creating objects. It defines a set of attributes (data) and methods (functions) that the created objects will have. An object is an instance of a class, a concrete entity built from the class blueprint.

Let's start by creating a simple Dog class.

First, open the dog.py file from the file explorer on the left side of the WebIDE. This file is currently empty.

Now, add the following code to dog.py to define the Dog class, create an instance, and use it:

## Define a simple Dog class
class Dog:
    ## Class attribute
    species = "Canis familiaris"

    ## Method
    def bark(self):
        print("Woof!")

## Create an instance (object) of the Dog class
my_dog = Dog()

## Access the class attribute using the instance
print(f"The species is: {my_dog.species}")

## Call the instance's method
print("The dog says:")
my_dog.bark()

Let's break down the code:

  • class Dog:: This line defines a new class named Dog.
  • species = "Canis familiaris": This is a class attribute. Its value is shared among all instances of the Dog class.
  • def bark(self):: This defines a method, which is a function inside a class. The self parameter is a reference to the current instance of the class and is used to access variables that belong to the class.
  • my_dog = Dog(): This line creates a new instance of the Dog class and assigns it to the variable my_dog.
  • my_dog.species: We access the species attribute of the my_dog object using dot notation.
  • my_dog.bark(): We call the bark method on the my_dog object. Python automatically passes the my_dog object as the self argument to the method.

Save the file. To run your script, open a terminal in the WebIDE and execute the following command:

python dog.py

You should see the following output, confirming that your object was created and its attribute and method were accessed correctly.

The species is: Canis familiaris
The dog says:
Woof!

Initialize Objects with the __init__ Method

While class attributes are shared by all instances, you often want each instance to have its own unique data. The __init__ method is a special method in Python classes that acts as a constructor. It is automatically called when you create a new instance, allowing you to initialize its unique attributes.

Let's modify the Dog class to give each dog a unique name and breed.

Open the dog.py file again and replace its content with the following code:

## Define a Dog class with an __init__ method
class Dog:
    ## Class attribute
    species = "Canis familiaris"

    ## The __init__ method (constructor)
    def __init__(self, name, breed):
        ## Instance attributes
        self.name = name
        self.breed = breed

    ## Method
    def bark(self):
        print("Woof!")

## Create instances with unique attributes
dog1 = Dog("Buddy", "Golden Retriever")
dog2 = Dog("Lucy", "Labrador")

## Access the instance attributes
print(f"{dog1.name} is a {dog1.breed}.")
print(f"{dog2.name} is a {dog2.breed}.")

## The class attribute is still shared
print(f"Both are of the species: {dog1.species}")

Here's what changed:

  • def __init__(self, name, breed):: We defined the __init__ method. It takes self, name, and breed as arguments.
  • self.name = name and self.breed = breed: These lines create instance attributes. They are unique to each Dog object. The self keyword attaches these attributes to the specific instance being created.
  • dog1 = Dog("Buddy", "Golden Retriever"): When creating an instance, we now pass arguments for name and breed. These are automatically passed to the __init__ method.

Save the file and run it from the terminal:

python dog.py

The output shows that each dog object has its own name and breed, while still sharing the class attribute species.

Buddy is a Golden Retriever.
Lucy is a Labrador.
Both are of the species: Canis familiaris

Customize Object Representation with __repr__

When you try to print an object directly, Python shows a default representation that includes the object's class and memory address, which is not very helpful. You can provide a more descriptive and developer-friendly string representation by defining the __repr__ special method.

Let's add a __repr__ method to our Dog class.

Open dog.py and replace its content with the final version of our code:

## Define a Dog class with __init__ and __repr__ methods
class Dog:
    ## Class attribute
    species = "Canis familiaris"

    ## The __init__ method (constructor)
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

    ## The __repr__ method for a developer-friendly representation
    def __repr__(self):
        return f"Dog(name='{self.name}', breed='{self.breed}')"

    ## Method
    def bark(self):
        print("Woof!")

## Create an instance
my_dog = Dog("Buddy", "Golden Retriever")

## Print the object directly
print(my_dog)

The key addition is:

  • def __repr__(self):: This defines the representation method.
  • return f"Dog(name='{self.name}', breed='{self.breed}')": This method should return a string. A good practice is to make the string look like a valid Python expression that could be used to recreate the object with the same state.

Save the file and run the script one last time:

python dog.py

Instead of a memory address, the output is now the clear, informative string returned by your __repr__ method. This is extremely useful for debugging.

Dog(name='Buddy', breed='Golden Retriever')

Summary

In this lab, you have learned the basics of Object-Oriented Programming in Python. You started by defining a simple class and creating an instance of it, learning how to use class attributes and methods. You then enhanced your class by adding the __init__ method to initialize each object with unique instance attributes. Finally, you customized the string representation of your objects by implementing the __repr__ method, which greatly improves the clarity and debuggability of your code. These concepts are the foundation for building more complex and organized applications in Python.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.