| by Arround The Web | No comments

How to Instantiate a Class in Python

Instantiating a class is a very basic principle of using the Object-Oriented Programming (OOP) approach, and people often confuse this term with something complex. In Python, instantiating a class or creating its instance is done by creating a variable and setting it equal to the class name with parenthesis, just like you would call a function.

This post will act as a guide and explain what is meant by instantiating and the process of instantiating a class in detail.

What Instantiating a Class in Python?

Instantiating a class simply means creating a duplicate/copy of a Python Class, which will contain all the original class’s variables, functions, and attributes. When referring to instantiating from the point of OOP, then it is the process of creating object variables of the class.

Note: Making objects/copies of a class is also called creating instances of a class, thus the term “instantiating” a Class

How to Instantiate a Class in Python?

As mentioned above, to instantiate a class, you need to call the name of the class like you would call a normal function and store the result in a variable. Again, in OOP terms, it is called calling the constructor. However, it causes confusion among people as you do not specify the constructor method with the same name in Python.

Anyhow, the syntax of instantiating a class is as follows:

objVar = ClassName( initializeVariableArguments )

 

In this syntax:

  • objVar is the variable in which the copy of the class is stored, or it is an object of the class
  • ClassName() is the name of the class to be instantiated into a variable
  • initializeVariableArguments are the arguments that will be used to initialize the values of variables of the class (optional)

Example 1: Instantiate a Basic Class in Python

To demonstrate the method of instantiating a class, you need to first have a class. Therefore, take the following code snippet that will create a class for persons with the names of two people stored in variables and a function to print the name of person1:

class Person:
  p1Name = "John Doe"
  p2Name = "Rudy Taylor"
 
  def getNameP1(self):
    return self.p1Name;

 

After that, create a variable named “pObj1” and set it equal to the “Person” class name with round brackets to make a copy of the class inside pObj1:

pObj1 = Person()

 

After that, you can use the pObj1 variable with the dot-operator to access the variables and function of the Person class:

print("Directly Access the Variable: ",pObj1.p2Name)
print("Calling the function of Class: ", pObj1.getNameP1())

 

The code snippet for this example is as follows:

class Person:
  p1Name = "John Doe"
  p2Name = "Rudy Taylor"
 
  def getNameP1(self):
    return self.p1Name;

pObj1 = Person()

print("Directly Access the Variable: ",pObj1.p2Name)
print("Calling the function of Class: ", pObj1.getNameP1())

 

When you execute this code, it will produce the following result on the terminal:

From this output, you can clearly conclude that you have successfully instantiated the Person class.

Example 2: Instantiating a Class in Python by Passing Values

If a class has un-initialized variables, then you will have to pass them values when creating instances of that class. To demonstrate this, take the following class code:

class Person:
 
  def __init__(self,p1Name,p2Name):
    self.p1Name = p1Name
    self.p2Name = p2Name
   
  def getNameP1(self):
    return self.p1Name;

 

In this class, the variables p1Name, and p2Name are un-initialized, and you need to pass the value while creating the instance as follows:

pObj1 = Person("Alex","Monroe")

 

After that is done, you can use the pObj1 to access the variables and function just like in the first example:

print("Directly Access the Variable: ",pObj1.p2Name)
print("Calling the function of Class: ", pObj1.getNameP1())

 

The complete code snippet for this example is as:

class Person:
 
  def __init__(self,p1Name,p2Name):
    self.p1Name = p1Name
    self.p2Name = p2Name
   
  def getNameP1(self):
    return self.p1Name;

pObj1 = Person("Alex","Monroe")

print("Directly Access the Variable: ",pObj1.p2Name)
print("Calling the function of Class: ", pObj1.getNameP1())

 

When you execute this program, it will create the following output on the terminal:

You have successfully instantiated a class with un-initialized variables successfully.

Conclusion

Instantiating a Class is the process of creating instances of that class that contain all the variables, functions, and other attributes, which can be called a copy of that class. To instantiate a class, you need to call its constructor method, and in Python, the constructor method is the name of the class followed by round brackets, just like calling a function. Once a class has been instantiated, you can access the attributes of the copied class by using a dot operator.

Share Button

Source: linuxhint.com

Leave a Reply