[go: up one dir, main page]

0% found this document useful (0 votes)
6 views9 pages

2.methods & Attributes

The document outlines the different types of methods in Python, including instance methods, class methods, and static methods. Instance methods operate on class instances and require 'self' as the first parameter, while class methods operate on the class itself and use 'cls'. Static methods do not modify class or instance attributes and do not require any specific parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

2.methods & Attributes

The document outlines the different types of methods in Python, including instance methods, class methods, and static methods. Instance methods operate on class instances and require 'self' as the first parameter, while class methods operate on the class itself and use 'cls'. Static methods do not modify class or instance attributes and do not require any specific parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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.

You might also like