0% found this document useful (0 votes)
3 views30 pages

Python PDF

This document provides an overview of classes and objects in Python, explaining the concepts of class definition, object instantiation, and the use of the __init__ method for initialization. It also covers key object-oriented programming principles such as inheritance, polymorphism, and encapsulation, along with examples to illustrate these concepts. Additionally, it discusses data modeling in Python, emphasizing the identity of objects and their attributes.

Uploaded by

amitbhai3137
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views30 pages

Python PDF

This document provides an overview of classes and objects in Python, explaining the concepts of class definition, object instantiation, and the use of the __init__ method for initialization. It also covers key object-oriented programming principles such as inheritance, polymorphism, and encapsulation, along with examples to illustrate these concepts. Additionally, it discusses data modeling in Python, emphasizing the identity of objects and their attributes.

Uploaded by

amitbhai3137
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT 4

Classes and Objects


A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of
bundling data and functionality together. Creating a new class creates a new type of object, allowing new
instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state.
Class instances can also have methods (defined by their class) for modifying their state.
Class creates a user-defined data structure, which holds its own data members and member functions, which can
be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
Some points on Python class:
 Classes are created by keyword class.
 Attributes are the variables that belong to a class.
 Attributes are always public and can be accessed using the dot (.) operator. Eg.: Myclass.Myattribute

Class Definition Syntax:

class ClassName:
# Statement-1
.
.
.
# Statement-N
# a class

class Dog:
pass

Class Objects
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual
values. It‟s not an idea anymore, it‟s an actual dog, like a dog of breed pug who‟s seven years old. You can have
many dogs to create many different instances, but without the class as a guide, you would be lost, not knowing
what information is required.
An object consists of :
 State: It is represented by the attributes of an object. It also reflects the properties of an object.
 Behavior: It is represented by the methods of an object. It also reflects the response of an object to other
objects.
 Identity: It gives a unique name to an object and enables one object to interact with other objects.

Declaring Objects (Also called instantiating a class)


When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and
the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class
may have any number of instances.
Example:
Declaring an object –
# Python3 program to
# demonstrate instantiating
# a class

class Dog:

# A simple class
# attribute
attr1 = "mammal"
attr2 = "dog"

# A sample method
def fun(self):
print("I'm a", self.attr1)
print("I'm a", self.attr2)

# Driver code
# Object instantiation
Pet = Dog()

# Accessing class attributes


# and method through objects
print(Pet.attr1)
Pet.fun()

Output:
mammal
I'm a mammal
I'm a dog

__init__ method
The __init__ method is similar to constructors in C++ and Java. Constructors are used to initializing the object‟s
state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at
the time of Object creation. It runs 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)

p = Person('Nikhil')
p.say_hi()

Output:
Hello, my name is Nikhil

Class and Instance Variables


Instance variables are for data, unique to each instance and class variables are for attributes and methods shared
by all instances of the class. Instance variables are variables whose value is assigned inside a constructor or
method with self whereas class variables are variables whose value is assigned in the class.
Defining instance variable using a constructor.
# Python3 program to show that the variables with a
value
# assigned in the class declaration, are class variables
and
# variables inside methods and constructors are instance
# variables.

# Class for Dog


class Dog:

# Class Variable
animal = 'dog'

# The init method or constructor


def __init__(self, breed, color):

# Instance Variable
self.breed = breed
self.color = color

# Objects of Dog class


Pet = Dog("Pug", "brown")
Buzo = Dog("Bulldog", "black")

print('Pet details:')
print('Pet is a', Pet.animal)
print('Breed: ', Pet.breed)
print('Color: ', Pet.color)

print('\nBuzo details:')
print('Buzo is a', Buzo.animal)
print('Breed: ', Buzo.breed)
print('Color: ', Buzo.color)

# Class variables can be accessed using class


# name also
print("\nAccessing class variable using class name")
print(Dog.animal)
Output:
Pet details:
Pet is a dog
Breed: Pug
Color: brown

Buzo details:
Buzo is a dog
Breed: Bulldog
Color: black

Accessing class variable using class name


dog

Defining instance variable using the normal method.


# Python3 program to show that we can create
# instance variables inside methods

# Class for Dog


class Dog:

# Class Variable
animal = 'dog'

# The init method or constructor


def __init__(self, breed):

# Instance Variable
self.breed = breed

# Adds an instance variable


def setColor(self, color):
self.color = color

# Retrieves instance variable


def getColor(self):
return self.color

# Driver Code
Pet = Dog("pug")
Pet.setColor("brown")
print(Pet.getColor())

Output:
brown

OOPs Concepts
Main Concepts of Object-Oriented Programming (OOPs)
 Class
 Objects
 Polymorphism
 Encapsulation
 Inheritance

Class
A class is a collection of objects. A class contains the blueprints or the prototype from which the objects are being
created. It is a logical entity that contains some attributes and methods.
To understand the need for creating a class let‟s consider an example, let‟s say you wanted to track the number of
dogs that may have different attributes like breed, age. If a list is used, the first element could be the dog‟s breed
while the second element could represent its age. Let‟s suppose there are 100 different dogs, then how would you
know which element is supposed to be which? What if you wanted to add other properties to these dogs? This
lacks organization and it‟s the exact need for classes.
Some points on Python class:
 Classes are created by keyword class.
 Attributes are the variables that belong to a class.
 Attributes are always public and can be accessed using the dot (.) operator. Eg.: Myclass.Myattribute
Class Definition Syntax:
class ClassName:
# Statement-1
.
.
.
# Statement-N

# Python3 program to
# demonstrate defining
# a class

class Dog:
pass

Objects
The object is an entity that has a state and behavior associated with it. It may be any real-world object like a
mouse, keyboard, chair, table, pen, etc. Integers, strings, floating-point numbers, even arrays, and dictionaries,
are all objects. More specifically, any single integer or any single string is an object. The number 12 is an object,
the string “Hello, world” is an object, a list is an object that can hold other objects, and so on. You‟ve been using
objects all along and may not even realize it.
An object consists of :
 State: It is represented by the attributes of an object. It also reflects the properties of an object.
 Behavior: It is represented by the methods of an object. It also reflects the response of an object to other
objects.
 Identity: It gives a unique name to an object and enables one object to interact with other objects.
To understand the state, behavior, and identity let us take the example of the class dog (explained above).
 The identity can be considered as the name of the dog.
 State or Attributes can be considered as the breed, age, or color of the dog.
 The behavior can be considered as to whether the dog is eating or sleeping.

Example: Creating an object


obj = Dog()

This will create an object named obj of the class Dog defined above. Before diving deep into objects and class let
us understand some basic keywords that will we used while working with objects and classes.
The self
1. Class methods must have an extra first parameter in the method definition. We do not give a value for this
parameter when we call the method, Python provides it
2. If we have a method that takes no arguments, then we still have to have one argument.
3. 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.
Note: For more information, refer to self in Python class

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.
Now let us define a class and create some objects using the self and __init__ method.

Example 1: Creating a class and object with class and instance attributes
class Dog:

# class attribute
attr1 = "mammal"

# Instance attribute
def __init__(self, name):
self.name = name

# Driver code
# Object instantiation
Pet = Dog("Pet")
Tommy = Dog("Tommy")

# Accessing class attributes


print("Pet is a {}".format(Pet.__class__.attr1))
print("Tommy is also a {}".format(Tommy.__class__.attr1))

# Accessing instance attributes


print("My name is {}".format(Pet.name))
print("My name is {}".format(Tommy.name))

Output
Pet is a mammal
Tommy is also a mammal
My name is Pet
My name is Tommy
Creating Class and objects with methods
class Dog:

# class attribute
attr1 = "mammal"

# Instance attribute
def __init__(self, name):
self.name = name

def speak(self):
print("My name is {}".format(self.name))

# Driver code
# Object instantiation
Pet = Dog("Pet")
Tommy = Dog("Tommy")

# Accessing class methods


Pet.speak()
Tommy.speak()
Output
My name is Pet
My name is Tommy

Inheritance
Inheritance is the capability of one class to derive or inherit the properties from another class. The class that
derives properties is called the derived class or child class and the class from which the properties are being
derived is called the base class or parent class. The benefits of inheritance are:
 It represents real-world relationships well.
 It provides the reusability of a code. We don‟t have to write the same code again and again. Also, it allows us
to add more features to a class without modifying it.
 It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of
B would automatically inherit from class A.

# Python code to demonstrate how parent constructors


# are called.

# parent class
class Person(object):

# __init__ is known as the constructor


def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber

def display(self):
print(self.name)
print(self.idnumber)

def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
# child class
class Employee(Person):
def __init__(self, name, idnumber, salary, post):
self.salary = salary
self.post = post

# invoking the __init__ of the parent class


