Signup/Sign In

How to create Objects in Python

In this article, we will learn to create an object in Python. We will look at the methodology, syntax, keywords, associated terms with some simple approaches, and some custom codes as well to better understand this topic. Let's first have a quick look over what is an object, and how it is used and defined in Python language.

What is an Object?

An object is the run time entity used to provide the functionality to the Python class. The attributes or properties defined inside the class are accessed only by using objects of that class. Also, the user-defined functions are accessed by using the object. A class constructor is automatically called when an object of the class is created. So, as soon as we define or create a class with attributes and methods, a new class object is created with the same name as the class. This class object permits us to access the different attributes as well as to instantiate new objects of that class.

An object consists of :

  • State - Attributes or Properties of an object.

  • Behavior - Methods of an object.

  • Identity - Unique Name to an object and communication between two or more object

Let us understand how objects are created with the help of an example.

Creating an Object of a Class in Python

The object is created after creating a class. Instant of the object is created using the name same as the class name and it is known as Object Instantiation. One can give any name to a newly created object. Object creation is similar to calling a function. This is because as soon as the object is created or instantiated with the class name, the default constructor of the class is called automatically.

Syntax:

Here is the syntax,

#object instantiation
object_name = class_name()

Let us take an example of a 'Dog' class to understand the creation of an object and how attributes and methods are accessible from the new object.

Example: Creating an object in Python

We take a class and name it "Dog". We define two attributes or two instances of the Dog class which shows the properties of Dog. The attributes are color and name. This is the simplest template of a class. Further, we define a constructor that uses __init__ for its declaration. It takes three parameters as shown below. Self is the default parameter and the other two arguments are attributes of the class. After this, the user can create its own function called member functions or user-defined function of the class and perform different operations on the attributes defined inside the class. After we create the class, we come out of the scope of the class and create a new object that calls the class constructor. Using object and dot(.) operator, the function is called. The function func() prints two statements after it is called.

#class is defined using class keyword
class Dog:
  
  #data members of class
  color = "black"  #attribute 1
  name = "Polo"    #attribute 2
   
  #class default constructor
  def __init__(self,name,color): 
          self.name = name
          self.color = color
  
  #user defined function of class
  def func(self):
          print("After calling func() method..")
          print("My dog's name is", self.name)
          print("His color is", self.color)


#object 1 is created and default constructor is called 
obj1 = Dog('Robert', 'white')

#user-defined function is called from object 1 
obj1.func()

#access the attribute
print("\nDirect access of attributes using object..")
print(obj1.name)


After calling func() method..
My dog's name is Robert
His color is white
Direct access of attributes using object..
Robert

Keywords used in the above example

class - A user-defined blueprint for an object that combines a set of attributes that describes the properties of that object. We used the 'Dog' class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.

Data members - It is a class variable or instance variable that holds properties associated with a class and its objects. Here, the data members are color and name.

self - This is a default parameter in every method in a class, even if we have no arguments to pass. This parameter has no value. Python provides the value to self when the method is called. In this example, when we call the method func() from object obj1 as obj1.func(), this is automatically converted into Dog.func(obj1) by Python.

__init__ - The __init__ represents constructor in python. this is used to initialize the object’s state. a constructor also contains some scripts that are executed at the time of Object creation. It is called as soon as an object of a class is instantiated.

Method - It is a user-defined function that can be defined inside or outside a class definition. It also contains a set of statements to execute. Here, func() is an example of a method.

Now, the user can create as many objects as he wants and can access data members, methods, and class variables from it. A user can define one or more classes and perform different actions using objects. We will learn more in detail in further articles.

Conclusion

In this article, we learned to create and instantiate an object in Python after creating a class. We used the Dog class example to better understand the topic. We learned about what actions can be performed by using objects.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.