SlideShare a Scribd company logo
Python – Object Oriented Programming
by Raghunath Akula
Classes & Objects
 Class serves as the primary means of abstraction in Object-Oriented
programing.
 A class provides a set of behaviors in the form of member_functions /
methods, with implementation that are common to all instance.
 In class, the state information for each instance is represented in the
form of Attributes / Data members.
 Instances are objects that are created which follow the definition given
inside of the class.
 No separate class interface definition.
class MyVector:
def __init__(self, x, y):
self . x = x
self . y = y
def __add__(self, param2):
return MyVector(self.x + param2.x, self.y + param2.y)
Obj1 = MyVector(1, 2) “Object initiation for MyVector class.”
Access Specifiers
 Python provides a number of access modifiers to help you set the level
of access for attributes and methods.
 Private - 2 leading under-scores in its name, but none at the end. Can’t
be accessed outside of class
 Built-in – Methods or attributes having 2 underscores at the beginning
and the end.
 Doesn’t support ‘protected’. Subclasses would be unable to access these
private data either.
Garbage Collector
 Python has automatic garbage collection.
 Will automatically detect when all of the references to a piece of
memory have gone out of scope. Automatically frees that memory.
 There’s also no Destructor method for classes
Methods
 Define a method in a class by including function definitions within the
scope of the class block
 There must be a special first argument self in all of method definitions
which gets bound to the calling instance
 There is usually a special method called __init__ in most classes
class MyVector:
count = 0 “Class attribute”
def __init__(self, x, y):
self . x = x
self . y = y
def __add__(self, param2):
return MyVector(self.x + param2.x, self.y + param2.y)
def displayCount(self):
count = self.x + self.y
return count
Obj1 = MyVector(1, 2) “Object initiation for MyVector class.”
print Obj1.displayCount() “Calling displayCount method.”
Constructor ( __init__ )
 Serves as a constructor for a class.
 The arguments passed to the class are given to its __init__() method
def __init__(self, x, y):
self . x = x
self . y = y
self
 The first argument of every method is a reference to the current instance of
the class
 Parameter object self refers to current object being created, similar to
keyword this in Java
 Although self explicitly mentioned when defining the method, not mandatory
included while calling the method. Self parameter is passed automatically by
python.
Attributes
Two Kinds of Attributes:
(1)Data Attributes
Owned by particular instance of a class.
Each instance has its own value for it.
(2)Class Attributes
All instance inside class share the same value for it.
Similar to static variables or class-wide constants.
“Example: Data Attributes”
class MyVector:
def __init__(self, x, y):
self . x = x
self . y = y
“Example: Class Attributes”
class MyVector:
count = 0 “static variable”
def displayCount(self):
count = self.x + self.y
return count
Inheritance
 A class can extend the definition of another class
 Allows to use methods and attributes already defined in the previous
classes.
 To define a subclass, put the name of the superclass in parentheses
“(…)”.
 Python has no ‘extends’ keyword like Java. Python supports multiple
inheritance.
“Extending MyVector class”
class SubVector (MyVector):
def __init__(self, sub_x, sub_y):
self . x = sub_x
self . y = sub_y
def displayCount(self):
count = self.x + self.y
return count
Method Overriding
 Overriding is a very important part of OOP. It has the ability of a
class to change the implementation of a method provided by one of its
ancestors.
 Python inheritance works through implicit delegation – when the object
cannot satisfy a request, it forward request to its ancestors.
“Parent Class”
class MyVector:
def __init__(self, x, y):
self . x = x
self . y = y
def displayCount(self):
return self.x + self.y
“Child Class”
class SubVector (MyVector):
def displayCount(self): “Overriding dispalyCount method”
return self.x + 10
Thank you!!

More Related Content

What's hot (20)

PDF
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
PPTX
Packages In Python Tutorial
Simplilearn
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Edureka!
 
PPTX
Python oop third class
Aleksander Fabijan
 
PPTX
Types of methods in python
Aravindreddy Mokireddy
 
ODP
Python Modules
Nitin Reddy Katkam
 
ODP
Exception handling in python
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Life cycle-of-a-thread
javaicon
 
PPTX
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Object Oriented Programming Concepts using Java
Glenn Guden
 
PDF
Python's magic methods
Reuven Lerner
 
PPTX
File handling in Python
Megha V
 
PPTX
Data Structures in Python
Devashish Kumar
 
PPTX
Python dictionary
Sagar Kumar
 
PDF
Set methods in python
deepalishinkar1
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
OOP concepts -in-Python programming language
SmritiSharma901052
 
PPT
File Handling In C++(OOPs))
Papu Kumar
 
PPTX
Characteristics of OOPS
abhishek kumar
 
Python programming : Control statements
Emertxe Information Technologies Pvt Ltd
 
Packages In Python Tutorial
Simplilearn
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Edureka!
 
Python oop third class
Aleksander Fabijan
 
Types of methods in python
Aravindreddy Mokireddy
 
Python Modules
Nitin Reddy Katkam
 
Life cycle-of-a-thread
javaicon
 
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Object Oriented Programming Concepts using Java
Glenn Guden
 
Python's magic methods
Reuven Lerner
 
File handling in Python
Megha V
 
Data Structures in Python
Devashish Kumar
 
Python dictionary
Sagar Kumar
 
