Classes and Object-Oriented Programming in python

Python is an object-oriented programming language, which means that it manipulates and works with data structures called objects. Objects can be anything that could be named in Python – integers, functions, floats, strings, classes, methods, etc. These objects have equal status in Python. They can be used anywhere an object is required. You can assign them to variables, lists, or dictionaries. They can also be passed as arguments. Every Python object is a class. A class is simply a way of organizing, managing, and creating objects with the same attributes and methods.

In Python, you can define your own classes, inherit from your own defined classes or built-in classes, and instantiate the defined classes.

Class Syntax

To define a class, you can use ‘class’, a reserved keyword, followed by the classname and a colon. By convention, all classes start in uppercase. For example:

class Students:


To create a class that takes an object:

class Students(object)

The __init__() method

Immediately after creating an instance of the class, you have to call the __init__() function. This function initializes the objects it creates. It takes at least the argument ‘self’, a Python convention, which gives identity to the object being created.


class Students:

def __init__(self) :

class Employees(object):

def __init__(self, name, rate, hours) :

A function used in a class is called a method. Hence, the __init__() function is a method when it is used to initialize classes.

Instance Variables

When you add more arguments to the def_init_() besides the self, you’ll need to add instance variables so that any instance object of the class will be associated with the instance you create.

For example:

class Employees(object):

def __init__(self, name, rate, hours) :

name.self = name

rate.self = rate

hours.self =hours

In the above example, name.self, rate.self, and hours.self are the instance variables.

When you create instances of the class Employees, each member will have access to the variables which were initialized through the __init__ method. To illustrate, you can create or ‘instantiate’ new members of the class Employees:

staff = Employees(“Wayne”, 20, 8)

supervisor = Employees(“Dwight”, 35, 8)

manager = Employees(“Melinda”, 100, 8)

You can then use the print command to see how the instance variables interacted with the members of the class Employees:

print(, staff.rate, staff.hours)

print(, supervisor.rate, supervisor.hours)

print(, manager.rate, manager.hours)

The Python Shell will display this output:

Wayne 20 8

Dwight 35 8

Melinda 100 8