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

PPTX
Python OOPs
PPTX
Object oriented programming in python
PPTX
Basics of Object Oriented Programming in Python
PPSX
python Function
PPTX
Chapter 07 inheritance
PPTX
Python oop third class
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPTX
Object oriented programming with python
Python OOPs
Object oriented programming in python
Basics of Object Oriented Programming in Python
python Function
Chapter 07 inheritance
Python oop third class
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Object oriented programming with python

What's hot (20)

PDF
Python programming : Strings
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PPTX
C# Inheritance
PPTX
PPTX
Static Data Members and Member Functions
PPTX
Classes and Objects in C#
PPS
Introduction to class in java
PDF
Python unit 3 m.sc cs
PDF
Python programming : Inheritance and polymorphism
PPTX
Functions in Python
PPTX
Functions in python
PPTX
Chapter 05 classes and objects
PDF
Django Introduction & Tutorial
PPTX
6-Python-Recursion PPT.pptx
PDF
Object oriented approach in python programming
PPTX
Classes objects in java
PDF
Python programming : Classes objects
PDF
What is Python? | Edureka
PPTX
Python 101: Python for Absolute Beginners (PyTexas 2014)
PDF
Generics
Python programming : Strings
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
C# Inheritance
Static Data Members and Member Functions
Classes and Objects in C#
Introduction to class in java
Python unit 3 m.sc cs
Python programming : Inheritance and polymorphism
Functions in Python
Functions in python
Chapter 05 classes and objects
Django Introduction & Tutorial
6-Python-Recursion PPT.pptx
Object oriented approach in python programming
Classes objects in java
Python programming : Classes objects
What is Python? | Edureka
Python 101: Python for Absolute Beginners (PyTexas 2014)
Generics
Ad

Similar to Python – Object Oriented Programming (20)

PPT
Introduction to Python - Part Three
PPT
Python3
PPT
Lecture topic - Python class lecture.ppt
PPT
Lecture on Python class -lecture123456.ppt
PDF
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
PDF
Dive into Python Class
PPT
Python session 7 by Shan
PPTX
python.pptx
PDF
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
PPTX
Python_Object_Oriented_Programming.pptx
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PPTX
classes and objects of python object oriented
PDF
Python - object oriented
PPTX
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
PPT
PHP - Introduction to Object Oriented Programming with PHP
PDF
Class and object in C++ By Pawan Thakur
PPTX
Basic_concepts_of_OOPS_in_Python.pptx
PPT
Class and object in C++
Introduction to Python - Part Three
Python3
Lecture topic - Python class lecture.ppt
Lecture on Python class -lecture123456.ppt
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Dive into Python Class
Python session 7 by Shan
python.pptx
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Python_Object_Oriented_Programming.pptx
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
classes and objects of python object oriented
Python - object oriented
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
PHP - Introduction to Object Oriented Programming with PHP
Class and object in C++ By Pawan Thakur
Basic_concepts_of_OOPS_in_Python.pptx
Class and object in C++
Ad

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Approach and Philosophy of On baking technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PDF
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Understanding_Digital_Forensics_Presentation.pptx
A Presentation on Artificial Intelligence
20250228 LYD VKU AI Blended-Learning.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Spectral efficient network and resource selection model in 5G networks
Approach and Philosophy of On baking technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
Teaching material agriculture food technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
Empathic Computing: Creating Shared Understanding

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