Python

Creating an Object

YouTube

Object-oriented programming (OOP) is a programming paradigm that organizes code into “objects” that represent real-world entities and the actions that can be performed on them.

In Python, a class is a template or blueprint for creating objects. When you create a class, you define its properties (also called attributes) and the actions it can take (called methods).

Creating a class

To create a class, you use the class keyword followed by the name of the class. For example:

class Dog:
  pass
Properties and Methods

This creates a class called Dog that doesn’t do anything yet. To add properties (variables) and methods (functions) to the class, you define them inside the class definition. For example:

class Dog:
  def __init__(self, name, breed):
    self.name = name
    self.breed = breed
    
  def bark(self):
    print("Woof!")
__init__ Method

Here, the __init__ method is a special method in Python that is called when you create an object from a class. It’s used to initialize the object with certain values. The self keyword refers to the current object, and you use it to access the object’s properties and methods. In this example, __init__ takes two arguments: name and breed, and sets them as the object’s name and breed properties.

The bark method is a simple method that prints “Woof!” when called.

Creating an object

To create an object from a class, you use the class name followed by parentheses. This is called instantiating the class. For example:

dog1 = Dog("Buddy", "Labrador")
dog2 = Dog("Lucy", "Poodle")

This creates two objects, dog1 and dog2, with the name and breed properties set to “Buddy” and “Labrador” for dog1, and “Lucy” and “Poodle” for dog2.

Dot Notation

To access an object’s properties and methods, you use the dot notation. For example:

print(dog1.name)  
# Output: Buddy
dog2.bark()  
# Output: Woof!

Hopefully now, you can start to see how everything in Python is an object. For example, a list which has a method of append, pop, count, is really just a class called List with methods to add, remove or count the attributes.

Let’s create our own class. First, we will use a real world object such as a car to replicate. However, instead of creating a Car class, let’s create a Vehicle class. This vehicle will represent all possible real world vehicles, such as a car, lorry or even a motorbike. We will be specialising things further in coming pages, so for now, we will just define the attributes needed, plus some methods that all vehicles will have. We will then create an object of this class and ensure that it is working as expected.

class Vehicle:
    def __init__(self, model, make, year):
        self.model = model
        self.make = make
        self.year = year
        self.speed = 0
        
    def move(self):
        if self.year > 1970:
            self.speed += 30
            print("You are going 30mph!")
            
    def slow_down(self):
        if self.speed >= 1:
            self.speed -= 5

vehicle = Vehicle("Car", "Volkswagen", 2015)
vehicle.move()
print(vehicle.speed)