Object-Oriented Programming in Python — Understanding Classes and Objects

Sari Lakkis
6 min readJun 8, 2021

In this article, we’ll discuss object-oriented programming (OOP) in Python. We’ll discuss classes and objects and we’ll learn how to create a class with its own properties and methods then we’ll see how to create an instance (object) from this class.

For this article, our problem is to create a model for products in an online store to manage them. Let’s see how we can do that with OOP!

The Source code of this article can be downloaded from my GitHub Python repository.

Let’s start!

Object-Oriented Programming (OOP)

OOP has dominated the software industry for many years now. From what its name says, it’s about using the idea of an object and a class to structure a software program in a way that it’s made of simple and reusable parts.

The reusable part of the code is constructed into a single object and its blueprint will be called the class. This class can be used to create objects.

Don’t worry we’ll understand what does that means shortly.

Let’s first explore objects!

Photo by Bianca Ackermann on Unsplash

What is an Object?

In most real-life apps we need to have more complicated types than our primitive data types (int, string…, etc.). We have things like: a customer or a product for example.

We need to model those things in our programs, instead of defining many variables to hold the data of a product, for example, its brand, price, and other properties, we can group them into one single entity, we’ll call it an object.

An object can represent any entity in the real world like a product or an abstract thing like a line or a currency etc.

Object Properties

The object has properties like the name of an object person, the color of an object line, the price of a product, and so on.

Object Methods

Also, the object has its own methods that define its behavior, for example, a method draw() for a line object or a method apply_discount() for a product object.

Now the blueprint code of objects that allows us to create them and use them in our programs is called a class.

What is a Class?

As we said, it’s the blueprint of the object, it carries the definition of the properties and the methods of this object.

The class will act as a type also that we use to create our objects, where an object created will be called an instance of this class.

Let’s take this example, we have an online store that has many products. For simplicity, we’ll assume that a product only has a brand and a price property. We want to write the program that manages our store.

How can we model a product in our example?

We might define each product with two variables a string and a float, for example:

product_brand_1 = "lenovo"
product_price_1 = 1200

But it will be very had to define the whole products like that also we’ll not be able to manage them in loops or combine the brand to the price where needed since they are independent variables.

So, instead, we’ll create our new type that has the name Product, which is simply the class, and then we can create as many products from it which are the objects.

Let’s do that to understand the idea more in-depth!

Creating a Class in Python

To create a class in Python we simply use the “class” keyword followed by the class name for example to create our Product class we write:

class Product:

The above code creates a class, not an object so it serves only as a blueprint for our real product objects that we’ll create later depending on what we define our class to be.

So up till now no object of type Product exists in the memory, deal? Great.

We can now directly define the properties inside the class but that will not be very useful so let’s jump directly and see the important function that we’ll use to do that!

The __init__ Function:

Inside the class, we define the built-in function called __init__. This function is executed once an object is created from this class, so we’ll use it to simultaneously define the properties and assign values to them.

We don’t have to explicitly define the object’s properties outside this function.

For our Product class we want to have two properties brand and price so we’ll pass the values that we want to be assigned to them when created as parameters:

def __init__(self, brand, price):

The variable ‘self’ is the first parameter, it will reference this class instance so the function can access the properties, we can name it any name if we want but must be the first parameter.

We place any code for initialization inside the __init__, so we’ll now assign the object properties with the parameters using the variable self.

To reference the object property, for example, the brand we write self.brand and then we assign it with the parameter brand.

We kept the same names for convenience but we could choose any name for our object property on the left side. For example, we could write:

self.brand_name = brand

And then the property of the object will be then called brand_name.

The full class definition with the two properties will be:

class Product:
def __init__(self, brand, price):
self.brand = brand
self.price = price

Creating an Instance or an Object in Python

The class Product defined above serves as a type that we can create many objects from it. Similar to what we do when we create an instance or object from an existing type like a List.

Now we have our own type Product to do that, so to create an object or an instance of the product we simply write:

Product("Lenovo", 1200)

The what creates an object and called the __init__ function we saw above. It’s called a constructor in many languages.

However, this object is anonymous, which might be useful in certain situations but if we need to keep track of it, we must assign it to a variable for example p1.

p1 = Product("Lenovo", 1200)

So now p1 is the reference that points to the object.

Accessing the Properties of an Object in Python

We can access the properties of an object in Python using the dot “.” accessor, we can either read or modify the value of the property. For example, we’ll change the price of p1:

p1.price = 1400

We can read the values of object properties the same way, for example:

print(p1.brand, p1.price)

Defining Object Methods in Python

Object methods are functions that belong to the object.

We saw how to define functions in the previous article, now our object can have its own functions that can access its properties and modify them.

For example, we want to create a function that changes the price of a product by applying a certain discount that will be passed as a parameter.

The difference between a normal function we could define outside the class and the method that we will define is that this method will change the object’s price while an outsider function would not.

We define the method as any function but we add a parameter ‘self’, if we want, to be able to access the object’s properties.

Here we change the value of the property price by applying the discount passed as a parameter.

def apply_discount(self, discount):
self.price = self.price * discount/100

How to Call an Object Method in Python?

To call the methods of an object we also use the dot (.) accessor similar to the way of accessing object properties. For example, to call our function apply_discount we’ll use the instance created p1:

p1.apply_discount(50)

If we print the value of p1.price we’ll see that the price was changed:

p1.apply_discount(50) #applying discount
print("Price after discount: ", p1.price)

The output will be:

Price after discount:  700.0

We can also call the function by using the class name Product and setting the first parameter to be the object we need to change its price:

p1 = Product("Lenovo", 1200)
Product.apply_discount(p1, 30)

The Complete Code

class Product:
def __init__(self, brand, price):
self.brand = brand
self.price = price

def apply_discount(self, discount):
self.price = self.price * discount/100

p1 = Product("Lenovo", 1200)
print(p1.brand, "original price: ", p1.price)

p1.price = 1400 #modifying price

p1.apply_discount(50) #applying discount
print("Price after discount: ", p1.price)

Conclusion

In this article, we explored object-oriented programming. We learned about classes and objects in Python, we saw how to create a class with properties and methods. We also saw how to create an instance (object) from this class and how to access the object properties and call its methods from our code.

In part one of the series we installed Python, in part two we installed VS Code and wrote our first program, in part three we learned about variables and how to read inputs from users of our application, in part four we discussed if-statements. in part five we learned about while loops, in part six we saw for loops and nested loops, in part seven we discussed lists, in part eight we explored functions.

--

--

Sari Lakkis

Professor | Consultant | Researcher. I give advice on strategy, design, and implementation of software that solves business problems to companies and startups.