Instance, Class, and Static Method

Jan 14, 20243 mins read

Python

Instance, Class, and Static Method

The ‘method’ terminology is quite popular whenever we are discussing about programming languages that support Object-Oriented Design.

During the process of creating the template(i.e. Class), we would have specified the attributes(e.g. legs, eye, weight, etc) and the actions(e.g. run, move, etc) that the real cat(instance) must exhibit.

Technically, those actions are referred to as methods. Methods are ‘Functions’ (‘Actions’ to a layman) that reside inside of the class.

Instance method, as the name suggests, is tied to a real object(instance) after it has been created from the Template(Class). This set of methods can act or work only on an instance of a Class.

Time to write code 🧑‍💻️. Implementation in Pytton

[*]snip

We defined the Template/Blueprint/Class for creating a real cat object by utilizing the ‘class’ keyword.

Here we listed the attributes we expect as name and age.

__init__ is called a constructor(a special kind of method). It’s main purpose is to populate a real new object(cat) with its attributes at the point of creation.

[*]’self’ (some languages called it ‘this’) refers or points to this real object(instance) that is being created.

With this template up here, creating new real cats becomes easy.

cat1 = Cat(‘Bingo’, 2)

cat2 = Cat(‘Nerd’, 1)

so we can easily know the name and age of different cats

[*]cat1.name1

Bingo

[*]cat1.age

2

Instance Method:

Remember we mentioned ‘self’ (‘this’ in some languages) earlier. The only way we are able to create an instance method in python is by mentioning self as the first parameter passed to a function declaration.

We define a method `eat` (it is function but called method since it resides in a class) and passed the first param as self. So this make it an instance method because we would be able to call it on every instance(real object) we create.

[*] Usage

[*]Output

Bingo is eating

Nerd is eating

So we were able to instruct the two cats(Bingo and Nerd) to eat.

[*]Quick Note:

- Instance method are able to acess their internal state(attribe) using self.name_of_attribute. They have access to their class using self.__class__ and are able to modify their class state(attribute)

__init__ as mentioned earlier is a constructor method(used in the constructor of an instance) and a specifically used by python.s So it is impossible to call it on those instances(cat1 and cat2).

- Using ‘self’ as used above is the convention in the python community and it’s highly encouraged you stick to it. Using ‘this’ or any other would work

Class method 

A class method can be accessed through the class just as the instance method can be accessed through an instance. It can be utilised whenever we want make changes across all the instances.

Say two cats come from the same parent. an attribute like the parent name(surname) should be widely shared across the class so that whenever it is modified it reflects across all other instances.

[*]Usage

[*]Output

‘Something’

So in the above example cat1 was able to know when the surname changed despite that the surname was changed by another cat.

In the above example, we called the class method on an instance(cat2). Since it is a Class method, calling it on the class would work well.

[*]Cat.change_name(‘Another name’)

Class method takes the first parameter as the cls(this refers to the class) and also decorated with @classmethod. It works directly on the Class without any instantiation.

Static Method

Static Method are normal functions that sits in a class. It is widely used as utils(utility function). Static method does need access to instance or class.

It is decorated with @staticmethod and does not need an instance to be created before usage.

It’s method works directory on the Class.

Cat.get_date()

Ready to take your Coding skills to the next level ? Check Youtube for Exclusive Content. Consider Subscribing to stay tuned.