Skip to content

Methods in Python

diagram what the dot actually does to a method mermaid
A function defined in a class body is just a function. Reaching it through an instance runs the descriptor protocol, which wraps it in a bound method that remembers the instance and supplies it as the first argument. That is the whole of what self is -- there is no magic, and reaching the function through the class gives you the unwrapped version.

Methods in Class in Python: A Comprehensive Guide

Section titled “Methods in Class in Python: A Comprehensive Guide”

In Python, methods are functions defined within a class. They encapsulate behavior that is specific to instances of the class. Understanding methods is essential for effective object-oriented programming. Let’s delve into the properties, advantages, and potential disadvantages of methods in Python classes.

  1. Encapsulation:

    • Description: Methods encapsulate behavior, grouping related functionality within a class.
    • Advantage: Encapsulation promotes code organization, making it easier to manage and maintain.
  2. Self Parameter:

    • Description: All methods in a class have self as their first parameter, representing the instance calling the method.
    • Advantage: Allows methods to access and manipulate the state of the instance, ensuring proper encapsulation.
  3. Access to Class Attributes:

    • Description: Methods have access to the class’s attributes and can modify them using the self parameter.
    • Advantage: Facilitates the manipulation of object state, contributing to the concept of encapsulation.
  4. Behavior Definition:

    • Description: Methods define the behavior of objects instantiated from the class.
    • Advantage: Enables the modeling of real-world actions, providing a blueprint for how instances should operate.
  5. Code Reusability:

    • Description: Methods can be reused across different instances of the same class.
    • Advantage: Promotes code reusability, reducing redundancy and improving maintainability.
  1. Modularity:

    • Description: Methods contribute to the modularity of code by organizing functionality into discrete units.
    • Advantage: Enhances code readability, maintenance, and the ability to make targeted updates.
  2. Object-Specific Behavior:

    • Description: Methods allow for the definition of behavior specific to each instance of a class.
    • Advantage: Enables objects to exhibit unique actions based on their state.
  3. Inheritance:

    • Description: Methods support the inheritance mechanism, allowing subclasses to override or extend methods from their superclass.
    • Advantage: Facilitates code reuse and the creation of specialized classes.
  4. Code Clarity:

    • Description: Methods contribute to a clearer organization of code by associating related actions with a specific class.
    • Advantage: Improves code maintainability and makes it easier for developers to understand the functionality of a class.
  5. Encapsulation of State:

    • Description: Methods work in conjunction with attributes to encapsulate the state of an object.
    • Advantage: Promotes data integrity and makes it easier to manage and control access to object state.

Potential Disadvantages of Methods in Python Classes:

Section titled “Potential Disadvantages of Methods in Python Classes:”
  1. Complexity:

    • Description: As the number of methods in a class increases, the complexity of the class may also rise.
    • Disadvantage: A highly complex class can be challenging to understand and maintain.
  2. Tight Coupling:

    • Description: Methods may introduce dependencies between different parts of a class, leading to tight coupling.
    • Disadvantage: High coupling can make the class less flexible and harder to modify without affecting other components.
  3. Overhead:

    • Description: Adding numerous methods to a class might introduce a slight runtime overhead.
    • Disadvantage: While typically negligible, excessive method calls could impact performance in some scenarios.
  4. Overuse of Accessors and Mutators:

    • Description: Excessive use of accessor and mutator methods (getters and setters) might violate principles of good design.
    • Disadvantage: It can lead to classes that are overly focused on exposing internal state, potentially undermining encapsulation.
  5. Learning Curve:

    • Description: For individuals new to object-oriented programming, understanding the concept of methods and their proper use can pose a learning curve.
    • Disadvantage: Novice programmers may initially find it challenging to grasp when and how to use methods effectively.
  • Instance Methods
  • Class Methods
  • Static Methods
  • Getter and Setter Methods

Instance methods are the most common type of methods in Python classes. They are defined within a class and are accessible only through an instance of the class. Instance methods have access to the instance’s state through the self parameter. They can also access the class’s attributes and other methods using the self parameter.

