Python - Object Oriented Programming
Python - Object Oriented Programming
Table of Contents
✓ class
✓ object
✓ constructor
✓ Inheritance & etc…
3. class:
3.1. Def1:
✓ A class is a model for creating an object and it does not exist physically.
3.2. Def2:
Syntax
class NameOfTheClass:
1. constructor
2. properties (attributes)
3. actions (behaviour)
• constructor
• properties
• actions
Syntax
class NameOfTheClass:
''' documentation string '''
1. Constructor
2. Variables
1. instance variables
3. Methods
1. instance methods
Note
class Employee:
def display(self):
print("Hello My name is Daniel")
output
Make a note
✓ In above program, when we run then we will not get any output because
we didn’t call display method
✓ Above program Employee represents a class which is defined by
developer.
✓ Developer defined only one method as display(self)
✓ Method we can define by using def keyword.
✓ Methods means it’s just like a functions to perform an operations
5|Page 19.Python Object Oriented program
Data Science – Python Object Oriented Programming
Kind info:
✓ Writing a class is not enough; we should know how to use the variables
and methods.
So,
6. object
Definition 1:
Definition 2:
Definition 3:
✓ An object exists physically in this world, but class does not exist.
Syntax
nameoftheobject = nameoftheclass()
Example
emp = Employee()
class Employee:
def display(self):
print("Hello my name is Daniel")
emp = Employee()
emp.display()
output
Hello my name is Daniel
class Employee:
def display(self):
print("Hello my name is Daniel")
def teaching(self):
print("I like teaching")
emp = Employee()
emp.display()
emp.teaching()
output
Hello my name is Daniel
I like teaching
Make a note
7. Constructor
Syntax
class NameOfTheClass:
def __init__(self):
body of the constructor
class Employee:
def __init__(self):
print("constructor is executed")
emp = Employee()
output
constructor is executed
✓ If we create object in two times then constructor will execute two times.
class Employee:
def __init__(self):
print("constructor is executed")
emp1 = Employee()
emp2 = Employee()
output
constructor is executed
constructor is executed
Syntax
class NameOfTheClass:
def __init__(self):
body of the constructor
class Employee:
def __init__(self):
print("constructor is executed")
emp = Employee()
output
constructor is executed
Syntax
class NameOfTheClass:
def __init__(self, parameter1, parameter2):
body of the constructor
class Employee:
def __init__(self, number):
self.number= number
print("Employee id is: ", self.number)
e1 = Employee(1)
e2 = Employee(2)
e3 = Employee(3)
output
Employee id is: 1
Employee id is: 2
Employee id is: 3
class Employee:
def __init__(self, number):
self.number = number
def display(self):
print("Employee id is:", self.number)
e1 = Employee(1)
e2 = Employee(2)
e3 = Employee(3)
e1.display()
e2.display()
e3.display()
output
Employee id is: 1
Employee id is: 2
Employee id is: 3
class Employee:
def __init__(self, number, name):
self.number = number
self.name = name
def display(self):
print("Hello my id is :", self.number)
print("My name is :", self.name)
e1=Employee(1, 'Daniel')
e1.display()
e2=Employee(2, 'Arjun')
e2.display()
Output
Hello my id is: 1
My name is: Daniel
Hello my id is: 2
My name is: Arjun
class Employee:
def __init__(self, number, name, age):
self.number = number
self.name = name
self.age = age
def display(self):
print("Hello my id is :", self.number)
print("My name is :", self.name)
print("My age is sweet :", self.age)
Output
Hello my id is: 1
My name is: Daniel
My age is sweet: 16
Hello my id is: 2
My name is: Arjun
My age is sweet: 17
Hello my id is :3
My name is: Prasad
My age is sweet: 18
Method Constructor
9. Instance variables:
class Student:
def __init__(self, name, number):
self.name=name
self.number=number
s1 = Student('Daniel', 101)
s2 = Student('Prasad', 102)
print("Studen1 info:")
print("Name: ", s1.name)
print("Id : ", s1.number)
print("Studen2 info:")
print("Name: ", s2.name)
print("Id : ", s2.number)
Output
Studen1 info:
Name: Daniel
Id: 101
Studen2 info:
Name: Prasad
Id: 102
class Employee:
def __init__(self):
self.eno = 10
self.ename = "Daniel"
self.esal = 10000
emp = Employee()
output
Employee number: 10
Employee name : Daniel
Employee salary : 10000
✓ Instance methods are methods which act upon the instance variables of
the class.
✓ Instance methods are bound with instances or objects, that’s why called
as instance methods.
✓ The first parameter for instance methods is self variable.
✓ Along with self variable it can contains other variables as well.
class Demo:
def __init__(self, a):
self.a=a
def m(self):
print(self.a)
d=Demo(10)
d.m()
Output
10
✓ Constructor
o By using self, we can initialize the instance variables inside
constructor __init__(self)
✓ Instance variable
o By using self, we can declare and access instance variables,
✓ Instance methods
o By using self, we can create instance methods.