1 of 29

OBJECT ORIENTED PROGRAMMING IN PYTHON

2 of 29

3 of 29

4 of 29

5 of 29

Creating Classes

  • Syntax:

  • Example:

5

6 of 29

  • Self:
  • The word 'self' is used to represent the instance of a class. By using the "self" keyword we access the attributes and methods of the class in python.
  • __init__ method:
  • "__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.

7 of 29

��Find out the cost of a rectangular field with breadth(b=120), length(l=160). It costs x (2000) rupees per 1 square unit

class Rectangle:    

def __init__(self, length, breadth, unit_cost=0):        

self.length = length        

self.breadth = breadth      

 self.unit_cost = unit_cost    

def get_area(self):      

  return self.length * self.breadth    

def calculate_cost(self):        

area = self.get_area()      

 return area * self.unit_cost

# breadth = 120 units, length = 160 units, 1 sq unit cost = Rs 2000

r = Rectangle(160, 120, 2000)

print("Area of Rectangle: %s sq units" % (r.get_area()))

8 of 29

Example

8

9 of 29

Creating Objects

9

10 of 29

10

11 of 29

Access Specifiers

  • Used to expose or hide the attribute and method of a class
  • Specifies the access level for class members

Type

Naming

private

__name (double underscore)

protected

_name

public

name

12 of 29

13 of 29

14 of 29

15 of 29

Class Inheritance

  • Instead of starting from scratch, you can create a class by deriving it from a preexisting class
  • The child class inherits the attributes of its parent class
  • Syntax:

15

16 of 29

Example:

16

17 of 29

Multilevel inheritance

class sam1:

def get(self):

print("base1")

self.a=int(input());

class sam2:

def disp(self):

print("base2")

self.b=int(input());

class sam3(sam1,sam2):

def display(self):

print(self.a,self.b);

s=sam3();

s.get();

s.disp();

s.display();

18 of 29

Overriding Methods

  • Override parent class methods.
  • Reason – To give special or different functionality in subclass

18

19 of 29

Static Method

  • Syntax:

class C():

@staticmethod

def fun(arg1, arg2, ...):

...

returns:

  • a static method for function fun. A static method is also a method which is bound to the class and not the object of the class
  • A static method can’t access or modify class state
  • It is present in a class because it makes sense for the method to be present in class

20 of 29

Static method Example

class sample:

e=10;

def get(self):

self.a=int(input("Enter No1"));

@staticmethod

def disp():

print("Static method",sample.e);

s=sample();

s.get();

sample.disp();

s.disp();

21 of 29

Class Method

A class method receives the class as implicit first argument, just like an instance method receives the instance�Syntax:

class C(object):

@classmethod

def fun(cls, arg1, arg2, ...):

  • A class method is a method which is bound to the class and not the object of the class.
  • They have the access to the state of the class as it takes a class parameter that points to the class and not the object instance.
  • It can modify a class state that would apply across all the instances of the class. For example it can modify a class variable that will be applicable to all the instances.

22 of 29

Class Method

class Kls(object):

no_inst = 0

def __init__(self):

Kls.no_inst = Kls.no_inst + 1

@classmethod

def get_no_of_instance(cls):

return cls.no_inst

ik1 = Kls()

ik2 = Kls()

print (ik1.get_no_of_instance())

print (Kls.get_no_of_instance())

23 of 29

Class method vs Static Method

  • A class method takes cls as first parameter while a static method needs no specific parameters.
  • A class method can access or modify class state while a static method can’t access or modify it.
  • In general, static methods know nothing about class state. They are utility type methods that take some parameters and work upon those parameters. On the other hand class methods must have class as parameter.
  • We use @classmethod decorator in python to create a class method and we use @staticmethod decorator to create a static method in python.

24 of 29

25 of 29

26 of 29

27 of 29

28 of 29

29 of 29

Thank you