Unit 5 CTPS
Unit 5 CTPS
Module - V
Unit – V (OOPs, FILES, MODULES, PACKAGES)
OUTLINE
OOPs concepts FILES AND MODULES
▪ Class ▪ Introduction
▪ Object ▪ Files and file handling
▪ Inheritance-Types of inheritance, ▪ Exceptions and Exception handling
▪ Polymorphism- Types of Polymorphism ▪ Modules
▪ Encapsulation ▪ Packages, Importing Module from a
▪ Abstraction, Methods and Constructure Package
Module V
OOPs, FILES, MODULES, PACKAGES
Class
I ntroduc tion Python Using Capstone Projec t
Object 12/4/2024 5
Module V Python Objects and Classes
OOPs, FILES, MODULES,
PACKAGES
# Example for create an object named p1, and print the value of x:
▪ Let’s understand the concept of parent class and child class with an
example.
▪ As we can see in the image, a child inherits the properties from the father.
Similarly, in python, there are two classes:
12/4/2024 57
Module V OOPs, FILES, MODULES, PACKAGES
▪ In Python, you create a parent (or base) class just like any
other class.
Single Inheritance:
Single Inheritance:
▪ In this code, the Child class inherits from the Parent class and overrides the
greet method.
▪ However, it still calls the greet method of the Parent class using the super()
function.
Single Inheritance:
Example 3
Multiple Inheritance:
▪ The child class, after inheriting properties from various parent classes, has
access to all of its objects.
Example:
In multiple inheritances, the child class first searches for the method in
its own class. If not found, then it searches in the parent classes
depth_first and left-right order.
Since this was an easy example with just two parent classes, we can
clearly see that class Parent_1 was inherited first, so the child class will
search the method in Parent_1 class before searching in class Parent_2.
This tells that the Child class first visited the class Parent_1 and
then Parent_2, so the f1() method of Parent_1 will be called.
I ntroduc tion Python Using Capstone Projec t 12/4/2024 70
Module V OOPs, FILES, MODULES, PACKAGES
In this Child_1 has access to functions f1() and f2() whereas Child_2 has access
to functions f1(), f2() and f3().
If we try to access the function f3() using the object of class Class_1, then an
error will occur stating:
▪ So here classes “BMW” and “Audi” are child classes whereas “Car” is a parent
class. We have provided no additional features and methods in the class
BMW whereas there is one additional method inside the class Audi.
As we can see here, the base class of both sub-classes is Car. Now, let’s see
what happens when using “ __base__” with the parent class “Car”:
Whenever we create a new class in Python 3.x, it is inherited from a built-in basic
class
I ntroduc tion called
Python Object.
Using Capstone Projec In
t other words, the Object class is the root of all classes.
12/4/2024 77
Module V OOPs, FILES, MODULES, PACKAGES
Variables
Encapsulation Methods Scope
functions
Figure 10: Demonstration of Encapsulation
Public Private
Example Example
class encap: class encap:
car=“toyota” __car=“toyota”
def brand(self): __def brand(self):
print(“toyota brand ”) print(“toyota brand ”)
Note: The scope of pubic variables/methods/functions are converted to private by entering double
underscore (__) the variable/function/methods.
Demonstration: Abstract& Concrete Class
Polymorphism in Python
▪ The key difference is the data types and number of arguments used in
function.
Polymorphism in Python
▪ As part of polymorphism, a Python child class has methods with the same
name as a parent class method. This is an essential part of programming.
Polymorphism in Python
Examples of user-defined polymorphic functions:
print(add(2, 3))
print(add(2, 3, 4))
Polymorphism in Python
Polymorphism with class methods:
▪ The below code shows how Python can use two different class types, in the
same way.
▪ Then call the methods without being concerned about which class type each
object is.
▪ In inheritance, the child class inherits the methods 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.
I ntroduc tion Python Using Capstone Projec t 12/4/2024 90
Module V OOPs, FILES, MODULES, PACKAGES
Polymorphism in Python
Polymorphism with Inheritance:
Polymorphism in Python
Using Inheritance and Method Overriding:
▪ Modules
File Handling
n
▪ In this section we will use Python’s open built in functions to
create a file object and obtain the data from the txt file.
Output
▪ Once the method .read(4) is called the first 4 characters are called.
The method .write() works similar to the method .readline(), except instead of reading a new line it
writes a new line.
You can check the file to see if your results are correct
Exception Description
Raised when a division or modulo operation is
ZeroDivisionError
attempted with zero.
Raised when a function receives an argument of
ValueError
the correct type but an inappropriate value.
Raised when an operation or function is applied
TypeError
to an object of inappropriate type.
Raised when a sequence (like a list) is indexed with an
IndexError
out-of-range index.
Raised when a dictionary is accessed with a key that
KeyError
does not exist.
Common Exceptions in Python
Exception Description
Raised when trying to open a file that does not
FileNotFoundError
exist.
Raised when an invalid attribute reference or
AttributeError
assignment is attempted.
Raised when an import statement fails to find
ImportError
the module definition.
NameError Raised when a local or global name is not found.
Common Exceptions in Python
Exception Description
Raised when an I/O operation fails (e.g., when a
IOError
file cannot be opened).
Raised when the result of an arithmetic operation
OverflowError
is too large to be expressed.
IndentationError Raised when the code is not properly indented.
Raised when an error occurs that doesn't fall
RuntimeError
into any other category.
Exception Handling
• These are mechanism provided by the interpreter to handle exception that allows program
to run smoothly without termination.
• Python uses Try, Except, Else, and Finally blocks to handle exceptions.
Table 5: Basic syntax for handling exceptions
*risky_operation() End_operation()
*handle_exception( ) *successful_peration()
MODULES AND PACKAGES
➢Modules
➢Packages
Modules in Python
▪ If you quit from the Python interpreter and enter it again, the
definitions you have made (functions and variables) are lost.
▪ You may also want to use a handy function that you’ve written in several
programs without copying its definition into each program.
I ntroduc tion Python Using Capstone Projec t 12/4/2024 134
Module V OOPs, FILES, MODULES, PACKAGES
Modules in Python
▪ To support this, Python has a way to put definitions in a file and use
them in a script or in an interactive instance of the interpreter. Such
a file is called a module;
Modules in Python
Modules in Python
▪ For instance, use your favorite text editor to create a file called fibo.py
in the current directory with the following contents:
0, 1, 1, 2, 3, 5, 8, 13, 21,
I ntroduc tion Python Using Capstone Projec t 34, 55,12/4/2024
89, 144, ... 138
Module V OOPs, FILES, MODULES, PACKAGES
▪ Now enter the Python interpreter and import this module with the
following command:
▪ This does not add the names of the functions defined in fibo
directly to the current namespace; it only adds the module name fibo
there.
Modules in Python
Modules in Python
Modules in Python
Python Import from Module
Modules in Python
Import all Names
▪ The * symbol used with the import statement is used to import all the
names from a module to a current namespace.
▪ If you know exactly what you will be needing from the module, it is
not recommended to use *, else do so.
Modules in Python
What does import * do in Python
# importing sqrt() and factorial from the module math
print(sqrt(16)) Output
print(factorial(6))
4.0
720
I ntroduc tion Python Using Capstone Projec t 12/4/2024 146
Module V OOPs, FILES, MODULES, PACKAGES
Modules in Python
Locating Python Modules
▪ First, it will check for the built-in module, if not found then it looks for a
list of directories defined in the sys.path.
Modules in Python
Locating Python Modules
▪ If the module isn’t found in the current directory, Python then searches
each directory in the shell variable PYTHONPATH.
Modules in Python
Locating Python Modules
Modules in Python
Locating Python Modules
Modules in Python
Renaming the Python module
# importing sqrt() and factorial from the module math
import math as mt
print(mt.sqrt(16))
print(mt.factorial(6))]
Modules in Python
Python Built-in modules: There are several built-in modules in
Python, which you can import whenever you like.
Modules in Python
Modules in Python
▪ We can use PIP to install packages that do not come with Python.
Modules in Python
Modules in Python
Modules in Python
How to Install Package with Python PIP
Modules in Python
▪ We can use the Python pip show command to display the details of
a particular package.
Modules in Python
Note:
Modules in Python
Get a list of locally installed Python Modules using Python PIP
Modules in Python
Uninstall Packages with Python PIP
Note: The PIP uninstall command does not uninstall the package
dependencies.
▪ If you want to remove the dependencies as well then you can see the
dependencies using the pip show command and remove each package
manually.
▪ We can search for a particular existing package using the Python pip
search command.
I ntroduc tion Python Using Capstone Projec t 12/4/2024 161
Module V OOPs, FILES, MODULES, PACKAGES
Modules in Python
Using Requirement files with Python PIP
▪ Let’s suppose you want more than one package then instead of installing
each package manually, you can install all the modules in a single go.
Modules in Python
Listing additional Packages with Python PIP
▪ The Python pip freeze command is used to list packages that don’t come
pre-installed with Python.
▪ We can also upgrade any package to a specific version using the below
command.