How to write a class in Python

A simple guide on how to create objects using Python classes

Objects are one of the fundamentals in any OOP language because they allow to both improve the management of your code and allow for a proper modularization of all processes.

What is an OOP language

The main objective of classes is being able to structure your code so that you can reuse some parts of it with minor variations called parameters. Generally, we would use functions for the same purpose, but while we can only store the output of a function, we can store entire objects into variables.

For example, if I want to simulate with a computer a pet that performs actions, has its own stats, and reacts to my inputs, a variable is too simple because I am not only interested in the output of the object (for example print “I want food”). I want to save the state of the object (its stats).

The use of classes has become so big that some languages have been based entirely on this programming methodology. With time it became a paradigm, and we refer to it as Object-Oriented Programming.

To object or not to object

There are discordant opinions on the advantage of using an object-oriented programming language. Some people love it, some others hate it with all their heart, and they are quite open about their feelings. There is no particular reason why people are averse to the adoption of this programming paradigm.

Personally, I find object-oriented programming languages to be the best option to organize code. Because I am a control freak and I always try to keep everything pathologically organized, as you can imagine I am quite fond of the use that developers make of classes.

Creating an object in Python

Creating an object in Python is extremely easy. However, all the tutorials that are available online, especially the ones taken directly from university lessons, manage to make this task way more complicated than it really is. Let us clarify what is the terminology associated with objects.

What is a class?

First of all, a class is a data structure.

Class state

A class can store information in time: all its stored information is called its state.

Class methods

A class can also store functions: a function that is part of a class is called a method.

Objects

We can create replicas of the same class that we call objects, and the process of creating an object is called instantiation.

Class vs. Object

A class is the code that is ready to become an object. It has no state, as it has not been called yet. From a class we can create an object, one of its possible variations.

Create an empty class

Let us now see how easy it is to create a class and then objects using Python. There are no libraries to import. This is the basic code to create a class in python: as you can see there is a first part where you declare the class, then a second part in which two objects are created using the same class.

#class creation
class class1():
    def __init__(self):
        pass

#instantiation
obj1 = class1()
obj2 = class1()

Obviously, this class does not do anything: its only string of code is pass.

What is __init__?

One of the most confusing things when learning classes is understanding what def __init__ does. Very simple: this code runs only once when an object is instantiated. So, when obj1, and also obj2 have been declared for the first time, this function has run once for each class.

A class without any code inside is useless and also purposeless. In the next article, I am going to create a class that is able to accept inputs, so that they can be stored as the state of the class.

Join our free programming community on discord, learn how to code, and meet other experts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: