Object Oriented Programming OOP in Python
Object Oriented Programming OOP in Python
Object Oriented Programming OOP in Python
Python is an object-oriented programming language. You can easily create and use classes
and objects in Python.
o Object
o Class
o Method
o Inheritance
o Polymorphism
o Data Abstraction
o Encapsulation
Object:
Object is an entity that has state and behaviour. It may be physical and logical. For example:
mouse, keyboard, chair, table, pen etc.
Everything in Python is an object, and almost everything has attributes and methods. All
functions have a built-in attribute __doc__, which returns the doc string defined in the
function source code.
Class:
Class can be defined as a collection of objects. It is a logical entity that has some specific
attributes and methods. For example: if you have an employee class then it should contain an
attribute and method i.e. an email id, name, age, salary etc.
Syntax:
class ClassName:
<statement-1>
.
.
.
<statement-N>
Method:
Method is a function that is associated with an object. In Python, method is not unique to
class instances. Any object type can have methods.
Inheritance:
Polymorphism:
Polymorphism is made by two words "poly" and "morphs". Poly means many and Morphs
means form, shape. It defines that one task can be performed in different ways. For example:
You have a class animal and all animals talk. But they talk differently. Here, the "talk"
behaviour is polymorphic in the sense and totally depends on the animal. So, the abstract
"animal" concept does not actually "talk", but specific animals (like dogs and cats) have a
concrete implementation of the action "talk".
Encapsulation:
Data Abstraction:
Data abstraction and encapsulation both are often used as synonyms. Both are nearly
synonym because data abstraction is achieved through encapsulation. Abstraction is used to
hide internal details and show only functionalities. Abstracting something means to give
names to things, so that the name captures the core of what a function or a whole program
does.
Classes are a way of grouping together related data and functions which act upon that data. In
fact a class is the basic building block in Python.A class is a kind of data type, just like a
string, integer or list. When we create an object of that data type, we call it an instance of a
class. The data values which we store inside an object are called attributes, and the functions
which are associated with the object are called methods. We have already used the methods
of some built-in objects, like strings and lists. When we design our own objects, we have to
decide how we are going to group things together, and what our objects are going to
represent.
Creating Classes:
The class statement creates a new class definition. The name of the class immediately
follows the keyword class followed by a colon as follows –
Syntax:
class ClassName:
'Optional class documentation string'
class_suite
Example:
class Employee:
‘Common base class for all employees’
empCount = 0
def displayCount(self):
print “Total Employee %d” % Employee.empCount
def displayEmployee(self):
print “Name : “, self.name, “, Salary: “, self.salary
The variable empCount is a class variable whose value is shared among all instances of a this
class. This can be accessed as Employee.empCount from inside the class or outside the class.
The first method __init__() is a special method, which is called class constructor or
initialization method. You declare other class methods like normal functions with the
exception that the first argument to each method is self. This self-reference is what we use to
set and get the instance’s attributes and to access its other methods, from within a method.
Syntax:
object_name = class_name( )
To create instances of a class, you call the class using class name and pass in whatever
arguments its __init__ method accepts.
Accessing Attributes:
The object can then access class variables and class methods using the dot operator (.) .
Class variable would be accessed using class name as follows −
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
class Employee:
'Common base class for all employees'
empCount = 0
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
Output:
You can add, remove, or modify attributes of classes and objects at any time −
__name__: Class name.
Methods:
A method is a piece of code that is called by name that is associated with an object. In most
respects it is identical to a function except for two key differences. We have already seen
some methods, such as keys and values, which were invoked on dictionaries. Each method is
associated with a class and is intended to be invoked on instances of that class.
Methods are defined inside a class definition in order to make the relationship
between the class and the method explicit.
The syntax for invoking a method is different from the syntax for calling a function.
Example:
class Dog:
def my_method(self):
print "I am a Dog"
dog = Dog()
dog.my_method() # Prints "I am a Dog"
Constructor Method:
Creating a constructor:
A constructor is a class function that begins with double underscore ( _ ). The name of the
constructor is always the same __init__( ).The __init__( ) method is automatically executed
when an object of a class is created.
While creating an object, a constructor can accept arguments if necessary. When you create a
class without a constructor, Python automatically creates a default constructor that doesn't do
anything.
Every class must have a constructor, even if it simply relies on the default constructor.
Example:
Inheritance:
The technique of creating a new class from an existing class is called Inheritance. The old or
existing class is called base class and the new class is known as derived class or sub class.
The child class inherits the attributes of its parent class, and you can use those attributes as if
they were defined in the child class. A child class can also override data members and
methods from the parent.
Syntax:
Class DerivedClass(BaseClass):
body_of_derived_class
Derived classes are declared much like their parent class; however, a list of base classes to
inherit from is given after the class name –
Example:
Class Person:
def__init__(self, name, age):
self.name = name
self.age = age
def display(self):
print(“NAME:”, self.name)
print(“AGE:”,self.age)
class Teacher(Person):
def __init__(self, name, age, exp, r_area):
Person.__init__(self, name, age)
self.exp = exp
self.r_area = r_area
def displayData(self):
Person.display(self)
print(“EXPERIENCE:”, self.exp)
print(“RESEARCH AREA:”, self.r_area)
Class Student(Person):
def __init__(self, name, age, course, marks):
Person.__init__(self, name, age)
self.course = course
self.marks = marks
def displayData(self):
Person.display(self)
print(“COURSE:”, self.course)
print(“MARKS:”, self.marks)
print(“*********TEACHER*********)
T = Teacher(“Jaya”, 43, 20, “Recommender Systems”)
T. displayData( )
print(“*********STUDENT*********”)
S = Student(“Latha”, 20,”B.Tech”, 78)
S.displayData( )
Output:
*********TEACHER********
NAME : Jaya
AGE : 43
EXPERIENCE : 20
RESEARCH AREA : Recommender Systems
*********STUDENT********
NAME : Latha
AGE : 20
COURSE : BTech
MARKS : 78
Similar way, you can drive a class from multiple parent classes as follows −
You can use issubclass() or isinstance() functions to check a relationships of two classes and
instances.
Types of Inheritance:
1. Multiple Inheritance: When a derived class inherits features from more that one base
class it is called Multiple Inheritance.
2. Multi-level Inheritance: The technique of deriving a class from an already derived
class is called Multi-level Inheritance.
3. Multi-path Inheritance: Deriving a class from other derived classes that are in turn
derived from the same base class is called Multi-path Inheritance.
Overriding Methods
Override means having two methods with the same name but doing different tasks. It means
that one of the methods overrides the other. Through method overriding a class may “copy”
another class, avoiding duplicated code, and at the same time enhance or customize part of
it.
You can always override your parent class methods. One reason for overriding parent's
methods is because you may want special or different functionality in your subclass.
Example:
class Parent: # define parent class
def myMethod(self):
print 'Calling parent method'
Output:
2 __del__( self )
Destructor, deletes an object
Sample Call : del obj
3 __repr__( self )
Evaluatable string representation
Sample Call : repr(obj)
4 __str__( self )
Printable string representation
Sample Call : str(obj)
5 __cmp__ ( self, x )
Object comparison
Sample Call : cmp(obj, x)
Data hiding:
An object's attributes may or may not be visible outside the class definition. You need to
name attributes with a double underscore prefix, and those attributes then are not be directly
visible to outsiders.
Example:
#!/usr/bin/python
class JustCounter:
__secretCount = 0
def count(self):
self.__secretCount += 1
print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount
Output:
1
2
Traceback (most recent call last):
File "test.py", line 12, in <module>
print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'
Python protects those members by internally changing the name to include the class name.
You can access such attributes as object._className__attrName. If you would replace your
last line as following, then it works for you −
.........................
print counter._JustCounter__secretCount
Output 2:
1
2
2
In our programs, we had been getting some or the other errors but we had not mentioned
much about them. Basically, there are two types of errors.
Abnormal or unexpected
behaviour of program
Error Exception
Exceptions are those which can be handled at the run time whereas errors cannot be handled.
An Exception is an Object of a type deriving from the System.Exception class. System
Exception is thrown by the CLR (Common Language Runtime) when errors occur that are
nonfatal and recoverable by user programs. It is meant to give you an opportunity to do
something with throw statement to transfer control to a catch clause in a try block.
Example:
>>>5/0
Traceback (most recent call last):
File “pyshell#5>”, line 1, in <module>
5/0
ZeroDivisionError: integer division or module by zero
>>>’Roll No’ + 123
Traceback (most recent call last):
File “pyshell#8>”, line 1, in <module>
’Roll No’ + 123
TypeError: cannot concatenate ‘str’ and ‘int’ objects
An Error is something that most of the time you cannot handle it. Errors are unchecked
exception and the developer is not required to do anything with these. Errors normally tend to
signal the end of your program, it typically cannot be recovered from and should cause you
exit from current program. It should not be caught or handled.
All the Errors are Exceptions but the reverse is not true. In general Errors are which nobody
can control or guess when it happened, on the other hand Exception can be guessed and can
be handled.
Example:
Python has many built-in exceptions which forces your program to output an error when
something in it goes wrong. When these exceptions occur, it causes the current process to
stop and passes it to the calling process until it is handled.
Hierarchy Of Exception:
In Python, exceptions can be handled using a try block and except block. A critical operation
which can raise exception is placed inside the try clause and the code that handles exception
is written in except clause.
Syntax:
try:
statements
except ExceptionName:
statements
Detailed Syntax:
try:
malicious code
except Exception1:
execute code
except Exception2:
execute code
. . . .
. . . .
except ExceptionN:
execute code
else:
In case of no exception, execute the else block code.
Example:
try:
a=10/0
print a
except ArithmeticError:
print "This statement is raising an exception"
else:
print "Welcome"
Output:
>>>
This statement is raising an exception
>>>
Explanation:
The malicious code (code having exception) is enclosed in the try block.
Try block is followed by except statement. There can be multiple except statement
with a single try block.
Except statement specifies the exception which occurred. In case that exception is
occurred, the corresponding statement will be executed.
At the last you can provide else statement. It is executed when no exception is
occurred.
Raising Exceptions:
You can explicitly throw an exception in Python using raise statement. raise will cause an
exception to occur and thus execution control will stop in case it is not handled.
Syntax:
1. raise Exception_class,<value>
Example:
1. try:
2. a=10
3. print a
4. raise NameError("Hello")
5. except NameError as e:
6. print "An exception occurred"
7. print e
Output:
1. >>>
2. 10
3. An exception occurred
4. Hello
5. >>>
Explanation:
ii) Exception can be provided with a value that can be given in the parenthesis. (here, Hello)
iii) To access the value "as" keyword is used. "e" is used as a reference variable which stores
the value of the exception.
Built-in User-defined
Creating your own Exception class or User Defined Exceptions by deriving classes from the
standard built-in exceptions are known as Custom Exception.
Example:
1. class ErrorInCode(Exception):
2. def __init__(self, data):
3. self.data = data
4. def __str__(self):
5. return repr(self.data)
6.
7. try:
8. raise ErrorInCode(2000)
9. except ErrorInCode as ae:
10. print "Received error:", ae.data
Output:
1. >>>
2. Received error : 2000
3. >>>