Call a Class in Python

Abdul Jabbar Oct 15, 2020
Call a Class in Python

Python is an object-oriented language that is easy to work with for any type of task to complete. It has very easy-to-use features; hence creating classes and objects is one of its features, and it’s absolutely easy in it. This article will introduce how to create the class instance and use it with the object to call classes using Python’s object-oriented programming.

An instance is an example of a single occurrence of something. Also, the object is called an instance of a class which means that any class object is unique to that class. Also, the class contains data members and methods, which saves the details related to that particular class. Furthermore, the class can be accessed by an object of that class using the dot notation.

Class is a user-defined pattern for an object that explicates a set of attributes identifying any class object. The attributes in the class are called the data members, which are also called the class variables and instance variables of the class. The methods of the class are accessed via dot notation from the main function. Furthermore, an instance is an individual and separate object of a particular class. An object that belongs to a class Vegetable, for example, is an instance of the class Vegetable only. Instance variables are recognized by only the created objects of the class. From this definition, we conclude that each class object has different access to the class variables and methods.

For example, we can consider vegetables as a class, and cucumber, carrot, potato, etc. are the objects or instances of the class vegetables. Because the carrot is unique for its taste, and the same is applicable for other vegetables also. In other words, we cannot call a carrot from the potato and vice versa.

The syntax for creating the instance of the class is following.

Object_name = class_name()

To work with the objects of a class, we have to call the class using the class name and then pass in whatever arguments its constructor method accepts.

"The below code would create first object of the Vegentables"
veg1 = vegetables("carrot") 
"The below code would create second object of the Vegentables"
veg1 = vegetables("cucumber")

Call an Instance of a Class in Python

Class methods can be called from all the instances and from the class itself too. These instances will use the same methods as the class. Let’s go over the code below that contains a method for the class vegetables.

First, We will create an object of a vegetable class and call the method. Here we are creating a class named vegetables. We allocated it the attributes name equals carrot and color equals red. Then we create a method called get_color(). In this class method, we will pass the parameter as self into the method. It will return the red color for this carrot class.

Then we create a method called get_name(). In this class method, we pass the parameter as self into the method. It will return the red color for this carrot class. Then finally, we will create the instance of a vegetable class and call the get color and get name class methods using the instance vegetable1 of the vegetables class.

class vegetables:
    name= "carrot"
    color= "red"

    def get_color(self):
        return self.color
    def get_name(self):

vegetable1= vegetables()


Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.


Related Article - Python Class