What are classes and objects in Python

What are classes and objects in Python?

Python is an object-oriented programming language. Unlike procedure-oriented programming, where the main emphasis is on functions, object-oriented programming stress on objects.
The object is simply a collection of data (variables) and methods (functions) that act on those data. And, class is a blueprint for the object.
We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
As many houses can be made from a description, we can create many objects from a class. 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

Like function definitions begin with the keyword def. 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 not mandatory, this is recommended.
Here is a simple class definition.
  1. class MyNewClass:
  2. '''This is a docstring. I have created a new class'''
  3. pass
A class creates a new local namespace where all its attributes are defined. Attributes may be data or functions.
There are also special attributes in it that begin with double underscores (__). For example, __doc__ gives us the docstring of that class.
As soon as we define a class, a new class object is created with the same name. This class object allows us to access the different attributes as well as to instantiate new objects of that 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 functions call.
  1. >>> 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.

Constructors in Python

Class functions that begins with double underscore (__) are called special functions as they have special meaning.

Deleting Attributes and Objects

Any attribute of an object can be deleted anytime, using the del statement. Try the following on the Python shell to see the output.
  1. >>> c1 = ComplexNumber(2,3)
  2. >>> del c1.imag
  3. >>> c1.getData()
  4. Traceback (most recent call last):
  5. ...
  6. AttributeError: 'ComplexNumber' object has no attribute 'imag'
  7. >>> del ComplexNumber.getData
  8. >>> c1.getData()
  9. Traceback (most recent call last):
  10. ...
  11. AttributeError: 'ComplexNumber' object has no attribute 'getData'
We can even delete the object itself, using the del statement.
  1. >>> c1 = ComplexNumber(1,3)
  2. >>> del c1
  3. >>> c1
  4. Traceback (most recent call last):
  5. ...
  6. 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.
Deleting Object in Python

Post a Comment