What is an object in Python

Python is an object-oriented programming language. Everything is in Python treated as an object, including variable, function, list, tuple, dictionary, set, etc. Every object belongs to its class. For example - An integer variable belongs to integer class. An object is a real-life entity. An object is the collection of various data and functions that operate on those data. An object contains the following properties.

  • State - The attributes of an object represents its state. It also reflects the properties of an object.
  • Behavior - The method of an object represents its behavior.
  • Identity - Each object must be uniquely identified and allow interacting with the other objects.

Let's understand the object in the aspect of classes.

The classes and objects are the essential key to the object-oriented programming. Classes are the blueprint of the object. Classes are used to bundling the data and functionality together. Each newly created class must have its object. Let's understand real-life example of class and object.

A human is a class which may have may attributes such as walking, sleeping, thinking, etc. Suppose we want to name and age of 100 humans, so we don't need to create a class for every person. We just need to instantiate the multiple objects of that perticular class.

The class contains the user-defined data structure that holds the own data members such as variables, constructs, and member functions, which can be accessed by creating an object of the class.

The syntax of creating a class is given below. The syntax of creating a class is given below.

Syntax:

The class keyword is used to define the class and the user-define class name replaces ClassName.

Creating an Object of class

The object is essential to work with the class attributes. Instantiate is a term used when we create the object of any class, and the instance is also referred to as an object. The object is created using the class name. The syntax is given below.

Syntax:

In the following example, we have created the object of Person class.

Example -

Output:

Age: 24 
Name: John

Explanation:

In the above code, we have created a Person class which consisted of two attributes age, name and display function. We created the object of person class called per . Using the object along with the .dot operator, we accessed the class function.






Latest Courses