In this Python oops program, we will create a Python class with instance method. The program creates a class with a constructor and methods to display and update the name attribute of an object. It creates an object, displays its initial name, updates the name, and then displays the updated name.
Python class with instance method
Steps to solve the program
- The code demonstrates how to create a Python class with instance method and variables for updating and displaying the value of an attribute.
- The constructor method __init__() is used to initialize the name instance variable when an object is created.
The display_name() method allows us to print the value of the name instance variable. - The update_name() method provides a way to update the value of the name instance variable by passing a new name as a parameter.
- By combining these methods, we can create objects of the MyClass class, set and update the name attribute, and display the updated name whenever needed. In the example, the name is initially set to “Omkar” and then updated to “Ketan”.
Output:
Related Articles
Python oops program to create a class with class variables.
Python oops program to create a class with a static method.
Python oops program to create a class with the class method.
Write a Python Class to get the class name and module name.
Write a Python Class object under syntax if __name__ == ‘__main__’.
Python class with Single Inheritance.