Person.__init__(self, name, idnumber)

def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
print("Post: {}".format(self.post))

# creation of an object variable or an instance


a = Employee('Rahul', 886012, 200000, "Intern")

# calling a function of the class Person using


# its instance
a.display()
a.details()
Output
Rahul
886012
My name is Rahul
IdNumber: 886012
Post: Intern

Polymorphism
Polymorphism simply means having many forms. For example, we need to determine if the given species of birds
fly or not, using polymorphism we can do this using a single function.
class Bird:

def intro(self):
print("There are many types of birds.")

def flight(self):
print("Most of the birds can fly but some cannot.")

class sparrow(Bird):

def flight(self):
print("Sparrows can fly.")

class ostrich(Bird):

def flight(self):
print("Ostriches cannot fly.")

obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()

obj_bird.intro()
obj_bird.flight()

obj_spr.intro()
obj_spr.flight()

obj_ost.intro()
obj_ost.flight()
Output
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.

Encapsulation
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of
wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables
and methods directly and can prevent the accidental modification of data. To prevent accidental change, an
object‟s variable can only be changed by an object‟s method. Those types of variables are known as private
variables.
A class is an example of encapsulation as it encapsulates all the data that is member functions, variables, etc.

# Python program to
# demonstrate private members

# Creating a Base class


class Base:
def __init__(self):
self.a = "GoodsforGoods"
self.__c = "GoodsforGoods"

# Creating a derived class


class Derived(Base):
def __init__(self):

# Calling constructor of
# Base class
Base.__init__(self)
print("Calling private member of base class: ")
print(self.__c)

# Driver code
obj1 = Base()
print(obj1.a)

# Uncommenting print(obj1.c) will


# raise an AttributeError

# Uncommenting obj2 = Derived() will


# also raise an AtrributeError as
# private member of base class
# is called inside derived class
Output
GoodsforGoods

Data Modelling in Python


The process of creating Data Models using the syntax and environment of the Python programming language is
called Data Modelling in Python. A Data Model is a data abstraction model that organizes different elements of data
and standardizes the way they relate to one another and to the properties of real-world entities. In simple words,
Data Modelling in Python is the general process by which this programming language organizes everything
internally and it treats and processes data.
The Data Model is the building block of Python. Internally Data Model has its design and code blocks for its
implementation. It is composed of entities, and entities are none but the objects. It is said that everything is an object
in Python. Each entity has its own attributes (properties) and methods(actions) or behaviour associated with it. Each
object has three attributes: an identity, a type, and a value.
Identity of an object

Every object, either for Data Modelling in Python or any other activity involving Python‟s environment, has an
identity which can never change once it is created. Think of identity as an object‟s address in the memory.
id() is the function that gives us the identity of the object or we can say it returns the virtual memory address of the
object. The memory address will be an integer value.
>>> a='hevodata'
>>> id(a)
1397871458544

>>> b=101
>>> id(b)
1623965024

>>> c='hevodata'
>>> id(c)
1397871458544

>>> a is b
False

>>> a is c
True
Type of an Object

During Data Modelling in Python, the type of an object means the name of the class to which the object belongs.
Function type() tells the type of the object. By knowing the type of an object, it is easy for user‟s to specify two
things.
The operation allowed on that object
The set of values the object can hold.
Python Code:
a='hevodata'
x=type(a)
print("Type of variable 'a' is: ", x)
Type of variable 'a' is: <class 'str'>
b=101
y= type(b)
print("Type of variable 'b' is: ", y)
Type of variable 'b' is: <class 'int'>

fruits = ('apple', 'banana', 'grapes', 'orange')


t = type(fruits)
print("Type of variable 'fruits' is: ", t)
Type of variable 'fruits' is: <class 'tuple'>
Value of an Object

An object‟s value during Data Modelling in Python is the data that is stored for that object. The value object can
hold is decided on the basis of the type of object.
Python Code:
var='article'
print("Value of variable 'var' is: ", var)
Value of variable 'var' is: article

Object values are changeable and it depends on their type. Python supports the following 2 types of objects based on
their values:

 Mutable Objects
 Immutable Objects

There is some type for which the value of an object cannot change those are called immutable objects and whose
value can be changed are called mutable objects.
1) Mutable Objects

The mutability of objects is decided on the basis of their type. Lists, Dictionaries are mutable objects. Those objects
whose values can be changed are called Mutable objects.
The following Python code is useful for creating a list for Data Modelling in Python:
#Let's create a list
>>> a = [11, 22, 33]
>>> print("List values: ",a)
>>> print("Identity of a: ",id(a))
List values: [11, 22, 33]
Identity of a: 1397871407048

