Python Programming Unit-V
Python Programming Unit-V
Python is an object oriented programming language. OOP uses the concept of objects and
classes.
class:
A class can be thought of as a 'blueprint' for objects. These can have their own attributes
(characteristics they possess), and methods (actions they perform).
Defining a class:
To define a class in Python, you can use the class keyword, followed by the class name and a
colon. The syntax to define a class is given as follows
class ClassName:
class_suite
The class has a documentation string which has brief description of the class.This can
be accessed via ClassName.__doc__.
The class_suite consists of all the component statements defining class members, data
attributes and functions.
The variables defined inside a class are called class variables and the functions defined in the
classes are called as methods. These class variables and class methods together called as class
members. The class members can be accessed through class objects.
When a class definition is entered, a new namespace is created, and used as the local
scope. This means that a class creates a new local namespace where all its attributes are
defined. Attributes may be data or methods.
Page 1
class Employee:
empCount = 0
def displayCount(self):
print ("Total Employee %d" % Employee.empCount)
def displayEmployee(self):
print ("Name : ", self.name, ", Salary: ", self.salary)
Creating objects:
Once a class is defined, we can create objects of the class. Creating objects of the class is
called instantiation. The syntax for creating an object is
Objectname=classname( )
Ex:
After an object is created, the class variables and methods can be accessed through objects
using dot(.) operator.The syntax is
Objectname . class_variable_name
Objectname . class_method_name( )
Ex:
emp1.displayEmployee()
emp2.displayEmployee()
Page 2
Methods:
Class methods are similar to the ordinary functions with just one small difference.
class methods must have the first argument named “self”. This is the first argument
that needs to be added to the beginning of the parameter list.
But , When we call the method we don’t pass any value for this parameter. Python
provides it automatically. The self argument refers to the object itself.
Ex:
"""Common base class for all employees""" Name : pavan , Salary: 2000
Name : kumar , Salary: 5000
empCount = 0 Total Employee 2
def displayCount(self):
print ("Total Employee %d" % Employee.empCount)
def displayEmployee(self):
print ("Name : ", self.name, ", Salary: ", self.salary)
emp1.displayEmployee()
emp2.displayEmployee()
print ("Total Employee %d" % Employee.empCount)
Here the variable empCount is a class variable whose value is shared among all instances of a
class. This can be accessed as Employee.empCount from inside the class or outside the class.
Also the first method _ _init_ _ is a special method, which is called class constructor
or initialization method that Python calls when you create a new instance of this class.
Page 3
Constructor:
A constructor is a special type of method which is used to initialize the instance members of
the class. Constructor definition is executed when we create the object of this class.
In python, the method __init__ ( ) simulates the constructor of the class. This method is
called when the class is instantiated. We can pass any number of arguments at the time of
creating the class object, depending upon __init__( ) definition. It is mostly used to initialize
the class attributes.
statement-1
statement-2
………….
…………..
statement-n
Ex:
def show(self):
print("Hello",self.name)
s1 = Student("John")
s1.show()
Python deletes unneeded objects built – in types or class instances automatically to free the
memory space. The process by which Python periodically reclaims blocks of memory that no
longer are in use is termed Garbage Collection.
Python's garbage collector runs during program execution and is triggered when an
object's reference count reaches zero. An object's reference count changes as the number of
aliases that point to it changes. An object's reference count increases when it is assigned a
new name or placed in a container list, tuple, ordictionary. The object's reference count
decreases when it's deleted with del, its reference is reassigned, or its reference goes out of
scope. When an object's reference count reaches zero, Python collects it automatically.
Page 4
A class can implement the special method __del__( ), called a destructor, that is invoked
when the instance is about to be destroyed.
def __del__(self):
print("object of Student class is destroyed")
s1 = Student("John")
s1.show()
del s1
The getattr( ) function returns the value of the named attribute of an object. If not
found, it returns the default value provided to the function. If named attribute is not found
and default is not defined then AttributeError exception raised. This is same as
object.name
class Person:
age = 23
name = "pavan"
p1 = Person()
print('The name of the person is:', getattr(p1,"name"),'and age is:', getattr(p1, "age"))
print('The name of the person is:', p1.name,'and age is:', p1.age)
2. hasattr(object, name):
The hasattr() function is used to check whether the object has named argument or not . this
function returns True if an object has the given named attribute and False if it does not.
Page 5
class Person: Output:
age = 23
name = 'pavan' Person has age?: True
Person has salary?: False
p1 = Person()
4. delattr(object, name):
The delattr() deletes an attribute from the object (if the object allows it). You can also delete
attribute of an object using del operator.
Page 6
Ex:
p1 = Person('pavan',32)
p1.display()
Built-in class attributes gives us information about the class. We can access the built-in class
attributes using the dot( .) operator.Following are the built-in class attributes.
Attribute Description
_ _module_ _ This gives us the name of the module in which the class is defined.
_ _bases_ _ A possibly empty tuple containing the base classes in the order of their
occurrence.
Page 7
Ex:
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def display(self):
print("name is",self.name,'and age is',self.age)
p1 = Person('pavan',32)
p1.display()
Inheritance:
Inheritance refers to defining a new class with little or no modification to an existing class.
The new class is called derived (or child or sub) class and the old class or existing class is
called the base (or parent or super) class.
class BaseClass:
class_suite
class DerivedClass(BaseClass):
class_suite
Page 8
Derived class inherits features from the base class, adding new features to it. This results into
re-usability of code
# definition of the superclass starts here
class Person:
#initializing the variables
name = ""
age = 0
#defining constructor
def __init__(self, personName, personAge):
self.name = personName
self.age = personAge
def showAge(self):
print(self.age)
self.studentId = studentId
def getId(self):
return self.studentId #returns the value of student id
Page 9
Output:
23
102
kumar
multiple Inheritance:
A class can be derived from more than one base classes in Python. This is called multiple
inheritance. In multiple inheritance, the features of all the base classes are inherited into the
derived class.This is illustrated in the following figure.
Syntax:
Page 10
Note:
In the multiple inheritance scenario, any specified attribute is searched first in the current
class. If not found, the search continues into parent classes in depth-first, left-right fashion
without searching same class twice.
Ex:
Multilevel Inheritance:
Multi-level inheritance is archived when a derived class inherits another derived class. There
is no limit on the number of levels up to which, the multi-level inheritance is archived in
python. This is illustrated in the following figure
Page 11
Syntax:
Page 12
Method overriding:
Method overriding is a concept of object oriented programming that allows us to change the
implementation of a method in the child class that is defined in the parent class. It is the
ability of a child class to change the implementation of any method which is already provided
by one of its parent class(ancestors).
In Python, method overriding occurs by simply defining a method in the child class with the
same name of a method in the parent class, but gives a new meaning.
Note:
To call the base class method from the derived class method , we have to use super( ) built-in
function. when we invoke the method using super( ) the parent version method is called.
def printmessage(self):
print(self.message)
class Child(Parent):
def __init__(self, txt):
super().__init__(txt)
x.printmessage()
Page 13
Data Hiding:
An object’s attributes may or may not be visible outside the class definition .Python uses a
special naming scheme for attributes to control the accessibility of the attributes. The
attribute names, which can be freely used inside or outside of a class definition, correspond to
public attributes.
First, we can prefix an attribute name with a leading underscore "_". This marks the
attribute as protected. It tells users of the class not to use this attribute unless,
somebody writes a subclass.
Second, we can prefix an attribute name with two leading underscores "__". The
attribute is now inaccessible and invisible from outside. It's neither possible to read
nor write to those attributes except inside of the class definition itself.
But in some situations, we need to access these attributes outside the class. This
can be done with the following syntax:
class A: Output:
When writing a program, errors may be encountered. There are two distinguishable kinds of
errors:
Page 14
Syntax error:
Error caused by not following the proper structure (syntax) of the language is called syntax
error or parsing error.
Ex:
>>> if a < 3
File "<interactive input>", line 1
if a < 3
^
SyntaxError: invalid syntax
Here a colon is missing in the if statement. Syntax errors are easy to fix.
Exceptions:
When a Python script raises exception, it creates an Exception object. If the script raises
an exception, it must handle the exception; otherwise, if it doesn't handle the exception
the program will terminate abruptly.
Handling Exceptions:
Python handles exceptions using try and except blocks. In try block you can write the code
which is suspicious to raise an exception, and in except block, you can write the code which
will handle this exception.
Syntax:
Page 15
Page 16
Multiple except blocks:
A try statement may have more than one except clause, to specify handlers for
different exceptions. At most one handler will be executed.
Syntax:
Ex:
Page 17
Except clause with multiple exceptions:
If you want to write a single except clause to handle multiple exceptions, this can be achieved
by writing names of exception classes in except clause separated by comma.
Syntax:
Ex:
Page 18
Except Clause with No Exceptions:
We can specify an except block without mentioning any exception name. This type of except
block must be last one if present.
This default except block can be used along with other exception handlers, which
handle some specific type of Exceptions. But the exceptions that are not handled by these
specific exception handlers can be handled by this default except block.
Syntax:
Ex:
Page 19
else clause:
The try … except statement has an optional else clause, which, when present, must follow all
except clauses. The code in the else clause executes if the code in the try: block does not raise
an exception.
Syntax:
try: Output 1:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling") Written content in the file successfully
except IOError:
print ("Error: can\'t find file or read data")
Output 2:
else:
Error: can't find file or read data
print ("Written content in the file successfully")
fh.close()
finally clause:
The try statement in Python can have an optional finally clause. In case if there is any code
which you want to be executed, whether exception occurs or not, then that code can be placed
inside the finally clause. The finally clause runs whether or not the try statement produces an
exception.
Page 20
try:
# block of code
finally:
# block of code
try: Output:
numerator = int(input("enter a value:"))
denominator = int(input("enter a value:")) enter a value:3
enter a value:0
res=numerator/denominator division by zero is not possible
print("result=",res) executing finally clause
except ArithmeticError:
except:
print("enter numeric integer values only")
finally:
print("executing finally clause")
Raising exceptions:
In Python programming, exceptions are raised when corresponding errors occur at run time,
but we can forcefully raise it using the keyword raise.
We can also optionally pass in value to the exception to clarify why that exception was raised
try: Output:
a = int(input("Enter a positive integer: "))
if a <= 0: Enter a positive integer: -4
raise ValueError("That is not a positive number!") That is not a positive number!
Page 21
User defined exceptions:
Python has many built-in exceptions which you can use in your program, but sometimes you
may need to create custom exceptions with custom messages to serve your purpose.
In Python, users can define such exceptions by creating a new class. This exception class has
to be derived, either directly or indirectly, from Exception class. This new exception can be
raised, like other exceptions, using the raise statement with an optional error message.
Ex:
Page 22
# defining Python user-defined exceptions Output:
class Error(Exception):
"""Base class for other exceptions"""
pass Enter a number: 15
This value is large, try again!
class ValueTooSmallError(Error): Enter a number: 1
"""Raised when the input value is too small""" This value is small, try again!
pass Enter a number: 10
Congratulations! You guessed it correctly.
class ValueTooLargeError(Error):
"""Raised when the input value is too large"""
pass
number = 10
while True:
try:
num = int(input("Enter a number: "))
if num < number:
raise ValueTooSmallError
elif num > number:
raise ValueTooLargeError
break
except ValueTooSmallError:
print("This value is small, try again!")
except ValueTooLargeError:
print("This value is large, try again!")
Page 23
Operator overloading:
Python operators work for built-in classes. But same operator behaves differently with
different types. For example, the + operator will, perform arithmetic addition on two
numbers, merge two lists and concatenate two strings as can be seen below.
This feature in Python, that allows same operator to have different meaning according to the
context is called operator overloading.
By default, most of the built-in operators will not work with objects of your classes. But In
python, each built-in function or operator has a special method corresponding to it.
You must add the corresponding special methods in your class definition to make your
object compatible with built-in operators.
When you do this, the behavior of the operator associated with it changes according to that
defined in the method thus providing the operator overloading.
For example, to overload the + operator to work with objects of our class, we will need to
implement the _ _ add_ _( ) method in the class. we can write own code but an object of the
class must be returned.
Ex:
def __str__(self):
return "({0},{1})".format(self.x,self.y)
p1 = Point(2,3)
p2 = Point(-1,2)
print(p1 + p2)
Page 24
When we call p1+p2, Python will call p1.__add__(p2) which in turn is Point.__add__(p1,p2)
Similarly, we can overload other operators as well. The special methods for some of
the operators are given below.
Page 25
Page 26