Classes in Python

Class is a blueprint for the object. An object is also called an instance of a class and the process of creating this object is called instantiation.

Defining a Class in Python 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 it is not mandatory but good practice to follow.

Here is a simple class definition.

class NewClass: '''This is a docstring created a new class''' pass

Another Example of class

>>> class MyClass: ... "This is an another class" ... x = 10 ... def func(self): ... print('Hi') ... >>> MyClass.a 10 >>> MyClass.func >>> MyClass.__doc__ 'This is an another class'
Creating an Object in Python

We saw that the class object could be used to access different attributes. It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call.

>>> ob = MyClass()
This will create a new instance object named ob. We can access attributes of objects using the object name prefix. Attributes may be data or method. Method of an object are corresponding functions of that class. Any function object that is a class attribute defines a method for objects of that class. This means to say, since MyClass.func is a function object (attribute of class), ob.func will be a method object.
>>> ob = MyClass() >>> MyClass.func >>> ob.func > >>> ob.func() Hello

You may have notices the self parameter in function definition inside the class. But we called the method simply as ob.func() without any arguments. It still worked. This is because, whenever an object calls its method, the object itself is pass as the first argument. So, ob.func() translates into MyClass.func(ob). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method's object before the first argument. For these reasons, the first argument of the function in class must be the object itself. This is conventionally called self. It can be named otherwise but we highly recommend to follow the convention. Now you must be familiar with class object, instance object, function object, method object and their differences.

Constructors in Python

Class functions that begins with double underscore (__) are called special functions as they have special meaning. Of one particular interest is the __init__() function. This special function gets called whenever a new object of that class is instantiated. This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize all the variables.
class ComplexNumber: def __init__(self,r = 0,i = 0): self.real = r self.imag = i def getData(self): print("{0}+{1}j".format(self.real,self.imag))
In the above example, we define a new class to represent complex numbers. It has two functions, __init__() to initialize the variables (defaults to zero) and getData() to display the number properly. Here, are some sample runs.
>>> c1 = ComplexNumber(2,3) >>> c1.getData() 2+3j >>> c2 = ComplexNumber(5) >>> c2.attr = 10 >>> (c2.real, c2.imag, c2.attr) (5, 0, 10) >>> c1.attr
Traceback (most recent call last): ... AttributeError: 'ComplexNumber' object has no attribute 'attr' An interesting thing to note in the above step is that attributes of an object can be created on the fly. We created a new attribute attr for object c2 and we read it as well. But this did not create that attribute for object c1. Deleting Attributes and Objects Any attribute of an object can be deleted anytime, using the del statement.
>>> c1 = ComplexNumber(2,3) >>> del c1.imag >>> c1.getData() Traceback (most recent call last): ... AttributeError: 'ComplexNumber' object has no attribute 'imag' >>> del ComplexNumber.getData >>> c1.getData() Traceback (most recent call last): ... AttributeError: 'ComplexNumber' object has no attribute 'getData'
We can even delete the object itself, using the del statement.
>>> c1 = ComplexNumber(1,3) >>> del c1 >>> c1
Traceback (most recent call last): ... NameError: name 'c1' is not defined Actually, it is more complicated than that. When we do c1 = ComplexNumber(1,3), a new instance object is created in memory and the name c1 binds with it. On the command del c1, this binding is removed and the name c1 is deleted from the corresponding namespace. The object however continues to exist in memory and if no other name is bound to it, it is later automatically destroyed. This automatic destruction of unreferenced objects in Python is also called garbage collection.