9.Classes and Objects


9.1 Classes and objects: Programmer-defined types, Attributes, Rectangles, Instances as return
values, Objects are mutable, Copying,

9.2 Classes and functions: Time, Pure functions, Modifiers, Prototyping versus planning,

9.3 Classes and methods: Object-oriented features, Printing objects, Another example, A more
complicated example,The __init__ method, The str method, Operator overloading, Type-based
dispatch, Polymorphism, Interface and implementation,

9.1 Classes and objects

Python is an object oriented programming language. Class is a abstract data type which can be defined as a template or blueprint that describes the behavior / state that the object of its type support.

An object is an instance of class . Objects have states and behaviors.

Example: A dog has states – color, name, breed as well as behaviors – wagging the tail, barking, eating.

Object is simply a collection of data (variables) and methods (functions) that act on those data. The process of creating this object is called instantiation

Defining a Class in Python :

We define a class using the keyword class. The first string is called docstring and has a brief description about the class. Although not mandatory, this is recommended. A class  attributes are data members( variables)  and  functions.

Syntax :

The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows −

class   ClassName:

          “””Optional class documentation string’ class_suite”””


•The class has a documentation string, which can be accessed via ClassName.__doc__.

•The class_suite consists of all the component statements defining class members, data attributes and method.

Example 1