Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Lecture 01 - Basic Concept About OOP With Python
1. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Object Oriented Programming with Python
(Lecture # 01)
by
Muhammad Haroon
1. Object Oriented Programming
Object Oriented Programming (OOP) is a programming technique is which programs are written on the
basis of objects. An object is a collection of data and function. Object may represent a person, thing or
place in real world. In OOP, data and all possible functions on data are grouped together. Object
oriented programs are easier to learn and modify.
Object Oriented Programming is a powerful technique to develop software. It is used to analyze and
design the applications in terms of objects. It deals with data and the procedures that process the data as
a single object.
Some of the object oriented languages that have developed are:
✓ Python
✓ C++
✓ Smalltalk
✓ Eiffel
✓ CLOS
✓ Java
✓ C#
1.1 Features of Object-Oriented Programming
Following are some features of object oriented programming:
✓ Objects
✓ Classes
✓ Class variable
✓ Encapsulation/ Information Hiding
✓ Polymorphism
✓ Inheritance
✓ Overloading
✓ Overriding
2. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
✓ Abstraction
✓ Real world Modeling
✓ Reusability
Objects
OOP provides the facility of programming based on objects. Object is an entity that consists of data and
functions.
Classes
Classes are designs for creating objects. OOP provides the facility to design classes for creating different
objects. All properties and functions of an object are specified in classes.
Classes Variable
A variable that is shared by all instances of a class. Class variables are defined within a class but outside
any of the class's methods. Class variables are not used as frequently as instance variables are.
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. An object obj that belongs to a class Circle, for example, is an
instance of the class Circle.
Instantiation
The creation of an instance of a class.
3. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Method
A special kind of function that is defined in a class definition.
Operator overloading
The assignment of more than one function to a particular operator.
Real world Modeling
OOP is based on real world modeling. As in real world, things have properties and working capabilities.
Similarly, objects have data and functions. Data represents properties and functions represent working of
objects.
Reusability
OOP provides ways of reusing the data and code. Inheritance is a technique that allows a programmer to
use the code of existing program to create new programs.
Information Hiding
OOP allows the programmer to hide important data from the user. It is performed by encapsulation.
Polymorphism
Polymorphism is an ability of an object to behave in multiple ways.
Objects
An object represents an entity in the real world such as a person, thing or concept etc. An object is
identified by its name. An object consists of the following two things:
✓ Properties: Properties are the characteristics of an object.
✓ Functions: Functions are the action that can be performed by an object.
Examples
Some examples of objects of different types are as follows:
✓ Physical Objects
o Vehicles such as car, bus, truck etc.
o Electrical Components
✓ Elements of Computer User Environment
4. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
o Windows
o Menus
o Graphics objects
o Mouse and Keyboard
✓ User-defined Data Types
o Time
o Angles
Properties of Object
The characteristics of an objects are known as its properties or attributes. Each object has its own
properties. These properties can be used to describe the object. For example, the properties of an object
Person can be as follows:
✓ Name
✓ Age
✓ Weight
The properties of an object Car can be as follows:
✓ Color
✓ Price
✓ Model Engine
✓ Engine Power
The set of values of the attributes of a particular object is called its state. It means that the state of an
object can be determined by the values of its attributes. The following figure shows the values of the
attributes of an object Car:
Car
Model Honda City
Color Silver
Price 1200000
Engine Power 1600CC
Table 1.1: Properties of an object Car
Function of Object
An object can perform different tasks and actions. The actions that can be performed by an object are
known as functions or methods. For example, the object Car can perform the following functions:
✓ Start
✓ Stop
✓ Accelerate
✓ Reverse
5. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
The set of all functions of an object represents the behavior of the object. It means that the overall
behavior of an object can be determined by the list of functions of that object.
Car
Start
Stop
Accelerate
Reverse
Table 2.1: Functions of an object Car
Classes
A collection of objects with same properties and functions is known as class. A class is used to define
the characteristics of the objects. It is used as model for creating different objects of same type. For
example, a class Person can be used to define the characteristics and functions of a person. It can be
used to create many object of type person such as Haroon, Farhan, Jahanzaib, Usman etc. All objects of
Person class will have same characteristics and functions. However, the values of each object can be
different. The values are of the objects are assigned after creating an object.
Each object of a class is known as an instance of its class. For example, Haroon, Farhan and Jahanzaib
are three instances of a class Person. Similarly, myBook and youBook can be two instances of a class
Book.
Declaring a Classes
A class is declared in the same way as a structure is declared. The keyword class is used to declare a
class. A class declaration specifies the variables and functions that are common to all objects of that
class. The variables declared in a class are known as member variable or data members.
Syntax
The syntax of declaring a class is as follows:
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.
Example
6. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Below is a simple Python program that creates a class with single method.
class Name:
# A sample method
def fun(self):
print("Hello Haroon")
# Code
obj = Name()
obj.fun()
The self
✓ Class methods must have an extra first parameter in method definition. We do not give a value
for this parameter when we call the method, Python provides it
✓ If we have a method which takes no arguments, then we still have to have one argument – the
self. See fun() in above simple example.
✓ This is similar to this pointer in C++ and this reference in Java.
When we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by
Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.
The __init__ method
The __init__ method is similar to constructors in C++ and Java. It is run as soon as an object of a class is
instantiated. The method is useful to do any initialization you want to do with your object.
# A Sample class with init method
class Person:
# init method or constructor
def __init__(self, name):
self.name = name
# Sample Method
def say_hi(self):
print('Hello, My name is', self.name)
7. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
p = Person('Muhammad Haroon')
p.say_hi()
Class and Instance Variables/attributes
In the Python language, instance variables are variables whose value is assigned inside a constructor or
method with self.
Class variables are variables whose value is assigned in class.
# Python program to show that the variables with a value
# assigned in class declaration, are class variables and
# variables inside methods and constructors are instance
# variables.
# Class for Computer Science Student
class CSStudent:
# Class Variable
stream = 'cs'
# The init method or constructor
def __init__(self, roll):
# Instance Variable
self.roll = roll
# Objects of CSStudent class
a = CSStudent(1)
b = CSStudent(2)
print(a.stream) # prints "cs"
8. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
print(b.stream) # prints "cs"
print(a.roll) # prints 1
# Class variables can be accessed using class
# name also
print(CSStudent.stream) # prints "cs"
We can define instance variables inside normal methods also.
# Python program to show that we can create
# instance variables inside methods
# Class for Computer Science Student
class CSStudent:
# Class Variable
stream = 'cs'
# The init method or constructor
def __init__(self, roll):
# Instance Variable
self.roll = roll
# Adds an instance variable
def setAddress(self, address):
self.address = address
# Retrieves instance variable
def getAddress(self):
9. OOP with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
return self.address
# Code
a = CSStudent(101)
a.setAddress("Muhammad Haroon, Multan")
print(a.getAddress())
How to create an empty class?
We can create an empty class using pass statement in Python.
# An empty class
class Test:
pass
End