Python Unit 3 Notes
Python Unit 3 Notes
class class_name:
<statement 1>
<statement 2>
.
.
<statement N>
Consider the following example to create a class Employee which contains two fields as
Employee id, and name.
The class also contains a function display(), which is used to display the information of
the Employee.
Example
class Employee:
id = 10
name = "Santosh"
def display (self):
print(self.id,self.name)
Here, the self is used as a reference variable, which refers to the current class object. It is always
the first argument in the function definition.
The self-parameter
The self-parameter refers to the current instance of the class and accesses the class variables.
We can use anything instead of self, but it must be the first parameter of any function which
belongs to the class.
It is physically existed
EX1:
class Employee:
id = 10
name = "santosh"
def display (self):
print(self.id,self.name)
1
# Creating a emp instance of Employee class (object creation)
emp = Employee()
emp.display()
class stud:
rno=0
name=0
def read(self):
self.rno=int(input("enter roll no"))
self.name=input("enter name")
def dis(self):
print(self.rno,self.name)
s1=stud()
s1.read()
s1.dis()
Default constructor:
A constructor without parameters is called as default constructor
parameterized constructor:
2
Ex: class test:
name = 0
branch=0
def__ init__(self,p1,p2):
self.name=p1
self. branch = p2
t=test(“Santhosh”,”it”)
3
INHERITANCE
The process of deriving a new class from the existing class is called as inheritance.
Existing class is called as parent/base/super class.
New class is called as derived/child/sub class.
ADVANTAGES:
1)Code reusability.
2) Reducing the code size.
3)Easy to understand.
4)Easy to debug.
SYNTAX:
class base_class_name:
Statements
class derived_class_name(base_class_name):
Statements
TYPES OF INHERITANCE
There are 6 types of inheritance.There are:
1) Single inheritance
2) Multiple inheritance
3) Multilevel inheritance
4) Hierarchical inheritance
5) Hybrid inheritance
6) Multipath inheritance
4
Single Inheritace:
Single inheritance enables a derived class to inherit properties from a single parent
class, thus enabling code reusability and the addition of new features to existing
code.
5
Multiple Inheritance:
When a class can be derived from more than one base class this type of
inheritance is called multiple inheritances. In multiple inheritances, all the features
of the base classes are inherited into the derived class.
6
Multilevel Inheritance :
In multilevel inheritance, features of the base class and the derived class are
further inherited into the new derived class. This is similar to a relationship
representing a child and a grandfather.
7
Hierarchical Inheritance:
When more than one derived class are created from a single base this type of
inheritance is called hierarchical inheritance. In this program, we have a parent
(base) class and two child (derived) classes.
8
Hybrid Inheritance:
9
Multipath inheritance:
Multipath Inheritance in C++ is derivation of a class from other derived classes,
which are derived from the same base class. This type of inheritance involves other
inheritance like multiple, multilevel, hierarchical etc. Here class D is derived from
class B and C. Class B and C are child of class A.
10
Super class():
The super() function is used to give access to methods and properties of a parent
or sibling class. The super() function returns an object that represents
the parent class.
super().variablename
11
Built in functions for accessing the class attributes
There are 4 build in functions used to work with the attributes of a class:
1) hasattr() :This function is used to check the existence of an attribute in a
class.If attribute is existed it returns true otherwise false.
SYNTAX: hasattr(class_name , 'attribute.name')
2) setattr() :This function is used to create a new attribute or changing the
existing attribute value.
SYNTAX: setattr(class_name, ' attribute _name' , value)
3) getattr(): This function is used to access the attribute value.If attribute is
existed it will display the value otherwise it raises an error.
SYNTAX: getattr(class_name , ' attribute_name')
4) delattr():This function is used to delete the attribute.If attribute is existed it
returns none otherwise it raises an error.
SYNTAX: delattr(class_name, ' attribute _name')
12
Abstract class:
It is an incomplete class
An abstract class may contain one or more abstract methods
Abstract method:
A method without definition is called as abstract method
We cannot create an object for abstract class because it is incomplete.
In python it is achieve by using abc package(abstract base class)
SYNTAX:
from abc import ABC, abstract method
This abstract class can be implemented by using inheritance.
It means we are providing the definition for the abstract methods in derived
class and making that incomplete class as complete.
13
14