Syntax of Instance Methods in Python Classes:

Section titled “Syntax of Instance Methods in Python Classes:”
method.py
class ClassName:
    def method_name(self, parameters):
        # Method body

Example of Instance Methods in Python Classes:

Section titled “Example of Instance Methods in Python Classes:”
instance_method.py
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
    def display(self):
        print('Name:', self.name)
        print('Salary:', self.salary)
 
employee1 = Employee('John', 10000)
employee2 = Employee('Bob', 20000)
employee1.display()
employee2.display()

Output:

command
C:\Users\username>python instance_method.py
Name: John
Salary: 10000
Name: Bob
Salary: 20000

In the above example, we have created two instance variables named name and salary. We have initialized the name and salary variables to the name and salary parameters of the __init__() method. We have printed the name and salary variables using the employee1 and employee2 objects. The output shows that the name and salary variables are unique to the object.

Another Example of Instance Methods in Python Classes:

instance_method.py
class Student:
    def register(self, name, roll):
        self.name = name
        self.roll = roll
 
    def display(self):
        print('Name:', self.name)
        print('Roll:', self.roll)
 
student1 = Student()
student1.register('John', 1)
student1.display()

Output:

command
C:\Users\username>python instance_method.py
Name: John
Roll: 1

In the above example, we have created two instance variables named name and roll. We have initialized the name and roll variables to the name and roll parameters of the register() method. We have printed the name and roll variables using the student1 object. The output shows that the name and roll variables are unique to the object.

Class methods are methods that are bound to a class rather than its instances. They are defined using the @classmethod decorator and have access to the class’s state through the cls parameter. Class methods can be used to create factory methods, which are methods that return an instance of the class. They can also be used to modify a class’s state that applies across all instances of the class. Class methods are commonly used as alternative constructors.

Syntax of Class Methods in Python Classes:

Section titled “Syntax of Class Methods in Python Classes:”
Syntax
class ClassName:
    @classmethod
    def method_name(cls, parameters):
        # Method body

Example of Class Methods in Python Classes:

Section titled “Example of Class Methods in Python Classes:”
class_method.py
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
    def display(self):
        print('Name:', self.name)
        print('Salary:', self.salary)
    @classmethod
    def from_string(cls, emp_str):
        name, salary = emp_str.split('-')
        return cls(name, salary)
 
employee1 = Employee('John', 10000)
employee2 = Employee.from_string('Bob-20000')
employee1.display()
employee2.display()

Output:

command
C:\Users\username>python class_method.py
Name: John
Salary: 10000
Name: Bob
Salary: 20000

In the above example, we have created two instance variables named name and salary. We have initialized the name and salary variables to the name and salary parameters of the __init__() method. We have printed the name and salary variables using the employee1 and employee2 objects. The output shows that the name and salary variables are unique to the object. We have created a class method named from_string() using the @classmethod decorator. We have called the from_string() method using the Employee class and employee2 object. The output shows that the from_string() method is accessible through both the Employee class and employee2 object.

Another Example of Class Methods in Python Classes:

class_method.py
class IronMan:
    def suitUp(self):
        print('Suiting up...')
    def startEngine(self):
        print('Starting engine...')
    def workingJarvis(self):
        print('Working Jarvis...')
    def fly(self):
        print('Flying...')
    def land(self):
        print('Landing...')
    def suitDown(self):
        print('Suiting down...')
    def journey(self, location):
        print('Journey started to ' + location)
    @classmethod
    def goToMars(cls):
        cls().suitUp()
        cls().startEngine()
        cls().workingJarvis()
        cls().fly()
        cls().journey('Mars')
        cls().land()
        cls().suitDown()
 
IronMan.goToMars()
mark1 = IronMan()
mark1.goToMars()

Output:

command
C:\Users\username>python class_method.py
Suiting up...
Starting engine...
Working Jarvis...
Flying...
Journey started to Mars
Landing...
Suiting down...

