The core elements of OOP are classes, objects, methods, and attributes. OOP uses these elements to achieve fundamental aims: encapsulation, abstraction, inheritance, and polymorphism.
Python has excellent support for object-oriented programming. If you’re wondering about OOP or how to use it in Python, read on for the details.
What Is Object-Oriented Programming in Python?
Python is a general-purpose programming language that supports object-oriented programming. Its central infrastructure aims at object and class-building applications or designing. Using OOP makes Python code cleaner and clearer. It also provides for easier maintenance and code reuse.
Here is an example to show you why to use OOP in Python.
In this example, jeans contain a list of values representing price, whether the item is on sale, its material, and cost. This is a non-OOP approach and causes some problems. For example, there is no explanation that jeans[0] refers to size. This is highly unintuitive compared to an OOP approach which would refer to a named field: jeans.size.
This example code isn’t very reusable since the behavior it relies on isn’t discoverable. Using OOP, you can create a class to avoid this problem.
How to Define a Class in Python
To create a class in Python, use the keyword “class” followed by your chosen name. Here’s an example defining a class named Myclass.
Let’s define a class, Pant, to represent various types of pants. This class will contain the size, on-sale status, material, and price. By default, it initializes those values to None.
How to Create an Object in Python
Now let’s initialize an object from the Pant class. First, we’ll define the initializer method which has the predefined name init. Once you define it, Python will automatically call this method whenever you create an object from that class.
Secondly, a particular self parameter will allow the initialize method to select a new object.
Finally, after defining the initializer, we’ll create an object named jeans, using the [objectName] = Pant() syntax.
Use Attributes and Methods to Define Properties and Behaviors
Objects in Python can use two different types of attribute: class attributes and instance attributes.
Class attributes are variables or methods that all objects of that class share. In contrast, instance attributes are variables that are unique to each object—the instance of a class.
Let’s create an instance method to interact with object properties.
Here, the first method, printinfo(), uses all properties except onsale. The second method, putonsale(), sets the value of the onsale property. Note how both these instance methods use the keyword self. This refers to the particular object (or instance) used to invoke the method.
When we call putonsale(), this method uses the self parameter to change the value of that specific object. If you’d created another instance of Pant—leggings, for example—this call would not affect it. An instance property of one object is independent of any others.
How to Use Inheritance in Python
You can expand the above example by adding a subcategory of the Pant class. In object-oriented programming, this is known as inheritance. Expanding the class will define extra properties or methods that are not in the parent class.
Let’s define Leggings as a subclass and inherit it from Pant.
The new initializer method takes all the same properties as the Pant class and adds a unique elasticity property. You can extend a class to reuse existing functionality and reduce code length. If your Leggings class did not inherit from the Pant class, you would need to reproduce existing code, just for a small change. You’ll notice these benefits more when you work with larger and more complicated classes.
Check if an Object Inherits From a Class With isinstance()
The isinstance() function checks if an object is an instance of a specific class or any of its ancestor classes. If the object is of the given type, or a type that inherits from it, then the function returns True. Otherwise, it’ll return False.
Note that Python considers the leggings object, of type Leggings, an instance of Pant, the parent class of Leggings. But a pants object is not an instance of the Leggings class.
Python Is the Perfect Introduction to OOP
Python has gained popularity because of its simple and easy-to-understand syntax compared with other programming languages. The language was designed around object-oriented programming, so it’s a great tool to learn the paradigm. Most large tech companies make use of Python at some point in their technology stack, and the prospects look good for Python programmers.
If you are looking forward to making a career in the development sector, then Python is an excellent place to start. Make sure you have a solid understanding of the basic principles—object-oriented programming is just the beginning!