Introduction to Python
Programming
22PLC202
Module 5
Think Python
By
Allen B Downey
Classes and objects
Classes and functions
Classes and methods
OOPs Concepts in Python
• Class
• Objects
• Polymorphism
• Encapsulation
• Inheritance
• Data Abstraction
Classes and Objects
• Till Now
• how to use functions
• to organize code and built-in types to organize data.
• Now
• learn “object-oriented programming”, which uses programmer defined
types to organize both code and data.
• Object-oriented programming is a big topic; let us get to know the glimpse
of it
Programmer-defined Data types
• A class is a user-defined blueprint or prototype from which objects
are created.
• Classes provide a means of bundling data and functionality
together.
• Creating a new class creates a new type of object, allowing new
instances of that type to be made.
• Each class instance can have attributes attached to it for
maintaining its state.
• Class instances can also have methods (defined by their class) for
modifying their state.
• Consider an example, to create a type called Point that represents
a point in two-dimensional space.
• For example, (0, 0) represents the origin, and (x, y) represents the
point x units to the right and y units up from the origin.
• There are several ways to represent points in Python:
• store the coordinates separately in two variables, x and y.
• store the coordinates as elements in a list or tuple.
• create a new type to represent points as objects - Creating a new type is
more complicated than the other options, but it has advantages.
Python class
• Classes are created by keyword class followed by a name, followed
by colon(:).
• Example : class is a keyword, Point is the name of the class
class Point:
Object of Python Class
• An Object is an instance of a Class.
• A class is like a blueprint while an instance is a copy of the class
with actual values.
• An object consists of:
• State: It is represented by the attributes of an object. It also reflects the
properties of an object.
• Behavior: It is represented by the methods of an object. It also reflects the
response of an object to other objects.
• Identity: It gives a unique name to an object and enables one object to
interact with other objects.
• Objects can be passed as parameters to functions
Attributes of Object
• Attributes are the variables that belong to a class.
• Attributes are always public and can be accessed using the dot (.)
operator and assign values to an instance
Rectangles
• Design a class to represent rectangles.
• Attributes should specify the location and size of a rectangle
• There are at least two possibilities:
• specify one corner of the rectangle (or the center), the width, and the
height.
• specify two opposing corners.
class Rectangle:
box = Rectangle() # object
box.width = 100.0 # Attribute
box.height = 200.0 # Attribute
box.corner = Point() # Attribute of type object
box.corner.x = 0.0
box.corner.y = 0.0
• box.corner.x means, “Go to the object box and select the attribute
named corner; then go to that object and select the attribute
named x.”
Instances as return values
• Functions can return instances. def find_center(rect):
p = Point()
• For example, p.x = rect.corner.x + rect.width/2
• find_center takes a Rectangle as an argument p.y = rect.corner.y + rect.height/2
and returns a Point that contains the
coordinates of the center of the Rectangle: return p
def find_center(rect):
p = Point()
p.x = rect.corner.x + rect.width/2 center = find_center(box)
p.y = rect.corner.y + rect.height/2
return p print_point(center)
• Here is an example that passes box as an
argument and assigns the resulting Point to
center:
center = find_center(box)
print_point(center)
Objects are mutable
• The state of an object can be changed by
making an assignment to one of its
attributes.
• For example, to change the size of a rectangle box.width = box.width + 50
without changing its position, the values of box. Height = box.height + 100
width and height can be modified
• Also functions can be written to modify def grow_rectangle(rect, dwidth, dheight):
objects. rect.width += dwidth
rect.height += dheight
• For example, grow rectangle takes a
Rectangle object and two numbers, dwidth
and dheight, and adds the numbers to the Print(box.width, box.height)
width and height of the rectangle: # (150.0, 300.0)
• Inside the function, rect is an alias for box, so grow_rectangle(box, 50, 100)
when the function modifies rect, box Print(box.width, box.height)
changes. # (200.0, 400.0)
Write a function named move_rectangle that takes a Rectangle and two numbers named dx and dy. It
should change the location of the rectangle by adding dx to the x coordinate of corner and adding dy to
the y coordinate of corner.
class Rectangle:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height Original Rectangle: 1 2 5 6
Moved Rectangle: 4 0 5 6
def move_rect(rectangle, dx, dy):
rectangle.x += dx
rectangle.y += dy
rect = Rectangle(1, 2, 5, 6)
print("Original Rectangle:", rect.x, rect.y, rect.width, rect.height)
move_rect(rect, 3, -2)
print("Moved Rectangle:", rect.x, rect.y, rect.width, rect.height)
Copying
• Aliasing can make a program difficult to
read because changes in one place
might have unexpected effects in
another place.
• It is hard to keep track of all the
variables that might refer to a given
object.
• Copying an object is often an alternative
to aliasing.
• The copy module contains a function
called copy() that can duplicate any
object: copy.copy()
• p1 and p2 contain the same data, but
they are not the same Point.
• The is operator indicates that p1 and p2 are not the same object
• The default behavior of the == operator is the same as the is
operator
• It checks object identity, not object equivalence.
• Python doesn’t know what should be considered equivalent for
programmer-defined types
• This operation is called a shallow copy because it copies the object
and any references it contains, but not the embedded objects
• deepcopy() copies not only the object but also the objects it refers
to, and the objects they refer to, and so on.
6. Develop a program to sort the contents of a text file and write the sorted
contents into a separate text file. [Hint: Use string methods strip(), len(), list
methods sort(), appeand(), and file methods open(), readlines(), and write()].
import os
infile=open('C:\\Users\\Smitha\\Desktop\\new.txt')
words=[ ]
for l in infile:
temp=l.split()
for i in temp:
words.append(i)
infile.close()
print(words)
words.sort()
print(words)
outfile=open('res.txt','w')
for i in words:
outfile.write(i)
outfile.write(' ')
outfile.close()