...

/

Class, Instance and Static Methods

Class, Instance and Static Methods

Learn comparison among class, instance and static methods.

Introduction

There are three types of methods defined on the Python object:

  • Class method
  • Instance method
  • Static method

Let’s begin writing a class that contains all the types mentioned earlier.

Press + to interact
class MyClass:
# Instance method
def instancemethod(self, arg1, arg2, ...):
return 'instance method called', self
# Class method
@classmethod
def classmethod(cls, arg1, arg2, arg3):
return 'class method called', cls
# Static method
@staticmethod
def staticmethod(arg1, arg2, arg3):
return 'static method called'

Let’s discuss them one by one.

Instance method

The first method instanceMethod is an instance method. This type is mostly used. It can accept any number of parameters. Here, self points to an instance of MyClass when this method is called. ...