Python Class and Objects

We know that a python class is a model or plan to create objects of a python class. This means, we write a python class with the attributes and actions of objects.

Attributes are represented by variables and actions are performed by methods. So, a python class contains variables and methods. The same variables and methods are also available in the objects because they are created from the python 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 python class is given as follows:

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