🛠️ Scheduled Maintenance | We’ll be undergoing scheduled maintenance and upgrades between 00:00 PST Jan 26th until 00:00 PST Jan 28th. There may be brief interruption of services in that period. We apologize for the inconvenience.


The Concept of Object-Oriented Programming in Python

Python Tutorials 6 min read
object-oriented programming
Blog / Python Tutorials / The Concept of Object-Oriented Programming in Python

With object-oriented programming, you have the freedom to represent real-world objects in your code which can be very useful. In this article, we will explain what it is, and this will enable you to handle more advanced programming concepts. However, if you are not accustomed to working with lists, tuples, dictionaries and loops you may find it difficult to keep up with the pace of this article. Hopefully, the linked articles will shed some light on these subjects. Or, if you’re a complete Python beginner starting from scratch, read our Complete Python Programming Guide and come back to this article afterwards.

What are Objects in Object-Oriented Programming?

We can say every value in Python is an object. Integers, floats, strings, lists – they are all objects. Think of them as logical objects.

Values are logical objects, object-oriented-programming

Therefore, object-oriented programming refers to the concept of interacting with one or more objects.

Which Languages Support Object-Oriented Programming?

Today, there are many languages supporting OOP – Java, PHP, Python, and C++, to name a few. The reason programming has evolved to this level is conceptual – objects allow us to model real-world concepts.

data science training

A few Examples of Objects

We can have a list containing the exact number of visitors in a gallery for March.

A list of visitors in march, object-oriented programming

This is a list containing 31 data points, given that March has 31 days.

Or we could enter names of the galleries as string values in a dictionary, called “Galleries”.

Creating a dictionary called galleries, object-oriented programming

An object contains data, such as a number or a string, plus the operations allowing us to manipulate the data.

What are Classes in Object-Oriented Programming?

The next thing we’ll do is introduce the concepts of classes, attributes, and methods. Each object belongs to some class, defining the rules for creating that object. And, we can attach a certain number of attributes to it.

Class defines rules to the object, object-oriented programming

Let’s use a metaphoric example that will make things much clearer.

Assume that Uncle Archibald can make bikes. So, he belongs to the “bike-makers” class. He knows the rules for creating a bike.

Archibald belongs to the bike makers class, object-oriented programming

Attributes in Object-Oriented Programming

Let’s say he made a bike. Therefore, it is an object of the “Bike-Makers” class. As an object, it has certain attributes that refer to the state of that object. Its color, size, and type (mountain bike or road bike) are attributes we can assign to it.

Assign attributes to the class, object-oriented programming

Methods in Object-Oriented Programming

A method is something different – it is a consequential logic sequence that can be applied to the object. It could be an operation like .turn_left(), turn_right(), slow_down(), or .accelerate().

Actions are applied to the object, object-oriented programming

Note: While programming, the focus should be on the object and not on the action, as it is in other types of programming.

How to Work with Objects?

Let’s provide a practical example that will help you visualize these concepts even better.

We can create a list with 3 floats. This will be our object. Sometimes, people will refer to objects with the word instance, or they may say properties instead of attributes. This is fine as we can use these terms interchangeably. The object we created belongs to the Python’s “List” class.

The object belongs tot he list class

One attribute of this list is the type of data in it. Technically, a list can contain various types of data, such as integers and strings. So, it is important to define the type of data used here. These are floats.

The type of data is floats

A method we could apply to this object is .extend() or .index().

Apply the .extend() method

Important:  These operations can be performed only after we have created the object.

What’s the Difference Between Functions and Methods?

The difference between a function and a method is subtle and can be confusing. Try to remember that a method is a special function. As with any other function, a method can have many parameters of various data types, but it will surely contain the object it is applied to as one of them. A method belongs to a certain class, while a function exists on its own.

Methods belong to a certain clas, functions exist on their own

Looking back at our “bike-makers” example, if there isn’t a bike that’s been made, there is no way to apply the .turn_left() method. One of the parameters of the .turn_left() method must be a bike object.

A Way to Distinguish Them

To avoid confusion between the terms, method and function, the Pythonic syntax is different in the two situations. The method name is not just succeeded by parentheses. It is written after the name of the object it is applied to and the dot operator.

The method name is written after the name of the object it is applied to

Why Do We Need Object-Oriented Programming?

To sum up, when you want to turn a real-life object into a few lines of code that make sense, object-oriented programming will assist you in doing that in an elegant way. Being able to pack all the attributes and methods of a certain object into a class may well result in you writing less code in the future.  The concept of object-oriented programming will definitely help you on your way to becoming a solid programmer.

So, if you want to expand your knowledge further, you can jump onto the unknown world of modules.

If you’re enthusiastic about boosting your Python knowledge, check out our super practical tutorials!

Earn your Data Science Degree

Expert instructions, unmatched support and a verified certificate upon completion!

Leave a Reply

Your email address will not be published.