0% found this document useful (0 votes)
6 views

OOP

Object-Oriented Programming (OOP) in Python allows developers to create modular and scalable applications using concepts like classes, objects, inheritance, encapsulation, polymorphism, and abstraction. Key elements include classes as blueprints for objects, inheritance for code reusability, and encapsulation for data protection. Additionally, message passing facilitates communication between different program parts, particularly in concurrent systems.

Uploaded by

mohsulai2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOP

Object-Oriented Programming (OOP) in Python allows developers to create modular and scalable applications using concepts like classes, objects, inheritance, encapsulation, polymorphism, and abstraction. Key elements include classes as blueprints for objects, inheritance for code reusability, and encapsulation for data protection. Additionally, message passing facilitates communication between different program parts, particularly in concurrent systems.

Uploaded by

mohsulai2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python OOPs Concepts

Object Oriented Programming is a fundamental concept in Python, empowering


developers to build modular, maintainable, and scalable applications. By
understanding the core OOP principles—classes, objects, inheritance, encapsulation,
polymorphism, and abstraction—programmers can leverage the full potential of
Python’s OOP capabilities to design elegant and efficient solutions to complex
problems.

What is Object-Oriented Programming in Python?


In Python object-oriented Programming (OOPs) is a programming paradigm that
uses objects and classes in programming. It aims to implement real-world entities
like inheritance, polymorphisms, encapsulation, etc. in the programming. The main
concept of object-oriented Programming (OOPs) or oops concepts in Python is to
bind the data and the functions that work together as a single unit so that no other
part of the code can access this data.

OOPs Concepts in Python


 Class in Python
 Objects in Python
 Message Passing

 Encapsulation in Python
 Inheritance in Python
Python Class
A class is a collection of objects. A class contains the blueprints or the prototype
from which the objects are being created. It is a logical entity that contains some
attributes and methods.
To understand the need for creating a class let’s consider an example, let’s say you
wanted to track the number of dogs that may have different attributes like breed,
and age. If a list is used, the first element could be the dog’s breed while the second
element could represent its age. Let’s suppose there are 100 different dogs, then
how would you know which element is supposed to be which? What if you wanted to
add other properties to these dogs? This lacks organization and it’s the exact need
for classes.

Some points on Python class:


 Classes are created by keyword class.
 Attributes are the variables that belong to a class.
 Attributes are always public and can be accessed using the dot (.) operator.
Eg.: Myclass.Myattribute
Python Objects
In object oriented programming Python, The object is an entity that has a state and
behavior associated with it. It may be any real-world object like a mouse, keyboard,
chair, table, pen, etc. Integers, strings, floating-point numbers, even arrays, and
dictionaries, are all objects. More specifically, any single integer or any single string
is an object. The number 12 is an object, the string “Hello, world” is an object, a list
is an object that can hold other objects, and so on. You’ve been using objects all
along and may not even realize it.
An object consists of:
 State: It is represented by the attributes of an object. It also reflects the
properties of an object.
 Behavior: It is represented by the methods of an object. It also reflects the
response of an object to other objects.
 ID: It gives a unique name to an object and enables one object to interact
with other objects.

Example: a rectangle object


r1 = Rectangle (25, 15)
Python Inheritance
In Python object-oriented Programming, Inheritance is the capability of one
class to derive or inherit the properties from another class. The class that
derives properties is called the derived class or child class and the class from
which the properties are being derived is called the base class or parent
class.

The benefits of inheritance are:

 It represents real-world relationships well.


 It provides the reusability of a code. We don’t have to write the same code
again and again. Also, it allows us to add more features to a class without
modifying it.
 It is transitive in nature, which means that if class B inherits from another
class A, then all the subclasses of B would automatically inherit from class A.

Types of Inheritance

 Single Inheritance: Single-level inheritance enables a derived class to


inherit characteristics from a single-parent class.
 Multilevel Inheritance: Multi-level inheritance enables a derived class to
inherit properties from an immediate parent class which in turn inherits
properties from his parent class.
 Hierarchical Inheritance: Hierarchical-level inheritance enables more than
one derived class to inherit properties from a parent class.
 Multiple Inheritance: Multiple-level inheritance enables one derived class
to inherit properties from more than one base class.

We can organize classes into a hierarchy when they have some common features.
This will allow us to derive new classes from existing classes.
A class, from which another class is derived, is called a super class or parent class.
A new class, which is derived from an existing class, is called a sub class or child
class. A sub class inherits all the data and operations/methods of the super class. In
addition, a sub class can have its own data and operations/methods.
Sub classes have an “is-a-kind-of” relationship with the super class. e.g. part time
employee is a kind of an employee.
Python Encapsulation
In Python object oriented programming, Encapsulation is one of the
fundamental concepts in object-oriented programming (OOP). It describes the
idea of wrapping data and the methods that work on data within one unit.
This puts restrictions on accessing variables and methods directly and can
prevent the accidental modification of data. To prevent accidental change, an
object’s variable can only be changed by an object’s method. Those types of
variables are known as private variables.
A class is an example of encapsulation as it encapsulates all the data that is
member functions, variables, etc.
Message Passing in python
Message passing in Python refers to the method of communication between
different parts of a program, especially in concurrent or distributed systems.
There are several ways to implement message passing in Python:

1. Using Queues with the multiprocessing module: This is useful for


communication between processes.
2. Using Queues with the queue module: This is useful for communication
between threads.
3. Using the socket library: This is for network communication.
4. Using asyncio: This is for asynchronous message passing within a single
thread.

You might also like