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

Python Unit 4

This document provides an overview of classes and modules in Python, detailing how to create classes, instantiate objects, and utilize the __init__() function. It also covers concepts like inheritance, polymorphism, and the use of modules, including how to create and import them. Additionally, it discusses the Python package manager pip and the Python Package Index (PyPI) for managing packages.

Uploaded by

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

Python Unit 4

This document provides an overview of classes and modules in Python, detailing how to create classes, instantiate objects, and utilize the __init__() function. It also covers concepts like inheritance, polymorphism, and the use of modules, including how to create and import them. Additionally, it discusses the Python package manager pip and the Python Package Index (PyPI) for managing packages.

Uploaded by

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

Python Programming Unit-2 Notes

CHAPTER 4 - Classes & Modules in Python

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.

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

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)

The __init__() Function


The examples above are classes and objects in their simplest form, and are not really useful in real
life applications.
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.
Python Programming Unit-2 Notes

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

print(p1.name)
print(p1.age)

John
36

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

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

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

Hello my name is John


Python Programming Unit-2 Notes

Modify Object Properties


You can modify properties on objects like this

p1.age = 40

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

p1 = Person("John", 36)

print(p1.age)

p1.age = 40

print(p1.age)

36
40
Hello my name is John

Delete Object Properties or Object


You can delete properties of objects or object by using the del keyword

del p1.age #delete the age property from the p1 object

del p1 # delete the p1 object


Python Programming Unit-2 Notes

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

p1 = Person("John", 36)
print(p1.age)

del p1.age
print(p1.age)

del p1
print (p1)

36

AttributeError Traceback (most recent call last)


<ipython-input-16-46454e0e6825> in <module>
12 del p1.age
---> 13 print(p1.age)
AttributeError: 'Person' object has no attribute 'age'

NameError Traceback (most recent call last)


15 del p1
---> 16 print(p1)
NameError: name 'p1' is not defined
Python Programming Unit-2 Notes

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 who(self):
print("Bird")

def swim(self):
print("Swim faster")

# child class
class Penguin(Bird):

def __init__(self):
print("Penguin is ready")
# call super() function
super().__init__()

def who(self):
print("Penguin")

def run(self):
print("Run faster")

peggy = Penguin()
peggy.who()
peggy.swim()
peggy.run()

Penguin is ready
Bird is ready
Penguin
Swim faster
Run faster
Python Programming Unit-2 Notes

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.

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

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)
Python Programming Unit-2 Notes

Following is the output:

Parrot can fly


Penguin can't fly

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.

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.

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.

# Factorial.py

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

#test.py

import Factorial
n=int(input(“Enter the value: “))
print(Factorial.fact(n))

Now enter the Python interpreter and import this module with the following command:

>>> Enter the value: 4


>>>24
Python Programming Unit-2 Notes

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

The Python pip list command displays a list of packages installed in the system.

pip list
Python Programming Unit-2 Notes

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.

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.

You might also like