MODULE IV
4.1 CLASSES AND OBJECTS
Programmer defined types, Attributes, Rectangles, Copying, Debugging
4.2 CLASSES AND FUNCTIONS
Time, Pure Functions, Modifiers, Prototyping vs Planning, Debugging
4.3 CLASSES AND METHODS
Object Oriented Features, The init Method and str Method, Operator Overloading, Type-based dispatch,
Polymorphism, Interface and Implementation, Debugging
Mamatha A, Asst Prof, Dept of CSE, SVIT Page 1
,Python Application Programming (15CS664) Module IV
MODULE IV
4.1 CLASSES AND OBJECTS
Python is an object-oriented programming language, and class is a basis for any object oriented
programming language.
Class is a user-defined data type which binds data and functions together into single entity.
Class is just a prototype (or a logical entity/blue print) which will not consume any memory.
An object is an instance of a class and it has physical existence.
One can create any number of objects for a class.
A class can have a set of variables (also known as attributes, member variables) and member
functions (also known as methods).
Programmer-defined Types
A class in Python can be created using a keyword class.
Here, we are creating an empty class without any members by just using the keyword passwithin it.
class Point:
pass
print(Point)
The output would be –
<class ' main .Point'>
The term main indicates that the class Point is in the main scope of the current module.
In other words, this class is at the top level while executing the program.
Now, a user-defined data type Point got created, and this can be used to create any number of
objects of this class.
Observe the following statements:
p=Point()
Now, a reference (for easy understanding, treat reference as a pointer) to Point object is created
and is returned. This returned reference is assigned to the object p.
The process of creating a new object is called as instantiation and the object is instance of a
class.
When we print an object, Python tells which class it belongs to and where it is stored in the
memory.
print(p)
The output would be –
< main .Point object at 0x003C1BF0>
Mamatha A, Asst Prof, Dept of CSE, SVIT Page 2
, Python Application Programming (15CS664) Module IV
The output displays the address (in hexadecimal format) of the object in the memory.
It is now clear that, the object occupies the physical space, whereas the class does not.
Attributes
An object can contain named elements known as attributes.
One can assign values to these attributes using dot operator.
For example, keeping coordinate points in mind, we can assign two attributes x and y for the
object of a class Point as below
p.x =10.0
p.y =20.0
A state diagram that shows an object and its attributes is called as object diagram.
For the object p, the object diagram is shown in Figure below.
Point
x 10.0
p 20.0
y
Figure : Object Diagram
The diagram indicates that a variable (i.e. object) p refers to a Point object, which contains two
attributes.
Each attributes refers to a floating point number.
One can access attributes of an object as shown –
>>> print(p.x)
10.0
>>> print(p.y)
20.0
Here, p.x means “Go to the object p refers to and get the value of x”.
Attributes of an object can be assigned to other variables
>>> x= p.x
>>> print(x)
10.0
Here, the variable x is nothing to do with attribute x.
There will not be any name conflict between normal program variable and attributes of an object.
A complete program: Write a class Point representing a point on coordinate system. Implement
following functions –
A function read_point() to receive x and y attributes of a Point object as user input.
Mamatha A, Asst Prof, Dept of CSE, SVIT Page 2