Introduction to Object-Oriented Programming in Python

Published by Mateusz Kędziora on

img LgWlXjgl6KxmAZyVIpCg1e2j

Object-oriented programming is a style of programming where data and operations on that data are organized around “objects”. An object is a concept from mathematics that in computer science means something that has state and behavior. In object-oriented programming, objects are created based on “classes”, which define what attributes (i.e., properties) and methods (i.e., functions) the objects should have.

To create a class in Python, we start with the keyword “class”, followed by the name of the class. In parentheses, we specify the name of the class that this class is inheriting from (called the “base class”), if applicable. Inside the class definition, we create attributes and methods using the keyword “self”. Example:

class Car:
    def __init__(self, brand, model, engine_capacity):
        self.brand = brand
        self.model = model
        self.engine_capacity = engine_capacity
        self.mileage = 0
    
    def drive_distance(self, distance):
        self.mileage += distance

In the above example, we create a class “Car” that has three attributes: “brand”, “model”, and “engine_capacity”. The class also has a method “drive_distance” that allows us to increase the value of the “mileage” attribute by a given distance value.

To create an object based on a class, we call the class constructor, which is the __init__ method. The constructor is called automatically when creating an object and is used to initialize the object’s attributes. Example:

my_car = Car("Ford", "Mustang", 5.0)
print(my_car.brand) # "Ford"
print(my_car.model) # "Mustang"
print(my_car.engine_capacity) # 5.0
print(my_car.mileage) # 0

To call a method of an object, we use the dot (.) and specify the method name and any arguments that are needed. Example:

my_car.drive_distance(100)
print(my_car.mileage) # 100

Object-oriented programming allows us to create a class structure where one class can inherit characteristics from another class (called “child classes”). In Python, inheritance is achieved by specifying the base class name in parentheses when creating the child class. Example:

class Sedan(Car):
    def __init__(self, brand, model, engine_capacity, seating):
        super().__init__(brand, model, engine_capacity)
        self.seating = seating

my_sedan = Sedan("Ford", "Mustang", 5.0, 4)
print(my_sedan.brand) # "Ford"
print(my_sedan.seating) # 4

In the above example, we create a class “Sedan” that inherits from the “Car” class. The class has an additional attribute “seating” and calls the base class constructor using the super() function.

To summarize, object-oriented programming allows us to create a structure of code where data and operations are organized around objects. Classes are templates for creating objects, and objects have attributes and methods. Through inheritance, we can create class hierarchies where child classes inherit characteristics from base classes and can extend them with their own. Object-oriented programming allows for better organization of code and makes it easier to extend and maintain.

Text generated using chat.openai.com.

Categories: Python

Mateusz Kędziora

Jestem uczniem technikum a prowadzeniem bloga zajmuje się hobbystycznie.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *