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

Python Ch4

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

Python Ch4

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

CHAPTER 4

Classes & Modules


in Python

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
 Python is an object-oriented programming language.
Almost everything in Python is an object, with its
properties and methods.
 A Class is like an object constructor, or a "blueprint"
for creating objects.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Create a Class
 To create a class, use the keyword class
#create a class named MyClass, with a property named x

class MyClass:
x = 5

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Create Object
 Now we can use the class named MyClass to create
objects
# create an object named p1, and print the value of x
p1 = MyClass()
print(p1.x)
# example of class and object
class MyClass:
x = 5

p1 = MyClass()
print(p1.x)

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Create Object
# example of class and object
class MyClass:
x = 5

p1 = MyClass()
print(p1.x)
Output :
5

The examples above are classes and objects in their


simplest form, and are not really useful in real life
applications.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
The __init__() Function
 To understand the meaning of classes we have to
understand the built-in __init__() function.
 All classes have a function called __init__(), which is
always executed when the class is being initiated.
The__init__() function is used to assign values to
object properties and other operations that are
necessary to do when the object is being created.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
The __init__() Function
# create a class named Person
# use the __init__() function to assign values for name and
age

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

p1 = Person("John", 36)

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
The __init__() Function
print(p1.name)
print(p1.age)

Output:
John
36

Note: The __init__() function is called automatically every


time the class is being used to create a new object.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Object Methods
 Objects can also contain methods. Methods in objects are functions that belong to the
object.
#function that prints a greeting, and execute it on the p1
object

class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Object Methods
p1 = Person("John", 36)
p1.myfunc()
Output:
Hello my name is John

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
The self Parameter
 The self parameter is a reference to the current instance of the class, and is used to
access variables that belongs to the class.
 It does not have to be named self , you can call it whatever you like, but it has to be
the first parameter of any function in the class.
 #use the words myobject and arg instead of self

class Person:

def __init__(myobject, name, age):


myobject.name = name
myobject.age = age

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
The self Parameter
def myfunc(arg):
print("Hello my name is " + arg.name)

p1 = Person("John", 36)
p1.myfunc()

Output:
Hello my name is John
`

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Modify Object Properties
You can modify properties on objects like this
# Set the age of p1 to 40

class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Modify Object Properties
p1 = Person("John", 36)
p1.age = 40

print(p1.age)
Output:
40

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Delete Object Properties or Object
 You can delete properties of objects or object by using the del keyword.
# delete the p1 object

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def myfunc(self):
print("Hello my name is " + self.name)

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Delete Object Properties or Object
p1 = Person("John", 36)
del p1.age
print(p1.age)
Output:
AttributeError Traceback (most
recent call last)
<ipython-input-16-46454e0e6825> in <module>
11 del p1.age
12
---> 13 print(p1.age)
AttributeError: 'Person' object has no attribute 'age'

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Delete Object Properties or Object
#delete the age property from the p1 object

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def myfunc(self):
print("Hello my name is " + self.name)

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Delete Object Properties or Object
p1 = Person("John", 36)
del p1
print(p1)

Output:
NameError Traceback (most
recent call last)
<ipython-input-17-8d9ca57d628a> in <module>
11 del p1
12
---> 13 print(p1)
NameError: name 'p1' is not defined

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Delete Object Properties or Object
p1 = Person("John", 36)
del p1
print(p1)

Output:
NameError Traceback (most
recent call last)
<ipython-input-17-8d9ca57d628a> in <module>
11 del p1
12
---> 13 print(p1)
NameError: name 'p1' is not defined

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Inheritance
 Inheritance is a way of creating a new class for using details of an existing class without
modifying it. The newly formed class is a derived class (or child class). Similarly, the
existing class is a base class (or parent class).
 Inheritance is used as given below in Python:
# parent class
class Bird:
def __init__(self):
print("Bird is ready")
def whoisThis(self):
print("Bird")
def swim(self):
print("Swim faster")

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Inheritance
# child class
class Penguin(Bird):
def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")
def whoisThis(self):
print("Penguin")
def run(self):
print("Run faster")

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Inheritance
print("Penguin is ready")
def whoisThis(self):
print("Penguin")
def run(self):
print("Run faster")
Output:
Bird is ready
Penguin is ready
Penguin
Swim faster
Run faster

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Inheritance
 In the above program, we created two classes i.e. Bird (parent
class) and Penguin (child class). The child class inherits the
functions of parent class. We can see this from
the swim() method.
 Again, the child class modified the behavior of the parent class.
We can see this from the whoisThis() method. Furthermore, we
extend the functions of the parent class, by creating a
new run() method.
 Additionally, we use the super() function inside the __init__()
method.This allows us to run the __init__() method of the parent
class inside the child class.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Polymorphism
 Polymorphism is an ability (in OOP) to use a common interface
for multiple forms (data types).
 Suppose, we need to color a shape, there are multiple shape
options (rectangle, square, circle). However we could use the
same method to color any shape. This concept is called
Polymorphism.
 Following is the example of Polymorphism in Python:

class Parrot:
def fly(self):
print("Parrot can fly")
def swim(self):
print("Parrot can't swim")

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Polymorphism
class Penguin:
def fly(self):
print("Penguin can't fly")
def swim(self):
print("Penguin can swim")
# common interface
def flying_test(bird):
bird.fly()
#instantiate objects
blu = Parrot()
peggy = Penguin()
# passing the object
flying_test(blu)
flying_test(peggy)
Output:
Parrot can fly
Penguin can't fly

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Polymorphism
In the above program, we defined two classes Parrot and Penguin.
Each of them have a common fly() method. However, their
functions are different.

To use polymorphism, we created a common interface


i.e flying_test() function that takes any object and calls the
object's fly() method. Thus, when we passed
the blu and peggy objects in the flying_test() function, it ran
effectively.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Polymorphism
In the above program, we defined two classes Parrot and Penguin.
Each of them have a common fly() method. However, their
functions are different.

To use polymorphism, we created a common interface


i.e flying_test() function that takes any object and calls the
object's fly() method. Thus, when we passed
the blu and peggy objects in the flying_test() function, it ran
effectively.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Modules
 In Python, a module is a self-contained Python file that contains
Python statements and definitions, like a file named GFG.py, can
be considered as a module named GFG which can be imported
with the help of import statement.
 However, one might get confused about the difference between
modules and packages.
 A package is a collection of modules in directories that give
structure and hierarchy to the modules.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Creating Modules
 A module is simply a Python file with a .py extension that can be
imported inside another Python program.
 The name of the Python file becomes the module name.
 The module contains definitions and implementation of classes,
variables, and functions that can be used inside another
program.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
Creating Modules
# Example
 Save the file name with Factorial.py writing below program and save in the same folder.

def fact(n):
if n==0 or n==1:
return(1)
else:
return(n*fact(n-1))

 Create new file named Test Module.py and execute below program.

import Factorial
print(Factorial.fact(6))

Output: 120

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
pip and pypi
Python pip is the package manager for Python packages. We can use pip to install
packages that do not come with Python. The basic syntax of pip commands in command
prompt is:
pip 'arguments'
How to install pip?
Python pip comes pre-installed on 3.4 or older versions of Python. To check whether pip is
Installed or not type the below command in the terminal.
pip –version
This command will tell the version of the pip if pip is already installed in the system.
We can install additional packages by using the Python pip install command. Let’s suppose
we want to install the numpy using pip. We can do it using the below command.
pip install numpy

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
pip and pypi
The Python pip list command displays a list of packages installed in the system.
pip list

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
pip and pypi
 The Python pip uninstall command uninstalls a particular existing package.
pip uninstall numpy
 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.

Dept. of Computer/IT Engineering LJ Polytechnic


Python Classes/Objects
pip and pypi
PyPI
 The Python Package Index, abbreviated as PyPI, is the official repository of software for
the Python programming language. By default, pip — which is the most popular
Python package manager — uses PyPI as the source for retrieving package
dependencies.
 PyPI lets you find, install and even publish your Python packages so that they are
widely available to the public. More than 300,000 different packages are currently
published in the index with more than 2,500,000 releases being distributed to users.

Dept. of Computer/IT Engineering LJ Polytechnic

You might also like