OOP Python
OOP Python
Programming
in
Python
Contents
> Differences Procedure vs Object Oriented Programming
> Features of OOP
> Fundamental Concepts of OOP in Python
> What is Class
> What is Object
> Methods in Classes
Overloading
2
Featuers of OOP
Ability to simulate real-world event much more effectively
Code is reusable thus less code may have to be written
Data becomes active
Better able to create GUI (graphical user interface) applications
Programmers are able to produce faster, more accurate and better-
written applications
What is an Object..?
Objects are the basic run-time entities in an object-oriented
system.
They may represent a person, a place, a bank account, a table of
data or any item that the program must handle.
When a program is executed the objects interact by sending
messages to one another.
Objects have two components:
- Data (i.e., attributes)
Behaviors (i.e., methods)
Object Methods
Define the behaviors for the
object
Example (taxi):
- PickUp
- DropOff
- GoOnDuty
- GoOffDuty
- GetDriver
- SetDriver
- GetNumPassengers
7
What is a Class..?
A class is a special data type which defines how to build a certain
kind of object.
The class also stores some data items that are shared by all the
instances of this class
Instances are objects that are created which follow the definition
given inside of the class
Python doesnt use separate class interface definitions as in some
languages
You just define the class and then use it
Methods in Classes
Define a method in a class by including function definitions
11
Self
The first argument of every method is a reference to the current
Continue
Although you must specify self explicitly when defining the
13
14
15
Encapsulation
Important advantage of OOP consists in the encapsulation of
17
can be accessed from outside, i.e. the value can be read and
changed
Data can be protected by making members private or protected.
Instance variable names starting with two underscore characters
cannot be accessed from outside of the class.
At least not directly, but they can be accessed through private
name mangling.
That means, private data __A can be accessed by the following
name construct: instance_name._classname__A
18
is a protected member.
Protected members can be accessed like public members from
outside of class
Example:
class Encapsulation(object):
def __init__(self, a, b, c):
self.public = a
self._protected = b
self.__private = c
19
Notation
Behavior
name
Public
_name
Protected
__name
Private
Inheritance
Inheritance is a powerful feature in object oriented programming
It refers to defining a new class with little or no modification to an existing
class.
The new class is called derived (or child) class and the one from which it
inherits is called the base (or parent) class.
Derived class inherits features from the base class, adding new features to it.
This results into re-usability of code.
Syntax:
class Baseclass(Object):
body_of_base_class
class DerivedClass(BaseClass):
body_of_derived_clas
22
24
Polymorphism
Polymorphism in Latin word which made up of ploy means
25
Cut
27
Operator Overloading
Python operators work for built-in classes.
But same operator behaves differently with different types. For
Expression
Internally
Addition
p1 + p2
p1.__add__(p2)
Subtraction
p1 p2
p1.__sub__(p2)
Multiplication
p1 * p2
p1.__mul__(p2)
Power
p1 ** p2
p1.__pow__(p2)
Division
p1 / p2
p1.__truediv__(p2)
29