0% found this document useful (0 votes)
30 views

Python Unit 3 Notes

Python programming

Uploaded by

abdul7ahad73
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Python Unit 3 Notes

Python programming

Uploaded by

abdul7ahad73
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Class:

 It is a user defined data type


 Class binds the data members and functions ( to perform a task) into a single unit.
 Class is a blueprint/logical structure or code template for object creation.
 Using a class, you can create as many objects as you want.
 In Python, class is defined by using the class keyword. The syntax to create a class is
given below.
Syntax

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.

Object: An object is an instance of a class. It is a collection of attributes (variables/data


members) and functions. We use the object of a class to perform actions.

It is physically existed

Syntax: object-name = class-name(<arguments>)

Accessing class members = object-name.varable name


Accessing class functions= object-name.function name(parameters(optional))

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()

Ex2: Read student details and display it.

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()

Constructor: It is a special function.


It is called automatically when an object is created.
It is used to initialize the object data.
In python constructor is declared with the name _init_(). Constructor are two types:
1) Default Constructor
2) Parameterized Constructor

Default constructor:
A constructor without parameters is called as default constructor

Syntax: objectname = classname()


Ex: class test:
name = 0
branch=0
def __init__(self):
self.name="santosh"
self. branch = " MCA"
t=test()

parameterized constructor:

a constructor with parameters is called parameterized constructor

To call the parameterized constructor syntax is


syntax: objectname=classname (parameters)

2
Ex: class test:
name = 0
branch=0
def__ init__(self,p1,p2):
self.name=p1
self. branch = p2
t=test(“Santhosh”,”it”)

Destructor:It is a special function.It is called automatically when an object is deleted


from the memory.
It is used to identify the existence of object.
In python destructor is created by
SYNTAX: def _del_(self):
Statement
To destroy the object from memory
SYNTAX:del.object_name

Ex: class stud:


def __init__(s):
print("con called auto ")
def__del__(s):
print("des called auto”)
s1=stud()
del s1

Destructor won't support parameters

ACCESS SPECIPIERS OR MODIFIER:


These are used to control the accessibility of data.
There are three types of access specifiers in python:
1) Public
2) Protected
3) Private
Python by default support public specifier.
Access specifiers decides the scope of accessibility of data or a function in a class.

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.

A(Parent/base/super class) to B( 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.

A (Base class) ->B(derived class)

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:

Inheritance consisting of multiple types of inheritance is called 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.

SYNTAX: super().function name

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

You might also like