Menu Close

Python Object Oriented Programming (OOPs)

Python Object Oriented Programming

In this article, you will learn all about Python Object Oriented Programming (OOP) concepts along with their fundamental concepts using examples. At the end of this article, you will clear about Python Object Oriented programming concepts.

This is our first tutorial on Python Object Oriented Programming. You can start your Python OOPs journey from here.

Python Object Oriented Programming (OOP)

Like other programming languages. Python is also a high-level, interpreted general purpose object oriented programming language.

Object Oriented Programming in Python is one of the most popular approaches for solving programming problems. So, Python allows us to develop applications using an object-oriented approach.

An Object has mainly two characteristics:-

  • attribute
  • method

Let’s take example to understand object.

In real life, A car can be a object and it has following properties.

  • name, color, price attributes.
  • start, stop, are the methods.

The concept of Python Object Oriented Programming is creating reusable code. This concept is also known as DRY ( Don’t Repeat Yourself ).

Major principles of Python Object Oriented Programming are given below.

  • Class
  • Object
  • Methods
  • Inheritance
  • Encapsulation
  • Data Abstraction
  • Polymorphism

Class

A class is a blue print of creating object.

You can relate the class to the sketch or prototype of the car. A sketch or prototype of the class may include all the attributes of the car such as name, color, price, start, stop, etc.

You can create more than one object using a class, such as using a sketch of a car to create more than one car.

Syntax of creating class in Python.

class Car:
	pass

In the above example, we have used the class keyword to create an empty class. You can learn about the class in our Python class tutorial.

Object

An object or instance is the instantiation of the class. When class is defined, only a description or blueprint of the object is created, Therefore not memory location allocate in the system for that class.

An object has its own attributes and methods.

Example of creating an object for class Car.

c1 = Car()

In above example, c1 is the object of class Car.

Let’s see understand the class and object in one example.

Example:

class Car:
	def __init__(self, name, color, price):
		self.car_name = name
		self.car_color = color
		self.car_price = price
		
#create object or instance of the class.
c1 = Car('BMW', 'Black', '50 Lakh')
c2 = Car('Audi', 'White', '40 lakh')


#access attribute of object or instance
print("Car name is {}. Car color and price are {}, {}.".format(c1.car_name, c1.car_color, c1.car_price))
print("Car name is {}. Car color and price are {}, {}.".format(c2.car_name, c2.car_color, c2.car_price))

Output

Car name is BMW. Car color and price are Black, 50 Lakh.
Car name is Audi. Car color and price are White, 40 lakh.

In the above example, we have created a class named Car and assigned some attributes.
These attributes are assigned inside the __init__ method of the class. This is a special method in Python that is automatically called when the object or instance of the class created.

When the object is created, the values during object creation are assigned in car_name, car_color, and car_price attributes.

Methods

Methods are the functions define inside the body of the class. Methods take the first parameter self implicitly. They define the behavior of the object.

Example:

#Creating methods in Python.
class Car:
	def __init__(self, name, color, price):
		self.car_name = name
		self.car_color = color
		self.car_price = price
				
	def details(self):
		print("Car name is {}. Car color and price are {}, {}.".format(self.car_name, self.car_color, self.car_price))
		
		

#create object or instance of the class.
c1 = Car('BMW', 'Black', '50 Lakh')
c2 = Car('Audi', 'White', '40 lakh')

#access the method using object.
c1.details()
c2.details()

Output

Car name is BMW. Car color and price are Black, 50 Lakh.
Car name is Audi. Car color and price are White, 40 lakh.

Inheritance

Inheritance is one of the most important concepts of the Object-oriented Programming language. Inheritance provides the facility to create a new class using the existing class without modifying it.

The newly derived class is called the child class. Similarly, the existing class is called base or parent class.

let’s understand Python inheritance through simple example.

Example:

class Car:
	def __init__(self, name, color, price):
		self.car_name = name
		self.car_color = color
		self.car_price = price
		
		
class BMW(Car):
    def __init__(self, name, color, price):
        super().__init__(name, color, price)

    def details(self):
        print("Car name is {}. Car color and price are {} and {}.".format(self.car_name, self.car_color, self.car_price))
			
#create object or instance of the class.
b = BMW('BMW 2 series', 'Black', '42 Lakh')

#access the method using object.
b.details()

Output

Car name is BMW 2 series. Car color and price are Black and 42 Lakh.

Encapsulation

Encapsulation is another most important aspect of Python Object Oriented Programming. Python Encapsulation is used to restrict the access of the attributes of the class by using a single prefix underscore ( _ ) or double underscore ( __ ).
Python encapsulation prevents the direct modification of the class attribute.

Example:

class Laptop:
	def __init__(self):
		self.__price = 45000
		
	def sell(self):
		print("The selling price is:- {}".format(self.__price))
		
	def setPrice(self, price):
		self.__price = price
				
#creating the object or instance
l1 = Laptop()

#access selling price
l1.sell()

#change the values
l1.__price = 50000
l1.sell()

#change the value using setPrice
l1.setPrice(50000)
l1.sell()

Output

The selling price is:- 45000
The selling price is:- 45000
The selling price is:- 50000

Data Abstraction

Data Abstraction is the process of hiding the implementation of the program from the user and shows only features to the user.

The best example of data abstraction is, When you buy a phone, Then you don’t need to know what works internally, you need to know only the functionality of the phone.

Polymorephism

Polymorphism contains two words “Poly” and “morphism“. “Poly” means many and “morphism” means many forms. In Python, Polymorphism means a single name but a different task.

For example Python len() function is used to find the length of the string as well as Python iterable.

Example:

print(len('Programming Funda'))
print(['Python','Java','C++','C'])

Output

17
['Python', 'Java', 'C++', 'C']

Conclusion

In this article, You have learned all about Python Object Oriented programming concepts. In a later article, we will learn all Python Object Oriented Programming ( OOPs ) concepts separately along with their examples.

If you want to become a strong Python programmer, Then you should have knowledge of Python Object Oriented Programming and its concepts.
If this article helped you, please share and keep visiting for further Python tutorials.

For more information:- Click Here

Frequently Asked Questions About Python Object Oriented Programming


  1. What is Object Oriented Programming ( OOP )?

    Ans:- Object-Oriented Programming ( OOP ) is a way of solving programming problems by creating objects.
    Python Object Oriented Programming ( OOP ) provides the facility to create a python program bonding related attributes and properties for each object.

  2. Is Python fully Object Oriented Programming ( OOP )?

    Ans:- Python support approx all the concepts of OOPs ( Object Oriented Programming ), but it is not fully object oriented programming because code can be write without Python classes.

  3. What type of language is Python?

    Ans:- Python is interpreted, high-level, general-purpose object-oriented programming language.

  4. What are the 4 basics of OOPs?

    Ans:- Definitions of the OOPs concept in Python are given below.
    1. Abstraction
    2. Encapsulation
    3. Inheritance
    4. Polymorphism

  5. How do you use OOPs in Python?

    Ans:- Python is a widely-used programming language with OOPs features. In Python, you can create a class with attributes and methods after you can call all the attributes and methods by using the object of the class.
    In Object-Oriented Programming, each object has its own properties and methods.

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply