Basics of Python Object-Oriented Programming

Python is a multi-paradigm programming language. Meaning, it supports different programming approaches. One of the popular approaches to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).

Java and Scala are also OOP languages. At their core, every instance in these languages is an object, and every object has attributes and behavior.

object-has

Let’s say we have an object: a BMW car. Attributes of this car include the year of manufacture, maximum speed, the number of airbags, etc. The behavior of the car could be the movement of the speedometer, change in speed, etc. 

Enroll for the Python Training Course to get trained in one of the top object-oriented programming language. Check out the course preview now!

A class is a blueprint for the object. Each object is an instance of some class. It is a collection of similar objects.

car-example

We are using PyCharm IDE here to implement the code. To learn how to install PyCharm and code with it, click here.

Below is a simple example of how to define a class and create an instance of that class.

class car: #Defining a class

   def getSpeed(self):

       print("155 mph")

BMW = car()#Creating an instance of class

FORD = car()

car.getSpeed(BMW)

car.getSpeed(FORD)

BMW.getSpeed()

FORD.getSpeed()

pycharm-projects

For creating an instance of any class, we use braces (curly brackets) along with the class name. Here, the self is used as a reference variable, which refers to the current class object. It is always the first argument in the function definition. However, using self is optional in the function call. All classes create objects, and all objects contain characteristics called attributes. 

We use the __init__() method to initialize an object’s initial attributes by giving them their default value (or state). This method must have at least one argument as well as the self variable, which refers to the object itself.

class car: 

   def __init__(self, year, speed):

       self.year=year

       self.speed=speed

   def getSpeed(self):

       print("maximum speed is: ",self.speed)

BMW = car(2018, 155)

FORD = car(2016, 140)

BMW.getSpeed()

FORD.getSpeed()

pycharm-2

Encapsulation is an important aspect of object-oriented programming. It is used to restrict access to methods and variables. In encapsulation, the code and data are wrapped together within a single unit from being modified by accident. In the code below, all the functions defined in the class are encapsulated and cannot be changed.

class car:

  def __init__(self, year, speed):

      self.year=year

      self.speed=speed

  def getSpeed(self):

      print("maximum speed is: ",self.speed)

  def setSpeed(self,speed):

      self.speed=speed

BMW = car(2018, 155)

FORD = car(2016, 140)

BMW.getSpeed()

BMW.setSpeed(143)

BMW.getSpeed()

FORD.getSpeed()

pycharm-3

Inheritance is a critical feature of object-oriented programming. It refers to defining a new class with little or no modification to an existing class. The new class is a derived (or child) class and the one from which it inherits is called the base (or parent) class.

class car

Sedan and SUV are the two child classes that are inheriting the properties of the parent class.

pycharm-4

Polymorphism is a feature that lets you use the same function in multiple ways.

parent-class

In the above example, we have created an instance of both child classes looping through the objects. By doing this, we polymorphed the definition to do two different things, depending on what our child's class is.

Want to get skilled in working with Python classes and files? Then check out the Python Training Course. Click to enroll now!

Conclusion

This tutorial demonstrates how to do Object-Oriented Programming in Python. It covered the concept of objects and classes and how to define and create them. This blog also gave you an idea about the different features of OOP, such as inheritance, encapsulation, and polymorphism. To learn more about object-oriented programming, watch the following video Object-Oriented Programming in Python:

To master Python programming, enroll in our Python Training Course.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.