In the above example, we have created seven instance methods named suitUp(), startEngine(), workingJarvis(), fly(), land(), suitDown(), and journey(). We have created a class method named goToMars() using the @classmethod decorator. We have called the goToMars() method using the IronMan class and mark1 object. The output shows that the goToMars() method is accessible through both the IronMan class and mark1 object.

Static methods are methods that are bound to a class rather than its instances. They are defined using the @staticmethod decorator and do not have access to the class’s state. Static methods are commonly used to create utility functions that do not require access to the class’s state. They can also be used to group related functionality within a class. Static methods are similar to class methods, but they do not receive the class as an implicit first argument.

Syntax of Static Methods in Python Classes:

Section titled “Syntax of Static Methods in Python Classes:”
Syntax
class ClassName:
    @staticmethod
    def method_name(parameters):
        # Method body

Example of Static Methods in Python Classes:

Section titled “Example of Static Methods in Python Classes:”
static_method.py
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
    def display(self):
        print('Name:', self.name)
        print('Salary:', self.salary)
    @staticmethod
    def is_valid_salary(salary):
        if salary > 0:
            return True
        else:
            return False
 
employee1 = Employee('John', 10000)
employee2 = Employee('Bob', 20000)
print(Employee.is_valid_salary(10000))
print(Employee.is_valid_salary(-10000))

Output:

command
C:\Users\username>python static_method.py
True
False

In the above example, we have created two instance variables named name and salary. We have initialized the name and salary variables to the name and salary parameters of the __init__() method. We have printed the name and salary variables using the employee1 and employee2 objects. The output shows that the name and salary variables are unique to the object. We have created a static method named is_valid_salary() using the @staticmethod decorator. We have called the is_valid_salary() method using the Employee class. The output shows that the is_valid_salary() method is accessible through the Employee class.

Another Example of Static Methods in Python Classes:

static_method.py
class Calculator:
    @staticmethod
    def add(a, b):
        return a + b
    @staticmethod
    def subtract(a, b):
        return a - b
    @staticmethod
    def multiply(a, b):
        return a * b
    @staticmethod
    def divide(a, b):
        return a / b
 
print(Calculator.add(10, 20))
print(Calculator.subtract(10, 20))
print(Calculator.multiply(10, 20))
print(Calculator.divide(10, 20))

Output:

command
C:\Users\username>python static_method.py
30
-10
200
0.5

In the above example, we have created four static methods named add(), subtract(), multiply(), and divide(). We have called the add(), subtract(), multiply(), and divide() methods using the Calculator class. The output shows that the add(), subtract(), multiply(), and divide() methods are accessible through the Calculator class.

Getter and setter methods are methods that are used to access and modify the state of an object. They are commonly used to control access to an object’s attributes. Getter methods are used to access an object’s attributes, while setter methods are used to modify an object’s attributes. Getter and setter methods are also known as accessor and mutator methods.

Syntax of Getter and Setter Methods in Python Classes:

Section titled “Syntax of Getter and Setter Methods in Python Classes:”
Syntax
class ClassName:
    def get_attribute(self):
        # Method body
    def set_attribute(self, value):
        # Method body

Example of Getter and Setter Methods in Python Classes:

Section titled “Example of Getter and Setter Methods in Python Classes:”
getter_setter_method.py
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
    def display(self):
        print('Name:', self.name)
        print('Salary:', self.salary)
    def get_name(self):
        return self.name
    def set_name(self, name):
        self.name = name
    def get_salary(self):
        return self.salary
    def set_salary(self, salary):
        self.salary = salary
 
employee1 = Employee('John', 10000)
employee1.display()
employee1.set_name('Bob')
employee1.set_salary(20000)
employee1.display()
print("Employee Name:", employee1.get_name())
print("Employee Salary:", employee1.get_salary())

Output:

command
C:\Users\username>python getter_setter_method.py
Name: John
Salary: 10000
Name: Bob
Salary: 20000
Employee Name: Bob
Employee Salary: 20000

