Types of Methods in Python Classes

 Types of Methods in Python Classes :

  • Instance Method
  • Class Method
  • Static Method


1. Instance Method

Definition:
Instance methods use the self keyword and work with instance variables.

Purpose:
To modify or access the object’s attributes.

Program:


Output :


2. Class Method

Definition:

Class methods use the @classmethod decorator and the cls keyword.

Purpose:

To access or modify the class variables.


Program:


Output:



3. Static Method

Definition:

Static methods use the @staticmethod decorator. They do not use self or cls.

Purpose:

To perform utility tasks that do not access instance or class variables.


Program:



Output :


Post a Comment

0 Comments