What are Regular Methods in Python? What are Static Methods in Python? What are Class Methods in Python?
 

In this post, I will be answering these questions in the simplest ways possible.
 

So, let's go.
 

What is a Method in Python?
 

Methods are simply functions inside a class; easy right?
 

class Human():

    """
    Below is a special method called a constructor and it is 
    used to specify arguments that should be passed on the 
    instantiation of a class
    """

    def __init__(self, firstname, lastname, age, height):
        self.firstname = firstname
        self.lastname = lastname
        self.age = age
        self.height = height

 

What are Regular Methods in Python?

Regular methods are methods that are used to manipulate an object per instance. The differences between the special method and a regular method are special methods are not user-defined rather they are in-built into python and that special methods start and end with double underscores(__name__, also called dunda)
 

class Human():

    """
    Below is a special method called a constructor and it is 
    used to specify arguments that should be passed on the 
    instantiation of a class
    """

    def __init__(self, firstname, lastname, age, height):
        self.firstname = firstname
        self.lastname = lastname
        self.age = age
        self.height = height

    """
    The function below gets the full name of an object
    after you must have passed the firstname and lastname
    on instantiation 
    """

    def get_full_name(self):
        return self.firstname + self.lastname

 

The get_full_name method above would be used the get the full name depending on the value passed to the class on instantiation. This full name value would only be for that particular instance and not for the whole class, i.e it does not change the full name value of the whole class.

This is how it is run.

 

person_1 = Human('John','Doe',45,6)

person_1.get_full_name()

 

What are Class Methods in Python?

Class methods are kind of the opposite of a regular method. It is used to manipulate the values of a class generally, i.e any value changed using a class method will be changed for the whole class.

 

A class method should be given a @classmethod decorator to tell python to treat the method as one.

 

Also, a class will always take cls as its first argument just like the regular method would take self as its first argument.

 

Note: Using cls and self is only conventional and not a must. But you are advised to use it for simplicity and code readability.

 

class Employee():
    
    raise = 1.05

    """
    Below is a special method called a constructor and it is 
    used to specify arguments that should be passed on the 
    instantiation of a class
    """

    def __init__(self, firstname, lastname, pay):
        self.firstname = firstname
        self.lastname = lastname
        self.pay = pay

    """
    The function below overrides the raise variable that
    was declared by the object
    """

    @classmethod
    def set_raise_amount(cls, amount):
        cls.raise = amount


A simple Employee class that sets an employee and a raise.

 

In this case, when the set_raise_amount function is called, it changes the original raise value set by the class therefore all instantiation henceforth takes the new raise amount as the value.

 

This is how it is run.

 

Employee.set_raise_amount(1.07)

 

Note: the class method can also be called by instance and get the same result like this:

 

employee_1 = Employee('John','Doe',45000)

employee_1.set_raise_amount(1.07)

 

But, this doesn't make much sense because it actually changing the value of raise for the whole object so why call the method with an instance.

 

What are Static Methods in Python?

A static method is just a regular function that has some logical connections with the class.

 

Unlike regular methods and class methods, It doesn't take self or cls as its first argument.

 

A static method should be given a @staticmethod decorator to tell python to treat the method as one.

 

class Employee():
    
    """
    Below is a special method called a constructor and it is 
    used to specify arguments that should be passed on the 
    instantiation of a class
    """

    def __init__(self, firstname, lastname, pay):
        self.firstname = firstname
        self.lastname = lastname
        self.pay = pay

    """
    The function below takes a payment amount and returns
    how much it will result in a year.
    """

    @staticmethod
    def pay_per_annum(payment):
        return f"{payment * 12}"

 

The pay_per_annum method is logically connected to the class but it is not tied to either an instance of the class in order to run.

 

This is how it is run.

 

Employee.pay_per_annum(60000)

 

A good sign to check when choosing to make a method either a class method or a static method is to observe if the method would be needing values directly from the class or not.

 

Conclusion

Endeavor to practice using other scenarios to get more understanding of these concepts.

 

Methods in Python are similar to other programming languages so if you have backgrounds in other languages try to relate them to these in order to have a better understanding.

 

Follow me on Twitter for more amazing content.

 

Keep coding. Keep learning.



This was originally published on codingwithease.com