In the above example, we have created two instance variables named name and salary. We have initialized the name and salary variables to the name and salary parameters of the __init__() method. We have printed the name and salary variables using the employee1 object. The output shows that the name and salary variables are unique to the object. We have created getter and setter methods for the name and salary variables. We have called the getter and setter methods using the employee1 object. The output shows that the getter and setter methods are accessible through the employee1 object. We have printed the name and salary variables using the getter methods. The output shows that the getter methods are accessible through the employee1 object.

Another Example of Getter and Setter Methods in Python Classes:

getter_setter_method.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.age = age
    def display(self):
        print('Name:', self.name)
        print('Age:', self.age)
    def get_name(self):
        return self.name
    def set_name(self, name):
        self.name = name
    def get_age(self):
        return self.age
    def set_age(self, age):
        if age > 18:
            self.age = age
        else:
            print('Age must be greater than 18')
 
student1 = Student('John', 20)
student1.display()
student1.set_name('Bob')
student1.set_age(15)
student1.set_age(30)
student1.display()
print("Student Name:", student1.get_name())
print("Student Age:", student1.get_age())

Output:

command
C:\Users\username>python getter_setter_method.py
Name: John
Age: 20
Age must be greater than 18
Name: Bob
Age: 30
Student Name: Bob
Student Age: 30

In the above example, we have created two instance variables named name and age. We have initialized the name and age variables to the name and age parameters of the __init__() method. We have printed the name and age variables using the student1 object. The output shows that the name and age variables are unique to the object. We have created getter and setter methods for the name and age variables. We have called the getter and setter methods using the student1 object. The output shows that the getter and setter methods are accessible through the student1 object. We have printed the name and age variables using the getter methods. The output shows that the getter methods are accessible through the student1 object. In the above example, we have used the setter method to validate the age variable that it must be greater than 18. If the age variable is less than 18, then it will print the message Age must be greater than 18. Otherwise, it will set the age variable to the age parameter of the set_age() method.

In this tutorial, you have learned about methods in Python classes. Methods are functions defined within a class. They encapsulate behavior that is specific to instances of the class. Understanding methods is essential for effective object-oriented programming. Let’s delve into the properties, advantages, and potential disadvantages of methods in Python classes. You have also learned about different types of methods in Python classes Like Instance Methods, Class Methods, Static Methods, Getter and Setter Methods. You have also learned about the properties, advantages, and potential disadvantages of methods in Python classes. For more information, visit the Python Classes page. For more tutorials like this, visit the Python Central Hub.


sketch What the dot does to a method p5.js
A function written in a class body is an ordinary function. Reaching it through an instance runs the descriptor protocol, which wraps it in a bound method carrying the instance -- and that wrapper is what supplies self. Reaching it through the class gives you the bare function, which is why calling it there demands self by hand.
pch.quizTag pch.quizDefaultTitle
  1. What is `C.m` when `m` is defined with `def m(self)` in class `C`?

    pch.quizShowAnswer

    B — A plain function — Verified: `C.m` prints as `<function C.m ...>`. The binding happens on attribute access through an INSTANCE, not on the class.

  2. `C.m()` with no arguments does what?

    pch.quizShowAnswer

    B — TypeError: missing 1 required positional argument: 'self' — Reaching the function through the class gives you the bare function, so you must supply `self` — `C.m(c)` works fine.

  3. Is `c.m is c.m` True?

    pch.quizShowAnswer

    B — No — a new bound method object is built on every access — Verified as False. So a bound method cannot be removed from a list of callbacks by identity, and is not stable as a dictionary key. Compare with `==`, or hold one reference.

  4. Why does storing `obj.method` as a long-lived callback keep `obj` alive?

    pch.quizShowAnswer

    B — The bound method holds a strong reference through `__self__` — That reference is exactly what makes `self` available later. `weakref.WeakMethod` exists for when you want the callback not to keep the object alive.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading