1523_67_122_Module_4
1523_67_122_Module_4
- Classes
- Objects
- Encapsulation
- Data hiding
- Inheritance
- polymorphism
Creating a class:
statements
Creating an object:
s1 = student()
__init__() function:
- All classes have a function called __init__(), which is always executed when the class is
being initiated.
- Use the __init__() function to assign values to object properties, or other operations that are
necessary to do when the object is being created
Constructors:
Syntax:
def __init__(self):
# body of destructor
Inheritance
- Mechanism of deriving a new class from an existing class.
- Parent class is the class being inherited from, also called base class.
- Child class is the class that inherits from another class, also called derived
class.
Types of Inheritance:
1) Single-level
2) Mutli-level
3) multiple
Single inheritance:
Test: marks
Multi-level inheritance:
Test: marks
Student: rollno,name
Test:marks Sports: sp
- Mechanism for hiding the data of a class from the outside world.
- In a class, data may be private or public.
- Private data members are not accessible by the outside world. It can be
accessed only by the member functions of the same class.
- Such data are to be prefixed with double underscore.
HYBRID INHERITANCE
Method Resolution Order
MRO is a concept used in inheritance. It is the order in which a method is searched for in a classes hierarchy
and is especially useful in Python because Python supports multiple inheritance.
In Python, the MRO is from bottom to top and left to right. This means that, first, the method is searched in the
class of the object. If it’s not found, it is searched in the immediate super class. In the case of multiple super
classes, it is searched left to right, in the order by which was declared by the developer. For example:
Polymorphism
- Means many forms.
- Eg: + operator :
- used to perform arithmetic addition operation. Or it can be used to perform
concatenation.
- Thus it can carry out different operations for distinct data types.
Function Polymorphism:
product(4, 5)
Class polymorphism:
- Python allows different classes to have methods with the same name.
Operator Overloading
- We can change the meaning of an operator depending upon the operands used.
- For example, the + operator will perform arithmetic addition on two numbers, merge two lists, or
concatenate two strings.
- The feature that allows the same operator to have different meaning according to the context is called
operator overloading.
Special Functions:
- Class functions that begin with double underscore __ are called special functions in Python.
__str__ method:
Syntax errors
Logical errors (Exceptions)
Exception:
- is an abnormal condition that is caused by a runtime error in the program.
- Disturbs the normal flow of the program.
- If it is not handled properly, the program will be terminated abruptly without
completing the remaining portions of the program.
- Some built-in exceptions are:
- ArithmeticError: deals with numeric calculations.
- TypeError: raised when an invalid operation is made for a specified datatype.
- ImportError: raised when an import statement fails.
- IndentationError: raised when indentation is not specified properly.
- IndexError: raised when an index is not found in a sequence.
- ZeroDivisionError: raised when division or modulo by zero is done.
- KeyError: Raised when a key is not found in a dictionary.
- NameError: Raised when a variable is not found in local or global scope.
Handling Exceptions:
Syntax:
try:
a=10
b=0
print(a/b)
except ArithmeticError:
print('sum is ',a+b)
print('difference is ',a-b)
The except Clause with No Exceptions
Except clause with multiple exceptions:
Syntax:
try...finally:
- The finally block is a place to put any code that must execute, whether the
try-block raised an exception or not.
- We cannot use else and finally clauses together with a try block.
- It is generally used to release external resources(like closing a file,
disconnecting a network)
Syntax:
try:
Statements
finally:
finally_Statements #executed always after the try block
Raising an exception: