Classes and Objects in Python
Classes and Objects are the two main components of object-oriented programming. A class is a basic block in Python. A class creates a new type and the object is an instance of the class. Classes provide a blueprint or a template using which objects are created. In Python, everything is an object or an instance of some class.
Defining classes:
class class-name: .......... ..........
Creating Objects:
Once a class is defined, the next job is to create an object of that class. The object can then access class variables and class methods using the dot operator (.). It has the following syntax:
object-name = class-name()
Class Methods and Self Argument:
Class Methods are the same as ordinary functions that we have been defining so far with just one small difference. Class methods must have the first argument named self. This is the first argument that is added to the beginning of the parameter list.
Moreover, you don’t pass a value for this parameter when you call the method. Python provides its value automatically. The self-argument refers to the object itself. Since the class methods use self, they require an object or instance of the class to be used. That is called instance methods.
__init__() Method:
The __init__() Method has a special significance in Python classes. The __init__() Methods is automatically executed when an object of a class is created. The method is very useful to initialize the variable of the class object.
Note: __init__() Method is prefixed as well as sufficed by double underscores. The __init__() Method can be declared as [python]def __init__(self, [args…])[/python].