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.

class Employee:
    name = "John"
    salary = 50000;

    def display(self):
        print(, self.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.

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

class Student:
    def __init__(self, name, id, age): = name = id
        self.age = age

s = Student("John Doe", 1001, 21)

# prints the attribute name of the object s
print(getattr(s, 'name'))

# reset the value of attribute age to 23
setattr(s, "age", 23)

# prints the modified value of age
print(getattr(s, 'age'))

# prints true if the student contains the attribute with name id

print(hasattr(s, 'id'))
# deletes the attribute age
delattr(s, 'age')

# this will give an error since the attribute age has been deleted

The output of the above program is:-

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

What are the built-in class attributes in Python?

Following attributes provides information about the class.

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,

class Student:
    def __init__(self,name, id, age): = name = id
        self.age = age

    def display(self):
        print("Name:%s, ID:%d, age:%d" % (,, self.age))

s = Student("John Doe", 1001, 21)

The output of the above program is:-

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

You may also like...

Leave a Reply

Your email address will not be published.