Mastering Object-Oriented Programming in Python

OOP Principles and Examples

Sarper Makas
ILLUMINATION
3 min readJun 1, 2023

--

Article Prev

I know that writing clean and readable code is so important to you. A class is a fundamental concept in object-oriented programming that serves as a template for creating objects.

It provides a way to organize and structure code by grouping related data and behavior together. Also, it gives us the ability to make little changes for each object.

Objects created from a class are instances of that class and have access to its attributes and methods.

Classes are a key component of writing clean and readable code.

Understanding the advantages of OOP

As you can see, accessing elements by using a list is so complicated. That’s because you can’t use a specific name to access each property.

Let’s look at how to use OOP.

Introduction to object-oriented programming (OOP) concepts

class Person: creating a class called Person

__init__(self): The constructor method, which takes self as the first parameter and takes other parameters after.

self: A variable that stores the data of the object. You can define variables for the object, such as self.name = name.

Person(): Calls the __init__(self) method and creates an object of the class. By default, self is assigned, but you need to pass other parameters by yourself, just like a function.

john.name: Accessing self.name variable of the John object.

Defining Methods

You might confuse a method with a function. But a method is simply a function inside a method. The first parameter must be self (there are some exceptions).

As you see there isn’t a big difference between a method and a function. The first parameter must be self.

Static Methods

As you can see, there isn’t a big difference between a method and a function. The first parameter must be self.

Static Methods

Staticmethod is a method that we define by using the staticmethod decorator.

Learning about decorators is important. That’s why I wrote an article about decorators.

For defining a static method, you need to write @staticmethod at the previous line.

Static methods do not require self as a parameter that is because you can’t access a static method from the object, you can access it through the class name. Also, you do not have access to yourself from the staticmethod.

As you can see, I changed the give_info method. It isn’t taking self, and it can take a parameter like a normal method or function. But at line 6, you can see the decorator.

Finally, as you can see, we need to call the give_info method by using the Person class name.

Class Methods

classmethod is a method that we define by using the classmethod decorator.

For defining classmethod, you need to write @classmethod at the previous line.

classmethod does not require self a parameter. That is because you can’t access a classmethod from the object; you can access it through the last name. Also, you do not have access to self from classmethod, but you do have access to cls (which is the first parameter).

But a class method has access to class variables.

calculate_age(birth_year): Returns the age of the person from their birth year.

total_person: A class variable to which you have access from each object and from the class itself. You can modify class variables.

cls: You can access class variables from cls. It’s the first parameter of the class methods.

--

--