Methods in Python O.O.P

titusnjuguna

Tito

Posted on August 17, 2021

Methods in Python O.O.P

methods in python

Introduction

This is a series of Simplified Object-oriented programming in python. Kindly check out the post before proceeding. As discussed in that post,here we will discuss the following:

  • Definition of Methods
  • Types of methods in python

What is a Method?

Recall definition of a class, we mentioned that an object has two properties: an attribute and behavior. Now in a simpler term, a method is a way of defining behavior of an object.

Python offers various types of these methods. These are crucial to becoming an efficient programmer and consequently are useful for a data science professional.

Types of Methods in Python

There are basically three types of methods in Python:

  • Instance Method
  • Class Method
  • Static Method

1. Instance method

This is the most commonly used type of method and the purpose of instance methods is to set or get details about instances (objects). They have one default parameter self, although you can add other parameters.
Example 1 of instance method(with default parameter only)

class Car:
   def instance_method(self):
       return("This is an instance method")
Enter fullscreen mode Exit fullscreen mode

Example 2 (with additional parameter)

class Car:
    def instance_method(self,b):
       return f 'this is instance method b={b}.'
Enter fullscreen mode Exit fullscreen mode

2. Class Method

The purpose of class methods is to set or get details/status of the class.When defining a class method, you have to identify it as class method with the help of the @classmethod decorator and the class method takes one default parameter cls

Lets create a class method

class Car:
    @classmethod
   def class_method(cls):
      return ("This is a class method")
Enter fullscreen mode Exit fullscreen mode

Let's try access the method

obj1 = Car()
obj1.class_method()
Enter fullscreen mode Exit fullscreen mode

Output:

This is a class method
Enter fullscreen mode Exit fullscreen mode

Different from other methods, we can access the class methods directly without creating an instance or object of the class.
using class_name.method_name()
Let’s see how:

Car.class_method()
Enter fullscreen mode Exit fullscreen mode

Output:

This is a class method
Enter fullscreen mode Exit fullscreen mode

3.Static Method

Static methods work independently,they do not need to access the class data.Since they are not attached to any class attribute, they cannot get or set the instance state or class state.

To define a static method, we can use the @staticmethod decorator there is no need to pass any special or default parameters.
Example:

class Car:
  @staticmethod
  def static_method():
    return "This is a static method."
Enter fullscreen mode Exit fullscreen mode

Just like the class method, we can call static methods directly, without creating an object/instance of the class:
using class_name.Static_method()

💖 💪 🙅 🚩
titusnjuguna
Tito

Posted on August 17, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Methods in Python O.O.P
python Methods in Python O.O.P

August 17, 2021