13.1Object-Oriented Programming: Classes and Objects
Covers IGNOU MCS-201 §13.0, 13.2
In C you used a structure to group data (Unit 6) and separate functions to work on it. A class goes one step further: it bundles the data and the functions that operate on it into one unit. Objects created from the class carry their own data and know how to work with it.
The object-oriented idea
Four ideas appear throughout OOP:
Idea
Meaning
In Python
Encapsulation
Keep data and the methods that use it together; let users work through methods without knowing the details
A class with attributes and methods
Inheritance
Build a new class from an existing one, reusing its code
class Child(Parent):
Polymorphism
One interface, many forms: different objects respond to the same call in their own way
Overriding, operator overloading, duck typing
Abstraction
Show only what a user needs; hide the rest
Public methods; names starting with _ mark internal details
Classes and objects
Defining a class
Syntax
class ClassName: """Optional docstring.""" class_variable = value # shared by all objects def method_name(self, parameters): statements
class is the keyword; by convention class names use CapitalisedWords.
The header ends with a colon and the body is indented, exactly like def and if.
A method is a function defined inside a class. Its first parameter, conventionally named self, receives the object the method is called on.
Calling a class like a function, Rectangle(), creates a new object.
obj.method(args) calls a method; Python turns it into ClassName.method(obj, args), which is how self gets its value.
Namespaces
A class has its own namespace holding its methods and class variables, and each object has a separate namespace holding its own attribute values. This is why two rectangles can have different heights while sharing one copy of the methods.
In Python, everything is an object: 28, "IGNOU" and [1, 2] are objects of the classes int, str and list. Every object has an identity (id()), a type (type()) and a value.
Key takeaways
OOP groups data and behaviour into objects created from classes.
class Name: defines a class; methods are functions in it with self first.
Calling the class (Name()) creates an object; obj.method() calls a method on it.
Each object has its own namespace of attribute values.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a class Circle with methods set_radius(r), perimeter() and area() (Check Your Progress 1, Q1), and show how to use it for radius 2.
Show worked answer
import math, then class Circle: with def set_radius(self, r): self.r = r, def perimeter(self): return 2 * math.pi * self.r and def area(self): return math.pi * self.r ** 2. Use: c = Circle(), c.set_radius(2), print(round(c.perimeter(), 2), round(c.area(), 2)), which prints 12.57 12.57. Every method reads the same attribute self.r set by set_radius.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/classes-in-python/object-oriented-programming-classes-and-objects/ to read this page.