0% found this document useful (0 votes)
10 views30 pages

LEC - Python - OOPS - SRNR

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including definitions of classes, class variables, instance variables, inheritance, and method overloading. It explains the creation and usage of classes, methods, constructors, and destructors, along with examples to illustrate these concepts. Additionally, it covers operator overloading and the differences between overloading and overriding methods in Python.

Uploaded by

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

LEC - Python - OOPS - SRNR

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including definitions of classes, class variables, instance variables, inheritance, and method overloading. It explains the creation and usage of classes, methods, constructors, and destructors, along with examples to illustrate these concepts. Additionally, it covers operator overloading and the differences between overloading and overriding methods in Python.

Uploaded by

Glory Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming- OOPS

Prof. SRN Reddy, CSE Dept, IGDTUW, Delhi


srnreddy@[Link]
OOP Terminology
• Class − It is a user defined data type that contain a set of attributes that characterize any object of
the class, data members and methods etc., All these attributes are accessed via . Operator.
A class is a template for creation of the object
• Class variable − A variable that is shared by all instances of a class. Class variables are defined
within a class
• Data member − A class variable or instance variable that holds data associated with a class and its
objects.
• Function overloading − The assignment of more than one behavior to a particular function. The
operation performed varies by the types of objects or arguments involved.
• Instance variable − A variable that is defined inside a method and belongs only to the current
instance of a class.
• Inheritance − The transfer of the characteristics of a class to other classes that are derived from it.
• Instance − An individual object of a certain class.
• Instantiation − The creation of an instance of a class.
• Method − A function that is defined in a class definition.
• Object − A unique instance of a data structure that's defined by its class. An object comprises both
data members and methods.
• Operator overloading − The assignment of more than one function to a particular operator.
Creating Classes

The class statement creates a new class definition.


class ClassName:
'Optional class documentation string’
class_suite
•The class has a documentation string, which can be accessed
via ClassName.__doc__.
•The class_suite consists of all the component statements defining
class members, data attributes and functions.
Defining Class Methods
Format:
class <classname>:
def <method name> (self, <other parameters>):
<method body>
Unlike functions, every
method of a class must
Example: have the ‘self’ parameter
class Person:
name = “ My name is :("
def sayName (self):
print ("My name is...", [Link])
When the attributes are
accessed inside the
methods of a class they
MUST be preceded by the
suffix “.self”
Creation of class and Accessing
class My_Course :
My_Course_Name= " Python Programming for [Link]"

def Show_Course(self):
print(self.My_Course_Name)
My_Object= My_Course()
My_Object.Show_Course()
My_Object.My_Course_Name=input("Entr c Name\t") # Accessing & Passing
new val to data Member
My_Object.Show_Course()
Example
Python Variables: 1 Class Variable 2. Instance Variable
[Link] Parameter Class Variable Instance Variable
1 Sharing the Value It is shared among all objects of Unique to every instance of the class
the class
2 Place of definition Outside of the any class method inside of the any class method using self
keyword
Usually defined inside the constructor
3 Use of special No need Need “self ” keyword
keyword
4 How to call Name of the class they belong to Name of the instance/ object

5 Example empyCount Name and Salary


Which are the instance and class variables of the following code
Example
‘Self’ Parameter
• When defining/calling methods of a class there is always at least one
parameter.
• This parameter is called the ‘self’ reference which allows an object
to access attributes inside its methods.
• Functions that are associated with classes are referred to as methods
Local Variables and Constructor
• The variable empCount is a class variable whose value is shared
among all instances of a this class. This can be accessed
as [Link] from inside the class or outside the class.
• The first method __init__() is a special method, which is called class
constructor or initialization method.
• Python calls when you create a new instance of this class.
• You declare other class methods like normal functions with the
exception that the first argument to each method is self. Python
adds the self argument to the list for you;
• you do not need to include it when you call the methods.
Constructor: A Special Method
• Constructor method: a special method that is used when defining a
class and it is automatically called when an object of that class has
been created.
• E.g., aPerson = Person() # This calls the constructor

• In Python this method is named ‘init’.


• This method should never have a return statement that returns a
value.
Destructor

• It destroys all the objects and the values of the variables stored

• It works opposite to the Constructor

• In python, it is automatically calls the destructor

• A destructor also begins and ends with two underscores(__) with del
Example for destructor
Built-In Class Attributes
Built-in attributes can be accessed using dot operator like any other
attribute −
• __dict__ − Dictionary containing the class's namespace.
• __doc__ − Class documentation string or none, if undefined.
• __name__ − Class name.
• __module__ − Module name in which the class is defined. This attribute is
"__main__" in interactive mode.
• __bases__ − A possibly empty tuple containing the base classes, in the order
of their occurrence in the base class list.
Built in Attributes
Class Inheritance
create a class by deriving it from a preexisting class by listing the parent class in
parentheses after the new class name.
The child class inherits the attributes of its parent class, and you can use those attribut
as if they were defined in the child class.
A child class can also override data members and methods from the parent.
Derived classes are declared much like their parent class; however, a list of base classe
to inherit from is given after the class name −
Syntax:-
class SubClassName (ParentClass1[, ParentClass2, ...]):
'Optional class documentation string' class_suite
Example: Note that Constructor will be called automatically
Example2: Note that Constructor will be called automatically
Multiple Inheritance
class AA: # Defines Class AA
…..

Class BB: # Defines Class BB


…….

Class CC(AA,BB): # Defines Class CC with multiple inheritance


Overriding Methods
• Overloading is the ability of a function or operator to behave
differently depending on the parameters passed on to the function or
the operands on which the operator operates
• Method overloading or function overloading is a type of
polymorphism in which we can define a number of methods with the
same name but with a different number of parameters as well as
parameters can be of different types
• We can override parent class methods.
• We override parent's methods for special or different
functionality in subclass
Overriding
Method overriding
Issues of Inheritance and Overriding
Issue1: Need to call base call method in the derived class having the
same name

Solution: Call by class name or user super()

Issue2: Need to call base call constructor in the derived class

Solution: Call by class name or user super()


Calling Base Class Method in Derived class Method along with overriding

Method overriding and calling base class method


Calling Base Class Method in Derived class Method and super ()
call base class constructor in the derived class by class name or super()
OPERATOR OVERLOADING
class AA:
def My_fun1_disp(self, x, y):

print(x+y)

AA().My_fun1_disp(2,5)
AA().My_fun1_disp(2.5,5.5)

AA().My_fun1_disp(" AI CLASS HAS BEGAN", " @ IGDTUW BY SRN REDDY" )


[Link] Overloading Overriding

1 Compile Time Runtime

2 More than one method of the same Specific implementation of the method
class shares the same method name that is already provided by the parent
having different signatures class is provided by the child class.

3 It is used to add more to the behavior Need of more than one class for method
of methods and there is no need of overloading
more than one class for method
overloading
Method Overloading

You might also like