Python: Object oriented programming, RTS Tech. Indore
This is a presentation to take your skills to next level. Hope you will like our work to make programming easier for you.
Feel free to contact for the online/offline batches.
4. Application
• One application is made of multiple modules
• A Module is a file with .py extension.
• A module can define functions, classes and
variables.
RTS tech
6. 6
Student.py
Following is Student module which contains two methods add()
and update( )
o def add_student( rollno, Name ):
o print("Adding Student")
o print('Name', Name )
o print(‘Rollno', rollno )
o return
o def update_student(Name ):
o print("Updating Student")
o print('Name', Name )
o return
RTS tech
7. 7
• You can import a module by import statement
in python program. We are importing
Student.py module in TestStudent.py file.
o import Student
• Now we can use module functions:
o Student.add_student(1,”Sonu”)
o Student.add_student(2,”Monu”)
TestStudent.py
RTS tech
8. from ... Import statement
We can import specific functions from a module using
from ...import statement.
o from Student import add,update
o add(1,"Ram")
o update("Shyam")
You can import all methods using * character
from Student import *
RTS tech
9. Module search path
When you import a module, python interpreter searches
module in different directories in the following sequences:
The current directory.
If module is not found then searches each directory specified
in environment variable PYTHONPATH.
If all fails then python checks the default path.
You can set PYTHONPATH as per your app requirements
set PYTHONPATH =c:pythonlibl;c:pythonlib2;
RTS tech
10. Objects: Real world Entities
Object=Related attributes+ Related methods
RTS tech
11. Objects vs Class
Attributes
• Color
• Brand
• Price
• …
Method
• take_Selfie()
• Calling()
• Messaging()
RTS tech
Virtual Entity Actual Entity
12. Class
It is a structure of an Object.
Class contains related attributes and methods of an
Object.
Class keyword is used to define a class.
Class contains the complete details about an object.
Ex.
Home
Pen
Chair
RTS tech
13. Object
• When we create a class only description is created no
memory is allocated.
• We can create an variable of the class.
• Variable is known as object(instance).
• We can create multiple object of the class.
RTS tech
14. Define a class
‘class’ keyword is used to define the class.
Class Mobile:
‘contains all details about mobile’
The first line after the class name is a document String.
Class Members:
Attributes
Methods
Constructor
Destructor
RTS tech
15. Mobile
class Mobile:
"contains mobile information“
#class variables are shared by all instance
object_count=0
def __init__(self):#constructor
self.color = "" #instance attribute
self.brand=“”
Mobile.object_count+=1
def get_color(self): #instance Method
return self.color
def set_color(self,color):
self.color = color
RTS tech
16. TestMobile.py
Create Object(variable) of Mobile class.
We can create multiple object of the class.
m1=Mobile()
m2=Mobile()
m1.set_color("black")
Print Value of the Mobile
print("Count",Mobile.object_count)
print("color: ",m1.get_color())
RTS tech
17. Built in Class Attributes
__doc__: to get doc string of the class.
__dict__: to get dictionary of the class members.
__bases__: to get base classes tuple.
__name__: to get class name.
__module__: to get module name.
RTS tech
19. Class(static) attributes
Class attributes are declared within a class but outside
method.
class Mobile:
object_count=0
It is shared by all instance of the class.
Static variables got memory one time.
We can access class variable by class name.
Mobile.object_count+=1
RTS tech
20. Instance (non-static) Attributes
Instance variables are define inside the constructor.
def __init__(self):#constructor
self.color =""
self.brand=“”
Instance variable get memory for each instance
separately.
We can access instance variable with the instance name.
RTS tech
21. Constructor
Constructor is defined by __init__(self) method.
Each class has only one constructor method.
Constructor is used to initialize the class and instance
variable.
It is executed at the time of object creation(instantiation
process).
self keyword represent current instance.
RTS tech
22. Methods in a class
Method is used to write business logic.
Method is define by def keyword.
Instance method contains a self attribute to access
instance variable.
Class method does not require self keyword.
We can access class method with the help of class name
class Mobile:
def get_color(self):
return self.color
def set_color(self,color):
self.color = color
RTS tech
23. __del__(self)
Destructor function remove an object from the memory.
__del__() method is used to create destructor in a class.
When an object is not in use the destructor function is
automatically called.
def __del__(self):
name=self.__class__.__name__
print("Object is Destroyed {}".format(name))
RTS tech
24. Object to string
We can convert an object into string form.
__str__(self) method is used to create an string function in
a class.
When we print an object the address is printed
<__main__.Mobile object at 0x000001EA4D8670D0>
after creating __str__(self) method in a class , when we
print an object the object is automatically converted into
string representation.
RTS tech
25. Inheritance
It is a concept of OOP language.
It is use to define a class based on another class.
One class is known as Base or super class and another class is
known as Derived or sub class.
Here Person is a Base class and Programmer, Doctor are Derived
classes.
By default Each class has Object as Base Class.
Syntax: class Doctor(Person):
pass
Person
Programmer Doctor
Person
RTS tech
26. Types of Inheritance
Class A
Class B Class C
Class B
Class A
Class C
Class B
Class A Class A
Class B Class C
1.Single Level 2.Multi Level 3.Multiple 4. Hierarchical
RTS tech
36. Method Overriding(cont.)
Sub class can override the Super class Method.
class Mobile:
def answer(self):
print("Answer the call")
class BasicPhone:
def answer(self):
print(“Press the key to answer the call")
class SmartPhone:
def answer(self):
print(“Swipe right to answer the call")
RTS tech
38. Abstract class
Abstract class contains abstract method.
Abstract method has only method declaration no method
definition
Different mobile phone has different implementation for
answerCall() method.
We can not create instance of abstract class.
Child class will provide implementation to the abstract
method.
RTS tech
39. Implementation
from abc import ABC,abstractmethod
class Mobile(ABC):
@abstractmethod
def answerCall(self):
pass
class BasicPhone:
def answer(self):
print(“Press the key to answer the call")
class SmartPhone:
def answer(self):
print(“Swipe right to answer the call")
RTS tech
40. Disclaimer
This is an educational presentation to enhance the skill of
computer science students.
This presentation is available for free to computer science
students.
Some internet images from different URLs are used in this
presentation to simplify technical examples and correlate
examples with the real world.
We are grateful to owners of these URLs and pictures.