The starting point for the creation of objects is a class, which is a user-defined template or prototype. Data and functionality can be bundled with the help of classes. New object types can be created by adding a new class, enabling the creation of new instances of such types. To retain the state of each class instance, attributes can be linked to it.
Additionally, class instances may include methods for changing their state that are defined by their class. A class is instantiated when a clone of it is created, inheriting all variables and functions of the class. Python makes it easy to instantiate classes. Simply calling the class with the arguments specified by the __init__ method creates an instance of the class. The freshly formed object is what will be returned.
Example 1: Program to instantiate a python class:
Programmers use the instantiation process to produce real instances, particular realities of abstraction, or patterns, like an object class or a system process. You can make an instance of an object by instantiating it, for instance, by defining a particular form of the object inside a class, giving it a name, and placing it at a certain physical location. Let’s declare an object of the class in the example below which is also referred to as instantiating a class.
val1 = "Rose"
val2 = "Jasmine"
print("Red is", self.val1)
print("White is", self.val2)
bloom = Flowers()
We have defined a class with the name ‘Flowers’. Inside that class, we have created two attributes of this class. The first attribute is given the name val1 and initialized with the string. The second attribute is given the name val2. It is also initialized with the string value. Then, we defined a sample method in the class with the ‘def’ keyword and named that method ‘f’. The method takes one argument. Self serves as a representation of the class instance.
In Python, we can access the class’s attributes and methods by using “self.” It ties the attributes to the supplied parameters. Within the sample sampling, we have printed the value of the class attributes. After that, we have defined an instance ‘bloom’ of the class Flower. With this class instance ‘bloom’, we have accessed the class attributes and the function.
At the time of the compilation, the following outcome is obtained.
Example 2: Program of using a constructor to define the instance variable:
Class variables contain properties and methods that are common to all instances of the class, while instance variables contain data that is specific to each instance. Class variables are assigned a value in the class. Whereas, instance variables have their value assigned within a function constructor or method with self. The below program demonstrates that instance variables are those found inside methods and constructors, whereas class variables are those with values specified in the class declaration.
items = 'food items'
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
p1 = Product("milk","50", "1kg")
p2 = Product("flour","70", "1gram")
print('Product 1 details:')
print('Product name:', p1.name)
print('Product price: ', p1.price)
print('Product quantity: ', p1.quantity)
print('\nProduct 2 details:')
print('Product name:', p2.name)
print('Product price: ', p2.price)
print('Product quantity: ', p2.quantity)
print("\nclass name accessing class variable")
Here, we have defined a class as ‘products’ and we have created the variable of the class as items. The value of the string is used to initialize the class variable. Then, we have defined the init method, which is called the constructor, and take three arguments. The Java and C++ constructors are analogous to the __init__ method. Initializing the state of an object is done by constructors.
Like methods, a function constructor also includes a group of statements (also known as instructions) that are carried out during the formation of an object. It begins to run as immediately as a class object is created. The method can be used to initialize an object in any way you choose. We have created three instance variables name, price, and quantity inside that init method and assigned them string values.
After that, we have defined the two objects p1 and p2 of the class ‘product’. Then, we printed the values of the food items with the print statement. In the end, we have also accessed the variable of the class by invoking the class name.
The following is the output obtained from the above-implemented code.
Example 3: Program to define the instance variable inside the class:
The following example is utilizing the conventional method to define an instance variable.
name = 'Person'
def __init__(self, age ):
self.age = age
def setGender(self, gender):
self.gender = gender
p1 = Person("Adult")
Firstly, we have defined the class as a Person. Inside that class, we have created the class variable as a name and assigned it a value. Then, we have defined the init method which generates the constructor and takes two inputs self and the age. The age variable parameter is then initialized. After that, we have defined the method to add the instance variable gender and in another defined method, we are getting the instance variable. With the objects p1 and p2, we have assigned the values to the class variable and the instance variable.
The instance variable value is displayed on the shell as shown in the below screenshot.
Now, you are familiar with instantiating a class in python as we have covered three different examples with different implementations. We saw that several characteristics might be accessed by using the class object. Additionally, it can be used to instantiate new objects belonging to that class. The process of creating an object is comparable to calling a function.