Python Class and Objects

A class represents a blueprint for the object in any programming language. Python has efficient methods for handling classes and an instance of a class, also known as object provides easy access to the attributes and methods of a class. The mapping of a real-world object with the help of class gives programmer flexible way of solving any kinds of problem in Python.

How to Create a Class in Python?

To create a class in Python, the keyword class is used. The syntax for the creation of a class is a follows:-

class ClassName:

”’optional class documentation string”’
Class attributes
Class methods

The statements in the class consist of attributes that define the characteristics of the object and methods that operate on these attributes.

Let us consider an example of where we are trying to model the real world employee data which contains name and salary.

So, the above class creates an Employee class which contains two private variables name and salary. The method display is used to display the name and salary of the employee.

 

How to create an instance of the class?

In order to use the attributes and methods of a class, we need to instantiate the class. The syntax for instantiating the class is as follows:-

<object-name> = <class-name>(<arguments>)

Self in Python?

The first argument of every method in Python programming language is self. which is a reference to the current instance of the class. This is similar to the keyword “this” in Java or C++. While calling the method, we don’t need to provide self in an argument, it is automatically received as a first parameter.

What are Python In-built class functions?

Python provides following built-in functions to work with the class.

SNFunctionDescription
1getattr(obj,name,default)It is used to access the attribute of the object.
2setattr(obj, name,value)It is used to set a particular value to the specific attribute of an object.
3delattr(obj, name)It is used to delete a specific attribute.
4hasattr(obj, name)It returns true if the object contains some specific attribute.

Lets us use an example to illustrate the use of the built-in functions

The output of the above program is:-

John Doe
23
True
Traceback (most recent call last):
File “test.py”, line 26, in <module>
print(s.age)
AttributeError: ‘Student’ object has no attribute ‘age’

What are the built-in class attributes in Python?

Following attributes provides information about the class.

SNAttributeDescription
1__dict__It provides the dictionary containing the information about the class namespace.
2__doc__It contains a string which has the class documentation
3__name__It is used to access the class name.
4__module__It is used to access the module in which, this class is defined.
5__bases__It contains a tuple including all base classes.

Let us use an example below to have a more clear view,

The output of the above program is:-

None
{‘name’: ‘John Doe’, ‘id’: 1001, ‘age’: 21}
__main__
SHARE Python Class and Objects

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Share