>>> a[0] = 1 #Change first value of list


>>> print("Changed List values: ",a)
>>> print("Identity of a: ",id(a))
Changed List values: [1, 22, 33]
Identity of a: 1397871407048
2) Immutable Objects

During Data Modelling in Python, Immutable Objects are the objects that stored data but their values cannot be
modified. Numbers, Strings, Tuples, and Sets are immutable.
The following Python code is useful for creating a variable with string value during Data Modelling in Python:
#Let's create a varible with string value
s = "Hevo"
print("Variable value: ",s)
print("Identity of s: ",id(s))
Variable value: Hevo
Identity of s: 1397871732528

s = "Data" #Change value of varibale 's'


print("Variable value: ",s)
print("Identity of s: ",id(s))

Variable value: Data


Identity of s: 1397836021296

Special Methods for Data Modelling in Python


The special method names allow your objects to implement, support, and interact with basic language constructs
such as Iteration, object creation, object destruction, collections, attribute access, etc.
Below are some of the examples of special methods for Data Modelling in Python which help to understand how
these built-ins work in Python.
 The __init__() method is for initialization and is called by the python interpreter itself when an instance of
an object is created.
 The len(x) method is for the length calculation of an object, internally the python interpreter calls x.__len()
 Call x[2] to get an item at location 2, internally the python interpreter calls x.__getitem__(2)
 When str(x) is called, internally the python interpreter, calls x.__str__()
 Operators are also magic methods, add operator x + y actually turns to x.__add__(y)
class MyList:
def __init__(self, *args):
self._data = list(args)

def __getitem__(self, index):


out = self._data[index]
return (out)

def __len__(self):
return len(self._data)

x = MyList(11, 22, 33, 4, 5) #List initialization

# Get length of list


print("Length of list: ",len(x))

# Get an item of list


print("Item of list: ", x[2])
Output:
Length of list: 5
Item of list: 33

Persistent storage of objects


The shelve module in Python‟s standard library is a simple yet effective tool for persistent data storage when using a
relational database solution is not required. The shelf object defined in this module is dictionary-like object which is
persistently stored in a disk file. This creates afile similar to dbm database on UNIX like systems. Only string data
type can be used as key in this special dictionary object, whereas any picklable object can serve as value
The shelve module defines three classes as follows −
Sr.No. Module & Description
Sr.No. Module & Description

1 Shelf
This is the base class for shelf implementations. It is initialized with dict-like object.

2 BsdDbShelf
This is a subclass of Shelf class. The dict object passed to its constructor must support first(), next(),
previous(), last() and set_location() methods.

3 DbfilenameShelf
This is also a subclass of Shelf but accepts a filename as parameter to its constructor rather than dict
object.
open(filename, flag = 'c', protocol=None, writeback = False)
The filename parameter is assigned to the database created.
Default value for flag parameter is „c‟ for read/write access. Other flags are „w‟ (write only) „r‟ (read only) and „n‟
(new with read/write)
Protocol parameter denotes pickle protocol writeback parameter by default is false. If set to true, the accessed
entries are cached. Every access calls sync() and close() operations hence process may be slow.
Following code creates a database and stores dictionary entries in it.
import shelve
s = shelve.open("test")
s['name'] = "Ajay"
s['age'] = 23
s['marks'] = 75
s.close()
This will create test.dir file in current directory and store key-value data in hashed form. The Shelf object has
following methods available −
Sr.No. Method & Description

1 close()
synchronise and close persistent dict object.

2 sync()
Write back all entries in the cache if shelf was opened with writeback set to True.

3 get()
returns value associated with key

4 items()
list of tuples – each tuple is key value pair

5 keys()
list of shelf keys

6 pop()
remove specified key and return the corresponding value.

7 update()
Update shelf from another dict/iterable

8 values()
list of shelf values
To access value of a particular key in shelf.
>>> s=shelve.open('test')
>>> s['age']
23
>>> s['age']=25
>>> s.get('age')
25
The items(), keys() and values() methods return view objects.
>>> list(s.items())
[('name', 'Ajay'), ('age', 25), ('marks', 75)]
>>> list(s.keys())
['name', 'age', 'marks']
>>> list(s.values())
['Ajay', 25, 75]
To remove a key-value pair from shelf
>>> s.pop('marks')
75
>>> list(s.items())
[('name', 'Ajay'), ('age', 25)]
Notice that key-value pair of marks-75 has been removed.
To merge items of another dictionary with shelf use update() method
>>> d={'salary':10000, 'designation':'manager'}
>>> s.update(d)
>>> list(s.items())
[('name', 'Ajay'), ('age', 25), ('salary', 10000), ('designation', 'manager')]

