Methods in Python
MUKESH KUMAR
AGENDA
✓Instance Methods
✓Class Methods
✓Static Methods
✓Accessor & Mutator Methods
✓Summary
Python Methods
Types of Methods
Instance Class Static
Methods Methods Methods
What is an Instance Method?
•Definition:
•A method that operates on an instance of a class and can modify
object attributes.
•Requires self as the first parameter.
•Example:
Key Points about Instance Methods
• Instance methods operate on the instance variables
(attributes) of a class.
• They are bound to the instance of the class.
• The first parameter of an instance method is conventionally
named self, which refers to the instance calling the method.
Through self, instance methods can access and modify the
attributes of that instance.
• Instance methods are the most commonly used type of
method.
What is a Class Method?
•Definition:
•A method that operates on the class rather than on instance
objects.
•Uses @classmethod decorator and takes a cls as the first
parameter.
•Example:
Key Points about Class Methods
• Class methods operate on class variables (static variables).
• They are bound to the class itself.
• They are defined using the @classmethod decorator.
• The first parameter is conventionally named cls, which
refers to the class itself.
• Class methods can access and modify class-level data but
not instance-level data.
• They are often used to create alternative constructors or
modify class-level state.
What is a Static Method?
•Definition:
•A method that does not modify the class or instance attributes.
•Uses @staticmethod decorator and does not require self or
cls.
•Example:
Key points about Static Methods
• Static methods are general utility methods that do not have
access to instance or class-level data.
• They are defined using the @staticmethod decorator.
• They do not take a self or cls parameter.
• Static methods are called using the class name or an object
reference.