Set methods in python
deepalishinkar1
 
Class and Objects in Java
Spotle.ai
 
OOP concepts -in-Python programming language
SmritiSharma901052
 
File Handling In C++(OOPs))
Papu Kumar
 
Characteristics of OOPS
abhishek kumar
 

Similar to Python – Object Oriented Programming (20)

PPTX
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
PPTX
object oriented porgramming using Java programming
afsheenfaiq2
 
PPTX
Object Oriented Programming.pptx
SAICHARANREDDYN
 
PPT
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
PPT
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
PDF
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
PPTX
Python advance
Mukul Kirti Verma
 
PPTX
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
PPT
Introduction to Python - Part Three
amiable_indian
 
PDF
1_7a6f85d03f132dcd9d7592bc4643be1c_MIT6_0001F16_Lec8.pdf
ID Bilişim ve Ticaret Ltd. Şti.
 
PDF
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
PPTX
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
PPT
Python3
Ruchika Sinha
 
PPTX
OOPS 46 slide Python concepts .pptx
mrsam3062
 
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
PPTX
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
PPTX
Python Lecture 13
Inzamam Baig
 
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
object oriented porgramming using Java programming
afsheenfaiq2
 
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
Python advance
Mukul Kirti Verma
 
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
Introduction to Python - Part Three
amiable_indian
 
1_7a6f85d03f132dcd9d7592bc4643be1c_MIT6_0001F16_Lec8.pdf
ID Bilişim ve Ticaret Ltd. Şti.
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
 
Python3
Ruchika Sinha
 
OOPS 46 slide Python concepts .pptx
mrsam3062
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
Python Lecture 13
Inzamam Baig
 
Ad

Recently uploaded (20)

PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Practical Applications of AI in Local Government
OnBoard
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Kubernetes - Architecture & Components.pdf
geethak285
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Ad

Python – Object Oriented Programming

  • 1. Python – Object Oriented Programming by Raghunath Akula
  • 2. Classes & Objects  Class serves as the primary means of abstraction in Object-Oriented programing.  A class provides a set of behaviors in the form of member_functions / methods, with implementation that are common to all instance.  In class, the state information for each instance is represented in the form of Attributes / Data members.  Instances are objects that are created which follow the definition given inside of the class.  No separate class interface definition. class MyVector: def __init__(self, x, y): self . x = x self . y = y def __add__(self, param2): return MyVector(self.x + param2.x, self.y + param2.y) Obj1 = MyVector(1, 2) “Object initiation for MyVector class.”
  • 3. Access Specifiers  Python provides a number of access modifiers to help you set the level of access for attributes and methods.  Private - 2 leading under-scores in its name, but none at the end. Can’t be accessed outside of class  Built-in – Methods or attributes having 2 underscores at the beginning and the end.  Doesn’t support ‘protected’. Subclasses would be unable to access these private data either. Garbage Collector  Python has automatic garbage collection.  Will automatically detect when all of the references to a piece of memory have gone out of scope. Automatically frees that memory.  There’s also no Destructor method for classes
  • 4. Methods  Define a method in a class by including function definitions within the scope of the class block  There must be a special first argument self in all of method definitions which gets bound to the calling instance  There is usually a special method called __init__ in most classes class MyVector: count = 0 “Class attribute” def __init__(self, x, y): self . x = x self . y = y def __add__(self, param2): return MyVector(self.x + param2.x, self.y + param2.y) def displayCount(self): count = self.x + self.y return count Obj1 = MyVector(1, 2) “Object initiation for MyVector class.” print Obj1.displayCount() “Calling displayCount method.”
  • 5. Constructor ( __init__ )  Serves as a constructor for a class.  The arguments passed to the class are given to its __init__() method def __init__(self, x, y): self . x = x self . y = y self  The first argument of every method is a reference to the current instance of the class  Parameter object self refers to current object being created, similar to keyword this in Java  Although self explicitly mentioned when defining the method, not mandatory included while calling the method. Self parameter is passed automatically by python.
  • 6. Attributes Two Kinds of Attributes: (1)Data Attributes Owned by particular instance of a class. Each instance has its own value for it. (2)Class Attributes All instance inside class share the same value for it. Similar to static variables or class-wide constants. “Example: Data Attributes” class MyVector: def __init__(self, x, y): self . x = x self . y = y “Example: Class Attributes” class MyVector: count = 0 “static variable” def displayCount(self): count = self.x + self.y return count
  • 7. Inheritance  A class can extend the definition of another class  Allows to use methods and attributes already defined in the previous classes.  To define a subclass, put the name of the superclass in parentheses “(…)”.  Python has no ‘extends’ keyword like Java. Python supports multiple inheritance. “Extending MyVector class” class SubVector (MyVector): def __init__(self, sub_x, sub_y): self . x = sub_x self . y = sub_y def displayCount(self): count = self.x + self.y return count
  • 8. Method Overriding  Overriding is a very important part of OOP. It has the ability of a class to change the implementation of a method provided by one of its ancestors.  Python inheritance works through implicit delegation – when the object cannot satisfy a request, it forward request to its ancestors. “Parent Class” class MyVector: def __init__(self, x, y): self . x = x self . y = y def displayCount(self): return self.x + self.y “Child Class” class SubVector (MyVector): def displayCount(self): “Overriding dispalyCount method” return self.x + 10