Python Classes and Objects

We know that a class is a model or plan to create objects. This means, we write a class with the attributes and actions of objects. Attributes are represented by variables and actions are performed by methods. So, a class contains variable and methods. The same variables and methods are also available in the objects because they are created from the class. These variables are also called 'instance variables' because they are created inside the instance (i.e. object).


Please remember the difference between a function and a method. A function written inside a class is called a method. Generally, a method is called using one of the following two ways:


  • classname.methodname()
  • instancename.methodname()

The general format of a class is given as follows:

class Classname(object):
attributes
def __init__(self):
def method1():
def method2():