Inheritance
Inheritance is the capability of one class to derive or inherit the properties from another class. The benefits of
inheritance are:

1. It represents real-world relationships well.


2. It provides reusability of a code. We don‟t have to write the same code again and again. Also, it allows us to
add more features to a class without modifying it.
3. It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of
B would automatically inherit from class A.
# A Python program to demonstrate inheritance

# Base or Super class. Note object in bracket.


# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"
class Person(object):

# Constructor
def __init__(self, name):
self.name = name

# To get name
def getName(self):
return self.name

# To check if this person is an employee


def isEmployee(self):
return False
# Inherited or Subclass (Note Person in bracket)
class Employee(Person):

# Here we return true


def isEmployee(self):
return True

# Driver code
emp = Person("Good1") # An Object of Person
print(emp.getName(), emp.isEmployee())

emp = Employee("Good2") # An Object of Employee


print(emp.getName(), emp.isEmployee())

Output:
Good1 False
Good2 True

Subclassing (Calling constructor of parent class)


A child class needs to identify which class is its parent class. This can be done by mentioning the parent class
name in the definition of the child class.
Eg: class subclass_name (superclass_name):

# Python code to demonstrate how parent constructors


# are called.

# parent class
class Person( object ):

# __init__ is known as the constructor


def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
def display(self):
print(self.name)
print(self.idnumber)

# child class
class Employee( Person ):
def __init__(self, name, idnumber, salary, post):
self.salary = salary
self.post = post

# invoking the __init__ of the parent class


Person.__init__(self, name, idnumber)

# creation of an object variable or an instance


a = Employee('Rahul', 886012, 200000, "Intern")

# calling a function of the class Person using its instance


a.display()
Output:
Rahul
886012

Different forms of Inheritance:


1. Single inheritance: When a child class inherits from only one parent class, it is called single inheritance. We
saw an example above.
2. Multiple inheritance: When a child class inherits from multiple parent classes, it is called multiple inheritance.
Unlike java, python shows multiple inheritance.
# Python example to show the working of multiple
# inheritance
class Base1(object):
def __init__(self):
self.str1 = "Good1"
print("Base1")

class Base2(object):
def __init__(self):
self.str2 = "Good2"
print("Base2")

class Derived(Base1, Base2):


def __init__(self):

# Calling constructors of Base1


# and Base2 classes
Base1.__init__(self)
Base2.__init__(self)
print("Derived")

def printStrs(self):
print(self.str1, self.str2)

ob = Derived()
ob.printStrs()
Output:
Base1
Base2
Derived
Good1 Good2

4. Multilevel inheritance: When we have a child and grandchild relationship.


# A Python program to demonstrate inheritance

# Base or Super class. Note object in bracket.


# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"
class Base(object):
# Constructor
def __init__(self, name):
self.name = name

# To get name
def getName(self):
return self.name

# Inherited or Sub class (Note Person in bracket)


class Child(Base):

# Constructor
def __init__(self, name, age):
Base.__init__(self, name)
self.age = age

# To get name
def getAge(self):
return self.age

# Inherited or Sub class (Note Person in bracket)


class GrandChild(Child):

# Constructor
def __init__(self, name, age, address):
Child.__init__(self, name, age)
self.address = address

# To get address
def getAddress(self):
return self.address

# Driver code
g = GrandChild("Good1", 23, "Noida")
print(g.getName(), g.getAge(), g.getAddress())
Output:
Good1 23 Noida

5. Hierarchical inheritance More than one derived classes are created from a single base.
5. Hybrid inheritance: This form combines more than one form of inheritance. Basically, it is a blend of
more than one type of inheritance.

# Python program to demonstrate private members


# of the parent class
class C(object):
def __init__(self):
self.c = 21

# d is private instance variable


self.__d = 42
class D(C):
def __init__(self):
self.e = 84
C.__init__(self)
object1 = D()

# produces an error as d is private instance variable


print(object1.d)
Output :
File "/home/993bb61c3e76cda5bb67bd9ea05956a1.py", line 16, in
print (object1.d)
AttributeError: type object 'D' has no attribute 'd'

Polymorphism
The word polymorphism means having many forms. In programming, polymorphism means the same
function name (but different signatures) being used for different types.
Example of inbuilt polymorphic functions :
# Python program to demonstrate in-built poly-
# morphic functions

# len() being used for a string


print(len("Goods"))

# len() being used for a list


print(len([10, 20, 30]))
Output:
5
3

Examples of user-defined polymorphic functions :

# A simple Python function to demonstrate


# Polymorphism

def add(x, y, z = 0):


return x + y+z

# Driver code
print(add(2, 3))
print(add(2, 3, 4))
Output:
5
9

Polymorphism with class methods:


We create a for loop that iterates through a tuple of objects. Then call the methods without being
concerned about which class type each object is. We assume that these methods actually exist in each
class.
class India():
def capital(self):
print("New Delhi is the capital of India.")

def language(self):
print("Hindi is the most widely spoken
language of India.")

def type(self):
print("India is a developing country.")

class USA():
def capital(self):
print("Washington, D.C. is the capital of
USA.")

def language(self):
print("English is the primary language of
USA.")

def type(self):
print("USA is a developed country.")

obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
country.capital()
country.language()
country.type()

Output:
New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

Polymorphism with Inheritance:


In Python, Polymorphism lets us define methods in the child class that have the same name as the
methods in the parent class. In inheritance, the child class inherits the methods from the parent class.
However, it is possible to modify a method in a child class that it has inherited from the parent class.
This is particularly useful in cases where the method inherited from the parent class doesn‟t quite fit the
child class. In such cases, we re-implement the method in the child class. This process of re-
implementing a method in the child class is known as Method Overriding.

class Bird:
def intro(self):
print("There are many types of birds.")

def flight(self):
print("Most of the birds can fly but some cannot.")

class sparrow(Bird):
def flight(self):
print("Sparrows can fly.")

class ostrich(Bird):
def flight(self):
print("Ostriches cannot fly.")

obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()

obj_bird.intro()
obj_bird.flight()

obj_spr.intro()
obj_spr.flight()

obj_ost.intro()
obj_ost.flight()

Output:
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.

Polymorphism with a Function and objects:


It is also possible to create a function that can take any object, allowing for polymorphism. In this
example, let‟s create a function called “func()” which will take an object which we will name “obj”.
Though we are using the name „obj‟, any instantiated object will be able to be called into this function.
Next, let‟s give the function something to do that uses the „obj‟ object we passed to it. In this case, let‟s
call the three methods, viz., capital(), language() and type(), each of which is defined in the two classes
„India‟ and „USA‟. Next, let‟s create instantiations of both the „India‟ and „USA‟ classes if we don‟t
have them already. With those, we can call their action using the same func() function:
def func(obj):
obj.capital()
obj.language()
obj.type()

obj_ind = India()
obj_usa = USA()

func(obj_ind)
func(obj_usa)

Operator Overloading
Operator Overloading means giving extended meaning beyond their predefined operational meaning. For
example operator + is used to add two integers as well as join two strings and merge two lists. It is
achievable because „+‟ operator is overloaded by int class and str class. You might have noticed that the
same built-in operator or function shows different behavior for objects of different classes, this is
called Operator Overloading.

# Python program to show use of


# + operator for different purposes.

print(1 + 2)

# concatenate two strings


print("Goods"+"For")

# Product two numbers


print(3 * 4)

# Repeat the String

print("Goods"*4)
Output
3
GoodsFor
12
GoodsGoodsGoodsGoods
Output:

3
GoodsFor
12
GoodsGoodsGoodsGoods

How to overload the operators in Python?


Consider that we have two objects which are a physical representation of a class (user-defined data type)
and we have to add two objects with binary „+‟ operator it throws an error, because compiler don‟t know
how to add two objects. So we define a method for an operator and that process is called operator
overloading. We can overload all existing operators but we can‟t create a new operator. To perform
operator overloading, Python provides some special function or magic function that is automatically
invoked when it is associated with that particular operator. For example, when we use + operator, the
magic method __add__ is automatically invoked in which the operation for + operator is defined.

Overloading binary + operator in Python :


When we use an operator on user defined data types then automatically a special function or magic
function associated with that operator is invoked. Changing the behavior of operator is as simple as
changing the behavior of method or function. You define methods in your class and operators work
according to that behavior defined in methods. When we use + operator, the magic method __add__ is
automatically invoked in which the operation for + operator is defined. There by changing this magic
method‟s code, we can give extra meaning to the + operator.
# Python Program illustrate how
# to overload an binary + operator

class A:
def __init__(self, a):
self.a = a

# adding two objects


def __add__(self, o):
return self.a + o.a
ob1 = A(1)
ob2 = A(2)
ob3 = A("Goods")
ob4 = A("For")

print(ob1 + ob2)
print(ob3 + ob4)

Output
3
GoodsFor
Output :

3
GoodsFor

Overloading comparison operators in Python :

# Python program to overload


# a comparison operators

class A:
def __init__(self, a):
self.a = a
def __gt__(self, other):
if(self.a>other.a):
return True
else:
return False
ob1 = A(2)
ob2 = A(3)
if(ob1>ob2):
print("ob1 is greater than ob2")
else:
print("ob2 is greater than ob1")

Output :

ob2 is greater than ob1

Python magic methods or special functions for operator overloading


Binary Operators:
OperatorMagic Method
+ __add__(self, other)
– __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other)
% __mod__(self, other)
** __pow__(self, other)
>> __rshift__(self, other)
<< __lshift__(self, other)
& __and__(self, other)
| __or__(self, other)
^ __xor__(self, other)
Comparison Operators :
OperatorMagic Method
< __lt__(self, other)
> __gt__(self, other)
<= __le__(self, other)
>= __ge__(self, other)
== __eq__(self, other)
!= __ne__(self, other)
Assignment Operators :
OperatorMagic Method
-= __isub__(self, other)
+= __iadd__(self, other)
*= __imul__(self, other)
/= __idiv__(self, other)
//= __ifloordiv__(self, other)
%= __imod__(self, other)
**= __ipow__(self, other)
>>= __irshift__(self, other)
<<= __ilshift__(self, other)
&= __iand__(self, other)
|= __ior__(self, other)
^= __ixor__(self, other)
Unary Operators :
OperatorMagic Method
– __neg__(self)
+ __pos__(self)
~ __invert__(self)
Note: It is not possible to change the number of operands of an operator. For ex. you cannot overload a unary
operator as a binary operator. The following code will throw a syntax error.
# Python program which attempts to
# overload ~ operator as binary operator

class A:
def __init__(self, a):
self.a = a

# Overloading ~ operator, but with two operands


def __invert__(self, other):
return "This is the ~ operator, overloaded as binary operator."

ob1 = A(2)
ob2 = A(3)

print(ob1~ob2)

Abstract classes
An abstract class can be considered as a blueprint for other classes. It allows you to create a set of
methods that must be created within any child classes built from the abstract class. A class which
contains one or more abstract methods is called an abstract class. An abstract method is a method that
has a declaration but does not have an implementation. While we are designing large functional units we
use an abstract class. When we want to provide a common interface for different implementations of a
component, we use an abstract class.
Why use Abstract Base Classes :
By defining an abstract base class, you can define a common Application Program Interface(API) for a
set of subclasses. This capability is especially useful in situations where a third-party is going to provide
implementations, such as with plugins, but can also help you when working in a large team or with a
large code-base where keeping all classes in your mind is difficult or not possible.
How Abstract Base classes work :
By default, Python does not provide abstract classes. Python comes with a module that provides the base
for defining Abstract Base classes(ABC) and that module name is ABC. ABC works by decorating
methods of the base class as abstract and then registering concrete classes as implementations of the
abstract base.
# Python program showing
# abstract base class work

from abc import ABC, abstractmethod

class Polygon(ABC):

@abstractmethod
def noofsides(self):
pass

class Triangle(Polygon):

# overriding abstract method


def noofsides(self):
print("I have 3 sides")

class Pentagon(Polygon):

# overriding abstract method


def noofsides(self):
print("I have 5 sides")

class Hexagon(Polygon):

# overriding abstract method


def noofsides(self):
print("I have 6 sides")

class Quadrilateral(Polygon):

# overriding abstract method


def noofsides(self):
print("I have 4 sides")

# Driver code
R = Triangle()
R.noofsides()

K = Quadrilateral()
K.noofsides()

R = Pentagon()
R.noofsides()

K = Hexagon()
K.noofsides()
Output:

I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides

# Python program showing


# abstract base class work

from abc import ABC, abstractmethod


class Animal(ABC):

def move(self):
pass

class Human(Animal):

def move(self):
print("I can walk and run")

class Snake(Animal):

def move(self):
print("I can crawl")

class Dog(Animal):

def move(self):
print("I can bark")

class Lion(Animal):

def move(self):
print("I can roar")

# Driver code
R = Human()
R.move()

K = Snake()
K.move()

R = Dog()
R.move()

K = Lion()
K.move()
Output:

I can walk and run


I can crawl
I can bark
I can roar

Implementation Through Subclassing :


By subclassing directly from the base, we can avoid the need to register the class explicitly. In this case,
the Python class management is used to recognize PluginImplementation as implementing the abstract
PluginBase.

# Python program showing


# implementation of abstract
# class through subclassing

import abc

class parent:
def Goods(self):
pass

class child(parent):
def Goods(self):
print("child class")

# Driver code
print( issubclass(child, parent))
print( isinstance(child(), parent))
Output:

True
True

Exception Handling
Difference between Syntax Error and Exceptions
Syntax Error: As the name suggests this error is caused by the wrong syntax in the code. It leads to the
termination of the program.
# initialize the amount variable
amount = 10000

# check that You are eligible to


# purchase Dsa Self Paced or not
if(amount > 2999)
print("You are eligible to purchase Dsa Self Paced")
Exceptions: Exceptions are raised when the program is syntactically correct, but the code resulted in an
error. This error does not stop the execution of the program, however, it changes the normal flow of the
program.
# initialize the amount variable
marks = 10000

# perform division with 0


a = marks / 0
print(a)

Try and Except Statement – Catching Exceptions


Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions
are kept inside the try clause and the statements that handle the exception are written inside except clause.
# Python program to handle simple runtime error
#Python 3

a = [1, 2, 3]
try:
print ("Second element = %d" %(a[1]))

# Throws error since there are only 3 elements in array


print ("Fourth element = %d" %(a[3]))

except:
print ("An error occurred")
Output
Second element = 2
An error occurred

Catching Specific Exception


A try statement can have more than one except clause, to specify handlers for different exceptions. Please note
that at most one handler will be executed. For example, we can add IndexError in the above code. The general
syntax for adding specific exceptions are –
try:
# statement(s)
except IndexError:
# statement(s)
except ValueError:
# statement(s)
# Program to handle multiple errors with one
# except statement
# Python 3

def fun(a):
if a < 4:
# throws ZeroDivisionError for a = 3
b = a/(a-3)

# throws NameError if a >= 4


print("Value of b = ", b)

try:
fun(3)
fun(5)

# note that braces () are necessary here for


# multiple exceptions
except ZeroDivisionError:
print("ZeroDivisionError Occurred and Handled")
except NameError:
print("NameError Occurred and Handled")
Output
ZeroDivisionError Occurred and Handled
If you comment on the line fun(3), the output will be
NameError Occurred and Handled

Try with Else Clause


In python, you can also use the else clause on the try-except block which must be present after all the except
clauses. The code enters the else block only if the try clause does not raise an exception.
# Program to depict else clause with try-except
# Python 3
# Function which returns a/b
def AbyB(a , b):
try:
c = ((a+b) / (a-b))
except ZeroDivisionError:
print ("a/b result in 0")
else:
print (c)

# Driver program to test above function


AbyB(2.0, 3.0)
AbyB(3.0, 3.0)
Output:
-5.0
a/b result in 0

Finally Keyword in Python


Python provides a keyword finally, which is always executed after the try and except blocks. The final block
always executes after normal termination of try block or after try block terminates due to some exception.
Syntax:
try:
# Some Code....

except:
# optional block
# Handling of exception (if required)

else:
# execute if no exception

finally:
# Some code .....(always executed)
# Python program to demonstrate finally

# No exception Exception raised in try block


try:
k = 5//0 # raises divide by zero exception.
print(k)

# handles zerodivision exception


except ZeroDivisionError:
print("Can't divide by zero")

finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
Output:
Can't divide by zero
This is always executed

Try

The try block lets you test a block of code for errors.
The except block lets you handle the error.
The else block lets you execute code when there is no error.
The finally block lets you execute code, regardless of the result of the try- and except blocks.

Exception Handling

When an error occurs, or exception as we call it, Python will normally stop and generate an error message.

try:
print(x)
except:
print("An exception occurred")
Else

You can use the else keyword to define a block of code to be executed if no errors were raised:

try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
C:\Users\My Name>python demo_try_except5.py
Something went wrong when writing to the file

You might also like